Deploying Rails with Phusion Passenger and Nginx in 5 minutes

Share this article

When I first started with RoR, I used to wonder how developers deployed their application with such ease. Here’s a guide which should come in handy if you want to deploy a Rails app with Nginx, without making the whole process look tedious. I could get this done in 5 minutes, how about you?

Phusion Passenger / mod_rails

Phusion Passenger is a robust module for Apache and Nginx servers, and is preferred over any other module for deploying web apps developed using the Rails framework. But if you’re running a Microsoft Windows platform, you might have to look for other alternatives since Phusion Passenger is compatible with anything but Windows. For this tutorial, I’ll be using Ubuntu, but you can go anything unix-like.

Choose between nginx and Apache

Apache and Nginx are widely adopted web servers. Although Apache is the best known process based web server, Nginx is most suited if you’re in need of a fast, and responsive server. It performs miracles when the server is under heavy load. For this post I’ll be relying on Nginx server, but if you have Apache in your stack, the method of hooking up is not much different from Nginx.

Deploying Rails App with Phusion Passenger and Nginx

First, download the latest stable version of Nginx from nginx.org. You will need its source code to install the Nginx module later. I’ll be using nginx-1.0.15 which is the latest stable release at the time of writing this tutorial.
cd /tmp
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar -xvzf nginx-1.0.15.tar.gz
Assuming that you have Ruby, Rails, and RubyGems installed, enter the following code to install Phusion passenger gem. gem install passenger
Now you’ll have to configure your Nginx to work with Passenger. Run the following command in your favorite shell.
passenger-install-nginx-module
The interactive installer will guide you through and you will have a completely functional Nginx server in just a few minutes. I’ve found the installer rather fun to use. If anything goes wrong, you’ll be notified about it, and the installer will display the probable cause and also the solution to overcome it. What more should you expect? When prompted, go for the custom Nginx installation method (option 2). This will be to your advantage in the long run and if you’d like to add some additional parameters to Nginx’s config script, this is the perfect choice. For example, if your application requires SSL support and you have OpenSSL source code lying in your server, you can painlessly compile them together by passing a parameter to the configuration file. When prompted, specify the location of the nginx source code which we downloaded earlier. In our case, it is /tmp/nginx-1.0.15. The next few steps are self explanatory. If everything goes well, you should see something along the lines “Nginx with Passenger support was successfully installed.” The installer might edit your Nginx configuration file in order to add some snippet of code, or you might have to do it manually. Either way, you’ll be notified about it. I was asked to configure it myself and here is what it prompted me to do. (The code given below may vary from user to user).
Please edit your Nginx configuration file (probably /opt/nginx/conf/nginx.conf),
http {
...
passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11;
passenger_ruby /usr/lib/ruby1.8;
...
}
server {
listen 80;
server_name www.yourhost.com;
root /somewhere/public; #
passenger_enabled on;
}
Copy the above content into your nginx.config and replace ‘somewhere’ (mentioned above) with the absolute path to your application. Here’s what (my) final configuration looks like;
worker_processes 1;
events {
worker_connections 1024;
}
http {
passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11;
passenger_ruby /usr/bin/ruby1.8;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name myapplication.com;
root /home/root/application/public;
passenger_use_global_queue on;
passenger_enabled on;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Set your Database environment, and restart nginx server using the following
sudo /etc/init.d/nginx restart
Voilà! That’s it and congratulations. You have successfully deployed your rails application. By entering your application URL in your browser and hopefully you should find it working. If you cant see it running or you land on an error page, check Nginx’s error.log (can be found at nginx.log file) or your application log file for more information. It could be because you missed certain gem or certain dependencies were not satisfied.

Frequently Asked Questions (FAQs) about Deploying Rails with Phusion Passenger and Nginx

What is the role of Nginx in deploying Rails with Phusion Passenger?

Nginx is a high-performance HTTP server and reverse proxy. It is used in conjunction with Phusion Passenger to deploy Rails applications. Nginx serves static files (like CSS, JavaScript, and images) directly, quickly, and efficiently, while Phusion Passenger handles the Ruby on Rails application code. This combination allows for efficient handling of both static and dynamic content, leading to faster load times and better overall performance for your Rails application.

How can I troubleshoot common issues when deploying Rails with Phusion Passenger and Nginx?

Troubleshooting issues when deploying Rails with Phusion Passenger and Nginx can involve checking several areas. First, ensure that your Rails application is running correctly in a development environment. Next, check the Nginx and Phusion Passenger configuration files for any errors or misconfigurations. The Nginx error logs can also provide valuable information about any issues. If you’re still having trouble, the Phusion Passenger documentation provides a comprehensive guide to troubleshooting common problems.

Can I use Phusion Passenger with other servers besides Nginx?

Yes, Phusion Passenger is also compatible with Apache. However, Nginx is often preferred for its performance and efficiency, especially for serving static files. If you’re already using Apache and don’t want to switch, you can still use Phusion Passenger to deploy your Rails application.

How can I optimize the performance of my Rails application with Phusion Passenger and Nginx?

There are several ways to optimize the performance of your Rails application when deploying with Phusion Passenger and Nginx. First, ensure that your Nginx configuration is optimized for your specific use case. This can involve tuning parameters like the number of worker processes and connections per worker. Additionally, Phusion Passenger offers several configuration options that can help optimize your Rails application’s performance, such as the number of application processes and threads.

What are the prerequisites for deploying Rails with Phusion Passenger and Nginx?

Before deploying Rails with Phusion Passenger and Nginx, you’ll need to have Ruby and Rails installed on your server. You’ll also need to install Nginx and Phusion Passenger. Additionally, your Rails application should be in a production-ready state, with all necessary database migrations run and assets precompiled.

How can I secure my Rails application when deploying with Phusion Passenger and Nginx?

Securing your Rails application involves several steps. First, ensure that your server is secure by keeping all software up to date and following best practices for server security. Next, configure Nginx to use HTTPS and secure headers. Finally, ensure that your Rails application follows best practices for secure coding.

Can I deploy multiple Rails applications on the same server with Phusion Passenger and Nginx?

Yes, you can deploy multiple Rails applications on the same server with Phusion Passenger and Nginx. Each application will need its own configuration block in the Nginx configuration file, but they can all run on the same server.

How can I update my Rails application after it’s been deployed with Phusion Passenger and Nginx?

Updating your Rails application after it’s been deployed involves pulling the latest code from your version control system, running any necessary database migrations, precompiling assets, and then restarting Phusion Passenger. This can be automated with a deployment script or tool.

What is the role of the ‘passenger_root’ directive in the Nginx configuration file?

The ‘passenger_root’ directive in the Nginx configuration file specifies the location of the Phusion Passenger installation. This is necessary for Nginx to be able to start and manage Phusion Passenger.

How can I scale my Rails application when deploying with Phusion Passenger and Nginx?

Scaling your Rails application when deploying with Phusion Passenger and Nginx can involve several strategies. One common approach is to increase the number of application processes or threads managed by Phusion Passenger. Another approach is to add more servers and distribute traffic among them using a load balancer.

Manjunath MManjunath M
View Author

Founder of Storylens - A no-code website building platform based on JavaScript components.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week