Easier Rails in Ubuntu 7.04 beta 4/1/2007

With the upcoming release of Ubuntu 7, Apache 2.2 will be included in the standard package manager, meaning we no longer have to work in a hybrid managed and from-source environment to get a proper Rails stack up and running. Apache 2.2 added mod_proxy, giving us access to the ProxyBalancer directive that allows Apache to serve as a frontend to several Mongrel application servers.

Here is a quick-and-dirty guide to installing my application stack:

  • Ubuntu 7.04 beta
  • Apache 2.2 with mod_rewrite and mod_proxy
  • Mongrel 1.0.1
  • MySQL 5 (finally)
  • Ruby 1.8.5 and Rails 1.2.3

For starters, get yourself a copy of Ubuntu 7.04 beta and get it installed. No one should have to give you directions on that part. Once that's up and running, we need to turn it into a development environment, so fire up a terminal and bang out the following:

$ sudo apt-get install build-essential

Now open Synaptic Package Manager and select and install mysql-server, libmysqlclient15-dev, ruby, irb, rdoc, ruby1.8-dev, and rubygems. Accept all the dependencies it alerts you to, and move on. Its very important you get ruby1.8-dev installed, or rubygems will not work correctly. I believe this to be an error in Synaptic, but we have to deal.

Now get back to the command line. Using Ruby's Gem installer, its time to finish up the Rails side of our stack. This part is similar to my previous howto for Ubuntu 6.

$ sudo gem install rails --include-dependencies
$ sudo gem install mysql
$ sudo gem install daemons gem_plugin mongrel mongrel_cluster --include-dependencies

Again, accept the dependencies it asks you to build, and accept the highest-versioned, non-win32 option in the menus. At this point you have a fully-functioning Rails environment, so test it out. Start a new rails app and configure a cluster like this:

$ mkdir ~/rails ; cd ~/rails ; rails www
$ mongrel_rails cluster::configure -p 8001 -a 127.0.0.1 -N 3
$ mongrel_rails cluster::start

Test it out by browsing to http://127.0.0.1:8001/ and you should see the Rails welcome screen. If not, you've got something screwy happening and you should ask about it in the comments.

Now its time to get our Apache 2.2 frontend running. Start by using Synaptic to install apache2 (and its dependencies). If you like, include PHP or whatever else you think will be fun. After you've got it all installed, run these commands to enable mod_rewrite and mod_proxy:

$ sudo a2enmod rewrite
$ sudo a2enmod proxy
$ sudo a2enmod proxy_balancer
$ sudo /etc/init.d/apache2 restart

Now add a line to the bottom of /etc/apache2/apache2.conf that directs Apache to your configuration file, which I store in ~/conf/httpd.conf.

Include /home/rcrowley/conf/httpd.conf

In this new config file, wherever you put it, place the following code (replacing whatever you need with new values):

NameVirtualHost *:80
<VirtualHost *:80>
  ServerName richarddcrowley.org
  Include /home/rcrowley/conf/www.conf
</VirtualHost>
<VirtualHost *:80>
  ServerName www.richarddcrowley.org
  Include /home/rcrowley/conf/www.conf
</VirtualHost>

I choose to link out to another config file to keep from repeating myself when configuring both with-www and without-www versions of my sites. In the new file:

DocumentRoot /home/rcrowley/rails/www/public
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://www_mongrel%{REQUEST_URI} [P,QSA,L]
<Proxy balancer://www_mongrel>
  BalancerMember http://127.0.0.1:8001
  BalancerMember http://127.0.0.1:8002
  BalancerMember http://127.0.0.1:8003
</Proxy>

Now you’re pretty much done, but its likely throwing 403 errors at you for most any request. Since this is undesirable behavior, you need to edit /etc/apache2/mods-available/proxy.conf. I found this method to be less-than-helpful, so I commented every line in the file out, restarted Apache, and life became better.

The last thing necessary is making your Mongrel cluster start up with your machine. This is exactly like last time. Create /etc/init.d/mongrel and put this in it:

#! /bin/sh
do_start()
{
  echo "Starting Mongrel..."
  mongrel_rails cluster::start -C ~rcrowley/rails/www/config/mongrel_cluster.yml
}
do_stop()
{
  echo "Stopping Mongrel..."
  mongrel_rails cluster::stop -C ~rcrowley/rails/www/config/mongrel_cluster.yml
}
case "$1" in
  start)
    do_start
    ;;
  stop)
    do_stop
    ;;
  restart|force-reload)
    do_stop
    do_start
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
    exit 3
    ;;
esac

Now make it run on startup:

$ sudo update-rc.d mongrel defaults

Go forth and hack.

Tags:

Comments

Max 4/22/2007

First, thanks for the guide. I'm running the new official 7.04 rather than the beta, and it's being run in Windows through VMWare.

Second--everything was going swimmingly up until: ***** begin my terminal output **** user@computer:~$ sudo gem install mysql

Select which gem to install for your platform (i486-linux) 1. mysql 2.7.3 (mswin32) 2. mysql 2.7.1 (mswin32) 3. mysql 2.7 (ruby) 4. mysql 2.6 (ruby) 5. mysql 2.5.1 (ruby) 6. Cancel installation > 3 Building native extensions. This could take a while... *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.

Provided configuration options: ************************************ *** OPTIONS EDITED OUT FOR SPACE *** ************************************

ERROR: While executing gem ... (RuntimeError) ERROR: Failed to build gem native extension. Gem files will remain installed in /var/lib/gems/1.8/gems/mysql-2.7 for inspection.

Results logged to /var/lib/gems/1.8/gems/mysql-2.7/gem_make.out **** end terminal output ***

What the heck? “gem_make.out” is empty. I'll be the first to tell you I'm a nincompoop at Linux, so if you don't have any ideas, I'll reformat and try again. For the record, I'm not using the 7.04 beta—I'm using the official 7.04

I appreciate any help homeslice

Richard Crowley 4/23/2007

When I ran across this error, I believe the ruby1.8-dev package fixed my problems. Double check that you have that installed and perhaps reinstall rubygems. If that doesn't work feel free to post back. (And please use a real email address next time.)

Bjarki 4/24/2007

I ran into the same problem and I fixed it by installing

sudo apt-get install libmysqlclient15-dev

I found the solution here http://ubuntuforums.org/archive/index.php/t-36505.html%3C/t-230524.html

Richard Crowley 4/24/2007

Thanks, Bjarki. I’m adding that to the list of packages people need to install.

Bjarki 4/25/2007

!!! PID file log/mongrel.8001.pid does not exist. Not running?

I ran into this problem and it took me a while to figure out how to fix it. If you get an error similar to the one below you might want to try this.

The solution that worked for me was to use the -c option on the cluster::configure so the command becomes something like this.

:~$ mongrel_rails cluster::configure -p 8001 -a 127.0.0.1 -N 3 -c /home/bjarki/rails/www

this should add the cwd line to config/mongrel_cluster.yml

Ben C. 6/10/2007

On my Ubuntu 7.04 Desktop setup, I needed to add "export PATH=$PATH:/var/lib/gems/1.8/bin" to my .bashrc to get the gem-install scripts in my path. After that, everything should work.

-- Ben C.

battery 6/25/2008

[...]The offline scripting tool might add some relief here, but it's still sort of a pain. I've had to separate "build" from "configuration" steps.[...]

Add your comment