Month: May 2011

Ubuntu 10.04 with Nginx, Ruby and PHP/PHP-FPM installation: errors and fixes.

A few days ago, I signed up for a Linode instance with Ubuntu 10.04 installed on it. The plan is to gradually move all my websites, including this blog to the new instance. I gave NGiNX a shot as my web server after reading a few articles on how it is light and fast, compared to Apache. Here’s what happened.

1. Don’t lock yourself out of your own server:
The server needs to run NGiNX with Ruby on Rails, MySQL, and PHP. I found Chris Kemson’s guide to be the best and started following it.
The installation was fairly smooth, except for one hilarious mistake on my part: I followed the instructions too closely. During the step for installing UFW (Uncomplicated Firewall), Chris asks us to first enable the firewall, and then turn on support for ssh and http. So I went along and turned on the firewall, only to find myself logged out of the ssh session and no way to go back in. First, I laughed pretty hard at myself, and then logged into the Linode control panel to see if there was anything I could do. Thankfully, Linode provides a web-console, which let me log in and allow ssh access through the firewall.

2. Phusion Passenger only speaks in one Ruby at a time
Something I wanted to support was being able to run both Rails 2 and 3. A lot of people seem to recommend installing RVM for this. RVM lets us run multiple versions of ruby on the same system, and switch between them as needed. This means that it is possible to setup Rails 2 under Ruby 1.8.7 and Rails 3 under Ruby 1.9.2. So far so good. The problem is in deployment. You see, Phusion Passenger does not allow us to run multiple versions of Ruby. The line
export PATH=/opt/myruby/bin:$PATH is an http level statement in nginx.conf, and specifying a different ruby for each server simply generates an error. I eventually settled with removing RVM, and running Rails 2 and 3, installed under the same Ruby version (1.9.2).

3. Errors with hard to find solutions
Here are a few NGiNX configuration related errors that haunted me:

  • 403: Forbidden / 500: Internal Server error / Completely blank index page.
  • Going to index.php (the root url) initiates a download of the index.php file

The following fixes seem to work for the above cases:

  • I like to serve my website out of each user’s public_html directory. Make sure that the public_html directory has the proper permissions to allow NGiNX to access it.
  • Make sure that the index setting specifies index.php:
    index index.html index.htm index.php;
  • Make sure that your server has the following lines:
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
        }
    
  • If you are deploying a rails website, then set the root in your site’s nginx configuration to that Rails project’s public folder, and not your website’s base public_html folder

Here’s what my NGiNX configuration looks like:

worker_processes  1;
events {
    worker_connections  1024;
} 
http {
    passenger_root /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.7;
    passenger_ruby /usr/local/bin/ruby;
    rails_env production;
    include       mime.types;
    default_type  application/octet-stream;
    index index.html index.htm index.php;
    sendfile        on;
    keepalive_timeout  65;
    server {      
        listen       80;
        server_name  localhost;
        location / {
            root   html;
        }
    }
    include sites-enabled/*;
}

And my site-specific configuration, for jaripeo.com, which runs on Rails 2:

server {
    access_log  /home/jaripeo/access.log;
    error_log  /home/jaripeo/error.log;
    listen  80;
    server_name  jaripeo.com *.jaripeo.com;
    root /home/jaripeo/rails/current/public;

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi_params;
    }
    passenger_enabled on;
}