Posted by Vince Wadhwani on May 09, 2007

Update: Nov 16, 2007 - See this article for instructions for Gutsy Gibbon

This is a follow-up post the previous articles I wrote on installing Ruby on Rails for Ubuntu Dapper Drake and Edgy Eft. This time around, I've moved away from Lighttpd and am deploying using Mongrel and Nginx. Why? I had a ton of problems with Lighttpd, from unexpected crashes to unexpected behavior. I learned an important lesson which I'll share: When possible, develop and deploy using the same tools. Because Mongrel is so great, we can do just that. So let's get started!

As usual, the first thing you want to do before starting any installation on Ubuntu is enable the repositories you'll need and then update your system. You can either uncomment the repositories in /etc/apt/sources.list or use Synaptic to enable UNIVERSE.

deb http://us.archive.ubuntu.com/ubuntu feisty universe
deb-src http://us.archive.ubuntu.com/ubuntu feisty universe

1. Once that's done, let's update:

sudo apt-get update
sudo apt-get dist-upgrade

2. We'll be installing some software that needs to be built this time around (don't worry it won't be scary) so we'll need to get packages required for compiling. In one swoop, you can type this command and get everything you need:

sudo apt-get install build-essential

3. Once you've got the tools, it's time to grab MySQL, my database of choice, and Ruby. Just copy and paste this command into your terminal if you're shady on spelling:

sudo apt-get install ruby ri rdoc mysql-server libmysql-ruby ruby1.8-dev irb1.8 libdbd-mysql-perl libdbi-perl libmysql-ruby1.8 libmysqlclient15off libnet-daemon-perl libplrpc-perl libreadline-ruby1.8 libruby1.8 mysql-client-5.0 mysql-common mysql-server-5.0 rdoc1.8 ri1.8 ruby1.8

Update: Install Ruby 1.8.6 manually to get Mongrel Cluster 1.0.2 working!

4. We next need to grab the latest ruby gems from rubyforge. You may need to modify this command if a version after 0.9.4 is available.

sudo wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
tar xvzf rubygems-0.9.4.tgz
cd rubygems-0.9.4
sudo ruby setup.rb

5. Now let's install Ruby on Rails!
sudo gem install rails --include-dependencies

The first time I ran that, I got an error that said:
Bulk updating Gem source index for: http://gems.rubyforge.org
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find rails (> 0) in any repository

Solution? Run it again. No idea why it always craps out, but it's happened to me on two different servers. Maybe it's a bug .

6. Now, let's get nginx and fcgi:
sudo apt-get install libpcre3 nginx libfcgi-dev libfcgi-ruby1.8 libfcgi0c2

7. Like the previous HowTo's I wrote, this one will use phpmyadmin to manage the database manipulation. I still haven't gotten around to learning MySQL on the command line so I use phpymyadmin as a crutch. If you don't need it you can skip this step. Otherwise let's grab PHP. Note that we're not installing Apache2 because we already have Nginx.
sudo apt-get install libxml2 ucf php5-common php5-cgi php5-mysql phpmyadmin

8. We're almost done.. now it's time to get Mongrel.

sudo gem install mongrel
Select which gem to install for your platform (i486-linux)
1. mongrel 1.0.1 (mswin32)
2. mongrel 1.0.1 (ruby)
3. mongrel 1.0 (mswin32)
4. mongrel 1.0 (ruby)
5. Skip this gem
6. Cancel installation
> 2

Choose 2 unless a more recent (ruby) version of Mongrel is available. We'll say Y to all the dependencies.

Install required dependency daemons? [Yn] Y
Install required dependency fastthread? [Yn] Y

Now, if you skipped the part about getting the build-essential, you'll get an error that says: ERROR: Failed to build gem native extension. Go back and grab the build tools (sudo apt-get install build-essential) and try this step again.

sudo gem install mongrel_cluster

copy the init file over to /etc/init.d/ by typing this all on one line:
sudo /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster /etc/init.d/mongrel_cluster

Update: you may need to change the above command if a version of Mongrel Cluster > 1.0.2 exists

Next, add a path statement to mongrel_cluster file just above the CONF_DIR variable:
vi /etc init.d/mongrel_cluster
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local:/usr/local/sbin:/usr/local/bin
Thanks to Russ Brooks for the above tip. You may also want to change the USER=mongrel to USER=www-data.

Finally, let's modify permissions and make sure we boot mongrel on startup:
sudo chmod +x /etc/init.d/mongrel_cluster
sudo update-rc.d mongrel_cluster defaults

9. Congratulations, you've got everything installed! It's time to deploy. If you have an existing rails application on another server, let's move it over to our root at /var/www/
sudo mv myrailsapp /var/www/

10. Modify your permissions of your app and phpmyadmin:
sudo chown -R www-data:www-data myrailsapp
sudo chown -R www-data:www-data phpmyadmin

11. Setup the Mongrel Cluster (source). This will get us a group of 3 mongrel clusters running on port 8000. From within your myrailsapp/config folder, type:
sudo mongrel_rails cluster::configure -e production \ -p 8000 -N 3 -c /var/www/apps/myrailsapp -a 127.0.0.1 \ --user mongrel --group mongrel

Now let's create a symlink to that file from within /etc where all our configs live:
sudo mkdir /etc/mongrel_cluster
cd /etc/mongrel_cluster/
sudo ln -s /var/www/myrailsapp/config/mongrel_cluster.yml

You can download a sample mongrel_cluster file HERE. In any case, it's a good idea to download it and cross reference it to what the above command produced.

12. Next we're going to put a script into /var/www/phpmyadmin folder to spawn fastcgi on port 8888. Make sure you give it execute permissions using sudo chmod +x fastcgi_script

Download the script here. Inspiration for this script came from Alexey N. Kovyrin. It has been modified only for Ubuntu's PHP path. Don't forget to put it into /var/www/phpmyadmin

13. We're *almost* done. Next step is to configure Nginx. Here's a sample nginx.conf file for your /etc/nginx/ folder. It's set up to handle one rails app and phpmyadmin. Adding additional servers just means more server blocks.

14. Now that we've got everything set up, let's turn this sucker on!

Step 1: PHP: . /var/www/phpmyadmin/fastcgi_script
Step 2: Mongrel: /etc/init.d/mongrel_cluster start
Step 3: Nginx: /etc/init.d/nginx/ start


References: