How to install Passenger for Nginx + few tricks

In this post I’ll cover how to install ‘passenger’ for nginx. What is passenger and why do you need it? Well, if you deploy your ruby applications straight to the cloud (heroku, etc), you don’t need that. But when it comes to running ruby apps on your own dedicated server (which I prefer), you need some magic to achieve this goal. If you don’t have your own dedicated/virtual server, you’ll probably won’t find this article very useful.

Before following instructions in this manual, make sure you have RVM installed (see my previous post). When you have RVM installed, you’ll need some sort of server to run your apps on. You can always use rails s -b command, but built-in rails server is not production-ready and works well for development purposes only. So here the passenger comes into play.

Passenger itself is little bit tricky. The trickiest part of using Passenger and Nginx is that you can’t update your nginx – you have to compile, build new version, install, and remove old one. New version should be installed into the new directory.

OK, now the question “how”. Start with installing passenger gem:

gem install passenger

And then

rvmsudo passenger-install-nginx-module

The command above will run interactive and very friendly (greetings to Phusion team in Netherlands!) step-by-step guide where I just leave default values.

Now let’s check if everything’s installed:

cd /opt/nginx/
ls -l

Sample output:

drwxr-xr-x 2 root root 4096 May  4 23:33 conf
drwxr-xr-x 2 root root 4096 May  4 23:33 html
drwxr-xr-x 2 root root 4096 May  4 23:33 logs
drwxr-xr-x 2 root root 4096 May  4 23:33 sbin

One more thing I really recommend to do right now is to make a backup copy of config file:

cd /opt/nginx/conf/
cp nginx.conf nginx.conf.bak.1

If you have your old nginx installation running, make backups for your config files in /etc/nginx because now we’re going to uninstall previous version:

apt-get remove nginx nginx-full nginx-light nginx-naxsi nginx-common

Done? Great! Time to run nginx:

sudo /opt/nginx/sbin/nginx

Let’s try to get a page from localhost and show it on your screen to see if newly installed nginx is running:

wget -O - http://localhost

You should see something like “Welcome to nginx!” and some html code. Well, great! But how to stop it? Here is the trick:

sudo kill $(cat /opt/nginx/logs/

Run wget command again and see that it has failed. Now we can run and stop nginx instances. What now? Everything works, but I’d love to get those “service nginx start”, “service nginx stop” commands back. Thanks to folks for posting this github gist. Here are few commands you need to replace init.d script:

cd /tmp
sudo cp /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

Now we can use “service nginx start”, “service nginx stop”, “service nginx stop”, etc. commands. Check yourself with wget-command above.

Now you can update nginx.conf with your previous configuration. When hosting ruby/rails apps, make sure you pointing them to public folder:

   server {
      listen 80;
      root /somewhere/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;

This command also helped when I messed everything and upgraded my ruby from 2.2.0 to 2.3.0:

rvmsudo passenger-install-nginx-module

Basically, it downloads and rebuilds your nginx with the settings for latest ruby in your system.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s