Deploying a Ruby application
on Passenger + Nginx

This guide teaches you how you can deploy your app to a server. This guide assumes that you have already done the following, and that you know how to do them:

  • Passenger and Nginx are already installed on the server.
  • You have already transferred the application's code to the server.
  • You have already installed Ruby.
  • You have already installed all necessary dependencies that your application needs.

If you miss one of these prerequisites or don't know how to perform it, then we recommend you to follow the Deployment tutorial. The deployment tutorial is end-to-end guide and teaches you how to install every component.

Table of contents

  • Loading...

Determine the Ruby command that Passenger should use

Before you begin, you need to tell Passenger which Ruby command it should use to run your app, just in case there are multiple Ruby interpreters on your system. Please run passenger-config about ruby-command to find out which Ruby interpreter you are using. For example:

$ passenger-config about ruby-command
passenger-config was invoked through the following Ruby interpreter:
  Command: /usr/local/rvm/gems/ruby-2.2.3/wrappers/ruby
  Version: ruby 2.2.3p85 (2015-02-26 revision 49769) [x86_64-linux]
  ...

Please take note of the path after "Command" (in this example, /usr/local/rvm/gems/ruby-2.2.3/wrappers/ruby). You will need it in one of the next steps.

Deploying an app to a virtual host's root

To deploy an app to an Nginx virtual host's root path, the following steps must be taken:

  • Add a virtual host entry (server block) to your Nginx configuration file.
  • The server block's root must point to your application's public subdirectory.
  • You must also set passenger_enabled on in the server block.

Here is an example:

server {
    listen 80;
    server_name yourserver.com;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /path-to-app/public;

    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /path-to-ruby;
}

Replace yourserver.com with your server's host name, and replace /path-to-app with your application's code directory path. However, make sure that Nginx is configured to point to the public subdirectory inside it!

Replace /path-to-ruby with the Ruby command that you obtained in Determine the Ruby command that Passenger should use.

When you are done, restart Nginx. If you installed Nginx via our Debian or RPM packages:

$ sudo service nginx restart

Otherwise, if you installed Nginx from source and don't have an init script:

$ sudo kill $(cat /opt/nginx/logs/nginx.pid)
$ sudo /opt/nginx/sbin/nginx

You are now done. If you run into any problems, please refer to the troubleshooting guide.

Deploying an app to a sub-URI or subdirectory

You can also deploy an app to a sub-URI instead of the root URI. For example, suppose that you already have a virtual host for the application /websites/phusion:

http {
    ...

    server {
        listen 80;
        server_name www.phusion.nl;
        root /websites/phusion/public;
        passenger_enabled on;
    }
}

And you want your application, located in /websites/secondapp, to be accessible from the URL http://www.phusion.nl/subpath. To do this, you need to perform the following:

  • Create a location with parameter ~ ^/<SUBURI>(/.*|$). This is a regular expression that says: "match everything that is exactly , or starts with /".
  • Inside the location block, set alias <PATH TO YOUR APPLICATION PUBLIC SUBDIRECTORY>$1.
  • Inside the location block, set passenger_base_uri <SUBURI>.
  • Inside the location block, set passenger_app_root <PATH TO YOUR APPLICATION ROOT>.
  • Inside the location block, set passenger_document_root <PATH TO YOUR APPLICATIOS PUBLIC SUBDIRECTORY>.
  • Inside the location block, set app_type and startup_file accordingly.

Here is an example:

http {
    ...

    server {
        listen 80;
        server_name www.phusion.nl;
        root /websites/phusion/public;
        passenger_enabled on;

        # This block has been added.
        location ~ ^/subpath(/.*|$) {
            alias /websites/secondapp/public$1;  # <-- be sure to point to 'public'!
            passenger_base_uri /subpath;
            passenger_app_root /websites/secondapp;
            passenger_document_root /websites/secondapp/public;
            passenger_enabled on;

        }
    }
}

When you are done, restart Nginx. If you installed Nginx via our Debian or RPM packages:

$ sudo service nginx restart

Otherwise, if you installed Nginx from source and don't have an init script:

$ sudo kill $(cat /opt/nginx/logs/nginx.pid)
$ sudo /opt/nginx/sbin/nginx

You are now done. If you run into any problems, please refer to the troubleshooting guide.