Running Ramaze with Unicorn, Nginx and God

I’ve been meaning to write a post about running a Ramaze website under a reasonable setup for a long time now so I’m happy to finally get to it. I’m going to focus my attention on the configuration of the setup vs the installation of the software - RACKRamazeUnicorn and God are ruby gems and there are plenty of good articles about installing Nginx out there already.

What You Need

I’ve included the version number of the software I’ve tested this setup with but I suspect this configuration will work with newer versions as well (and probably older versions too).

  1. RACK (web server interface)
    • 1.0.1
  2. Unicorn (http server)
    • 0.95.2
  3. Nginx (front end reverse proxy server)
    • 0.6.36
  4. God (process monitoring)
    • 0.7.13
  5. Ramaze (web framework)
    • 2009-05

Some Assumptions

While the config files below make the following assumptions you should be able to handle any discrepancies by altering the paths or changing the run user.

  1. Nginx is configured to read configuration files from its conf/sites-enabled directory
    • Add include <path to nginx install>/conf/sites-enabled/*; to the http section of your primary Nginx config file to enable this
  2. Your website will run under the www-data user
  3. The NginxUnicorn and God config files exist within the config directory of your Ramaze website
  4. The RACK config file (config.ru) is in the root of your website
  5. You’ve created the log and tmp/pids directories underneath the root of your website

Configuring the Pieces

1. RACK

This config file is actually a very simple ruby file, its main purpose in this situation is to load your website’s app.rb via the require statement. This in turn loads the rest of your site.

3. Unicorn

While I’ve commented the major parts of the configuration file inline, I highly recommend checking out the Unicorn website and this sample configuration file for more details/features.

2. Nginx

You can find more info about configuring Nginx here. If assumptions #2 & #4 are true then you’ll need to create a symlink in the <path-to-nginx-install>/conf/sites-enabled/* directory to your Nginx config in the config directory under the root of your website. You can do this by executing

ln -s /var/www/apps/my.domain.com/config/nginx.conf <path-to-nginx-install>/conf/sites-enabled/my.domain.com

4. God

As with the RACK and Unicorn config files, this one is also written in Ruby. The God home page has a much more detailed breakdown of the config file and is definitely worth a read.

Flicking the Switch

Once you have your configuration files in place and your website on your target server you’re going to need to turn it all on. The first step is to tell God to keep an eye on your Unicorn process and the second step is to cycle Nginx forcing it to pickup your new website’s config file.

Alerting God

The first thing to do is start God and have it monitor your master Unicorn process for your new website. If God is not already running you can start it using:

god --log-level=debug

To have it start monitoring your Unicorn process, execute the following:

sudo god load /var/www/apps/my.domain.com/config/god.conf

Cycling Nginx

Cycling Nginx is something that cries out for a script so here is a simple one:

Dealing with Errors

After flicking the switch for the first time under this setup I’ve run into a 502 bad gateway error multiple times. Each time it was because I had forgotten to create the required tmp/pids or log directories under the website root. If you do run into a 502 its probably because Unicorn had trouble starting up or Nginx is not pointing to the right spot.

Debugging

Check the God logs for your website by executing:

god --log-level debug log my.domain.com

That will tell you if God has managed to start the Unicorn process successfully. If it has then checkout the Unicorn error logs in the log directory of your website. If it cannot start the process and doesn’t indicate why then try executing the Unicorn startup command in a shell directly (that should at least give you the error message Unicorn is generating).

pixelstats trackingpixel
blog comments powered by Disqus