FastCGI Ruby on Rails with Lighttpd on Ubuntu
January 18th, 2007
Here’s my take on how to install a Ruby on Rails app with lighttpd. The app is configured to start on boot with an init.d script that runs the spinner as any system user.
Warning added 09 Jan. 2008. This info is a bit dated. I think fastcgi has fallen from grace in favor of http with mongrel or one of it’s derivatives. I currently use apache2 and mod-proxy with mongrel clusters.
This article assumes your Ruby on Rails app works great with script/server and that you have a working lighttpd installation. There are a growing number of tutorials on the web describing the initial installation of Ruby on Rails and lighttpd so we’re not going to cover those.
We will be configuring lighttpd to communicate with your app on some ports, then installing a process control script in /etc/init.d that can initialize and tear down your app. When you’re finished you will be able to sudo /etc/init.d/myapp {start|stop|restart}. Your app will also automatically start on boot and terminate gracefully on shutdown. Let’s assume an application called myapp is to be run by the system user crazyhorse for this example.
Here is a summary of the scripts you should already have as part of your rails installation.
script/process/spawner (starts instances of your app)
script/process/spinner (will try to run spawner periodically in case a process dies)
script/process/reaper (can restart and terminate the app)
You can use the -h option to learn more about these scripts but the defaults are very reasonable. Next we will create a script in /etc/init.d to run the rails process control scripts, then set up filesystem links in the runlevel directories so your app starts and stops automatically on boot and shutdown.
Paste the following code into a file named /etc/init.d/myapp, replacing myapp and crazyhorse as required.
#! /bin/sh
# Start and stop myapp's ruby on rails dispatchers
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SCRIPTNAME="myapp"
RAILS_ROOT="/home/crazyhorse/myapp"
SPAWN_OPTS="-a127.0.0.1 -p8000 -i3 -eproduction"
USER="crazyhorse"
case "$1" in
start)
su $USER -c $RAILS_ROOT/script/process/spinner -c "$RAILS_ROOT/script/process/spawner $SPAWN_OPTS"
;;
stop)
su $USER -c "$RAILS_ROOT/script/process/reaper -a graceful"
;;
restart)
su $USER -c "$RAILS_ROOT/script/process/reaper -a restart"
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 3
;;
esac
exit 0
Now permit your script to be executed with chmod and use update-rc.d to install the filesystem links.
# sudo chmod 755 /etc/init.d/myapp
# sudo update-rc.d myapp defaults
You should now be able to start and stop your app like any other installed daemon! Try it and make sure it works.
# sudo /etc/init.d/myapp start
# ps uax | grep myapp
# sudo /etc/init.d/myapp restart
# sudo /etc/init.d/myapp stop
Add something like the following to your lighttpd.conf. Notice that lighttpd is instructed to listen for fastcgi on localhost, ports 8000 through 8002. The accesslog directive tells lighttpd to keep a myapp specific accesslog.
$HTTP["host"] =~ "(^|\.)myapp\.com" {
server.document-root = "/home/crazyhorse/current/public"
server.error-handler-404 = "/dispatch.fcgi"
server.indexfiles = ("dispatch.fcgi")
url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )
fastcgi.server += (
".fcgi" => (
"localhost-8000" => ( "host" => "127.0.0.1", "port" => 8000 ) ,
"localhost-8001" => ( "host" => "127.0.0.1", "port" => 8001 ) ,
"localhost-8002" => ( "host" => "127.0.0.1", "port" => 8002 ) )
)
accesslog.filename = "/home/crazyhorse/lighttpd/access.log"
}
Check the lighttpd config file for errors.
# lighttpd -t -f /etc/lighttpd/lighttpd.conf
Create the lighttpd log file directory and give ownership to the web daemon user so it has write access.
# mkdir /home/crazyhorse/lighttpd
# sudo chown www-data:www-data /home/crazyhorse/lighttpd
Add the web daemon user to crazyhorse’s group. This will allow read-only access to the web daemon so long as appropriate group permissions are set in the /home/crazyhorse/myapp/public directory. It is important that the web daemon (in this case www-data) be allowed to read the public dir for page caching, css, javascript, error pages, and other assets to work.
# sudo gpasswd -a www-data crazyhorse
# groups www-data
Restart lighttpd and then start the rails app.
# sudo /etc/init.d/lighttpd restart
# sudo /etc/init.d/myapp start
Make sure port 80 is still up.
# netstat -tulpn | grep :80
You should now be up and running.
* Update 5/1/2007 * Rails will start with an http, rather than fcgi, interface via mongrel if mongrel is installed. To use the fcgi interface with mongrel installed you can make the SPAWN_OPTS line in the init.d script look like this.
SPAWN_OPTS="fcgi -a127.0.0.1 -p8000 -i3 -eproduction"
on December 27th, 2007 at 02:04 PM
Probably this is a version thing, but there’s no script/process/spinner in my app. I’m on rails 1.2.3. So I had to change: su $USER -c $RAILS_ROOT/script/process/spinner -c ”$RAILS_ROOT/script/process/spawner $SPAWN_OPTS”
to
and then it worked fine. There’s also a script/process/inspector, so I suspect I could add:
To add a status command.
on December 27th, 2007 at 09:28 PM
This is old so yes, it is a version thing. I’ll add a warning or two. Thank you.