Deploying Ruby on Rails on Ubuntu Feisty Fawn via Mongrel and Apache

Note:

  • Once again, another quick, down and dirty solution. The guys behind mongrel have even written up how to set it up. This is a more condensed form, and suited for Ubuntu Feisty Fawn
  • This is not the best solution for deployment, but it works if you want to do early testing/deployment for clients. An ideal solution would be utilizing capistrano and mongrel_cluster.
  • After installing mongrel, when executing ./script/server, you will not use Webrick anymore, but instead mongrel for development. You can still use Webrick if you want, but there isn’t much of a difference.
  • This requires the use of RubyGems. If you don’t have it installed, see my guide on setting up Rails using RubyGems.

Plan of Attack:

  • Install and test Mongrel
  • Install and test Apache
  • Additional tips
    • Mongrel Service
    • Let Apache handle Requests for Static Files
  • After thoughts

Install Mongrel

Enter into the Terminal:

sudo apt-get install ruby1.8-dev build-essential
sudo gem install -y mongrel

Note: If you see the following make sure you select the latest version of mongrel for ruby. In this case option 2.

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
> 

Testing Mongrel

Go into your rails application root directory, and execute:

mongrel_rails start -p 8000

Open up your webbrowser and go to http://localhost:8000 and your application should load.
Note: mongrel_rails has alot of command line options. Executing mongrel_rails -h will get you more information.

Install Apache

Enter into the Terminal:

sudo apt-get install apache2
sudo a2enmod proxy_http

You’ll need change one file, specifically /etc/apache2/mods-available/proxy.conf
Look for the following line, it should be on line 11:

#Allow from .example.com

and replace it with:

Allow from all

Now as root, open up /etc/apache2/sites-available/railsapp File.
Enter the following:

<VirtualHost *>
    ServerName railsapp.com
    ServerAlias www.railsapp.com

    ProxyPass / http://www.railsapp.com:8000/
    ProxyPassReverse / http://www.railsapp.com:8000/
    ProxyPreserveHost on
</VirtualHost>

Note: replace railsapp.com and www.railsapp.com with the actual domain/ip you will be using.

Enable the site and restart Apache

sudo a2ensite railsapp
sudo /etc/init.d/apache2 restart

Testing Apache

Fire up your browser. go to your site. It should be up and running.

Troubleshooting: If you get 403 forbidden, try removing the default site ( Terminal sudo a2dissite default) and change the (www.)railsapp.com setting to localhost. Optionally remove the lines containing ServerName and ServerAlias.

Additional Tips

  • mongrel will need to be started up again, when you reboot your machine. We will cover setting it up as a service further down.
  • mongrel can be given flags to run under a different environment. Perhaps for simple production sites.
  • If two people try to access the site at the very same time, expect slight delays as only one intsance of mongrel is running.
  • From now on mongrel will handle all requests. But this is a daft, as Apache can easily handle requests for static files: javascript, stylesheet and images. For solution view further down.
  • If you want to deploy more than one rails application you’ll have to specify a ServerName, and ensure that mongrel is using different ports

Mongrel Service

There are some tips online on how to setup a mongrel service.

I like the second one by Bojan Mihelac as you can customize the default port, and the environment the mongrel instance is running in.

Here it is reproduced, make sure to install it in /etc/init.d/mongrel File. Then you can start it by executing /etc/init.d/mongrel start Terminal. Works with start, stop, and restart.

Letting Apache handle Requests for Static Files

Include this into your railsapp file File, inside the <VirtualHost>

ProxyPass /images !
ProxyPass /stylesheets !
#continue with other static files that should be served by apache

Alias /images /path/to/public/images
Alias /stylesheets /path/to/public/stylesheets
#continue with aliases for static content


Note: Ensure that the /path/to/public is readable by Apache

After thoughts

Using Mongrel for light sites is alright but it doesn’t scale well. Thats why we have mongrel_cluster to the recuse.

3 Comments »

  1. Jeremy Roush said,

    June 2, 2007 @ 8:56 am

    Very damn cool. Thanks a bunch.

  2. Fengbo Xie said,

    October 6, 2007 @ 1:20 pm

    Very useful init.d service script. Thanks a lot.

  3. roger said,

    September 27, 2008 @ 3:37 am

    Nice. Mod_rails is nice, too :)

RSS feed for comments on this post · TrackBack URI

Leave a Comment