Today I’m going to run you through getting your Rails app running on Ubuntu, Ruby Enterprise Edition (REE), Rails, Nginx and Phusion Passenger for Nginx. Lastly, I’ll quickly show you how to successfully deploy your Rails app to a sub URI without any 403 Forbidden errors.
Here are my notes from one of my client’s latest Rails deployment.
Ruby Enterprise Edition (REE):
We’re going to start by installing Ruby Enterprise Edition (REE). Download the latest .deb REE package, then install it:
You should see something similar to ruby 1.8.7 (2010-04-19 patchlevel 253) … Ruby Enterprise Edition 2010.02. Now, update your RubyGem package manager and all of your installed gems:
Ruby on Rails
At this point you should make sure you’ve installed the Ruby on Rails gem. Don’t worry, you’ve probably already done that. Here’s the command to install Rails (just in case):
Install, Configure and Manage Nginx
First, let’s install the Open SSL library. You’re going to need this later for SSL support and management:
Next, we are going to install the Passenger for Nginx module. This module will install both Nginx and Passenger:
Follow the default options. Soon, you’ll be prompted with two options to either select 1 or to select 2.
Choose option 2 if you need to install additional Nginx modules. One module that comes to mind is the Nginx SSL module.
For this tutorial, select option 1. Let the install finish. Congrats, you’ve just installed a blazing fast Webserver (Nginx) and Rails application server (Passenger)!
You will also need to create a user and group for nginx. Issue the following command to do so:
Finally, let’s create an init.d script and have it boot at startup. The init.d will allow us to control Nginx:
Copy and paste the following Nginx init script into the new file:
To save the file using nano, press ctrl+o, then enter. Then exit by pressing ctrl+x.
Change permission on the script to make it an executable:
Have the new executable run at startup:
Now, you have the power to start, stop and restart Nginx. Start your shiny new Nginx Webserver with this command:
Navigate to your server’s IP address using Firefox or your favorite browser. You should see “Welcome to Nginx!” This means Nginx is up-and-running. Now we can focus on configuring Nginx for your Rails web application.
Edit your Nginx configuration:
Make sure Nginx knows the location of your passenger and ruby installation:
For fun, let’s modify our Server section to deploy our Rails app to a Sub URI:
To do this, we have to make a symlink from our Rails app’s public directory to the document root of our static website:
This command will create a symlink called blog in the document root /www/domain.com/public_html/.
Remember, if you want to make the sub uri work, do not create a folder called blog in the document root. This will get you a nice Rails sub uri 403 Forbidden error from your passenger Nginx setup.
For a typical Xen VPS setup, you should also think about adding the following tweaks to your Nginx conf:
Read more about the the passenger_max_pool_size directive. The maximum value depends on your server’s cpu processor and other factors. Do experiment!
When you’re finished, save the file and restart Nginx:
In the future you can restart the entire Nginx server or only restart your application with the following command:
The next time you load your Rails app it will restart.
Passenger also has some pretty cool built-in monitoring applications. You can access them using these commands:
I suggest installing another utility to see what’s using memory on your entire server. It’s called htop:
Well, you’re done really! I hope this helps.
For the uninitiated, deploying to Ubuntu (or any Linux distro) can be daunting. But, once you get over having nothing but a black screen to work with, you’ll realize how unshackled and productive you feel. Good luck Beloved!