Booting nginx and mongrel_cluster with init scripts

Once you get your Rails app up you want it to stay up, even if the power goes out and forces a reboot at your hosting facility. This means getting some startup scripts into /etc/init.d for mongrel and nginx.

I'm using Ubuntu Dapper for these examples. Any Debian based distro should be nearly the same.

For nginx, I used the /etc/init.d/skeleton file as a base. My nginx is installed at /usr/local/nginx. You can see my modified skeleton for nginx. I uncommented the section for reload as nginx does respond to SIGHUP by reloading its config file.

Once you have the file edited to reflect your path particulars, install it by typing # update-rc.d nginx defaults from within the /etc/init.d directory.

Setting up mongrel_cluster is as easy as following Zed's directions. This amounted to the following for my particular setup:

Create the mongrel_cluster folder that will contain cluster config files # mkdir /etc/mongrel_cluster Make a symlink from the mongrel_cluster folder to your particular mongrel_cluster.yml file. My html root has subfolders for all the sites I host. Let's call this one It's managed using Capistrano, so the current part of the path is really another symlink to the current version of my app. Your path might be more simple. # cd /usr/local/nginx

ln -s html/ /etc/mongrel_cluster/mysite.yml

Now cd over to where your gems are. I'm breaking these commands down so that they fit width-wise. Copy the startup script that mongrel_cluster ships with over to /etc/init.d # cd /usr/lib/ruby/gems/1.8/gems

cp mongrel_cluster-0.2.1/resources/mongrel_cluster /etc/init.d

chmod +x /etc/init.d/mongrel_cluster

Finally, register the startup script with the system using the update-rc.d utility # update-rc.d mongrel_cluster defaults

One final thing I had to do, and that Zed's doc mentions at the end, is modify mongrel_cluster_ctl to reference my ruby binary. For some reason ruby isn't found using the #!/usr/bin/env ruby method. Simply edit /usr/bin/mongrel_cluster_ctl and directly reference ruby at the top. #!/usr/local/bin/ruby

Having init scripts makes starting and stopping your server processes really easy. For example, you could restart nginx by typing: # /etc/init.d/nginx restart