Debian Rails Stack - NginX and Mongrel
Debian Rails Stack - NginX and Mongrel

If you are reading this it is because you already know that you want a Rails setup on a Debian server with NginX in front of a Mongrel Cluster.

These are the steps I used to completely set up my server…

The machine I started with had an existing Debian install and also had Apache2 and MySQL installed.

Note: I am assuming you are working as root on your machine. If not you will have to do sudo in front of all of the following commands.

Make sure that your /etc/apt/sources.list is up to date and then update your repositories and installed packages.

$ apt-get update  
$ apt-get upgrade

Remove all Apache instances

$ dpkg --purge apache apache2

Install Ruby and its friends

$ apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8

I am also using SSL, so I need to install the ruby ssl libraries.

$ apt-get install libopenssl-ruby libopenssl-ruby1.8

Setup symlinks for Ruby and its friends

$ ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby
$ ln -s /usr/bin/ri1.8 /usr/local/bin/ri
$ ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc
$ ln -s /usr/bin/irb1.8 /usr/local/bin/irb

Install GCC, G++ and Make so you can compile code

$ apt-get install gcc g++ make

Get the most recent version of RubyGems (check for current version)

$ wget
$ tar xvzf rubygems*
$ cd rubygems* 
$ ruby setup.rb

I cleaned up the install files after the install

$ cd ..
$ rm -rf rubygems-1.2.0 rubygems-1.2.0.tgz

Set up a symlink for the Gem command

$ ln -s /usr/bin/gem1.8 /usr/local/bin/gem

Now we need to install Rails in all its glory

$ gem install rails --include-dependencies

I use Git for source code management, so I need to install it. I also use submodules and the code in Debian’s repository does not support it, so I need to install it from source.

In order to make sure that Git will actually make, we have to do a little setup work.

$ apt-get install tcl8.4 tk8.4

Now lets get the latest version of Git and install it

$ cd ~/
$ wget
$ tar xzf git*
$ cd git*
$ ./configure
$ make
$ make install

I cleaned up the install files after the install

$ cd ..
$ rm -rf git- git-

I also want to make sure that I have all of the dependencies

$ apt-get build-dep git-core

I also wanted to have image libraries at my finger tips when I wanted them, so I installed ImageMagick and RMagick

$ apt-get install imagemagick librmagick-ruby1.8 librmagick-ruby-doc libfreetype6-dev xml-core

Next we are going to install Mongrel

$ gem install mongrel

And then Mongrel Cluster

$ gem install mongrel_cluster

Get the machine ready for the NginX install

$ apt-get install zlib1g-dev libgcrypt11-dev libpcre3-dev libssl-dev

Install NginX and friends

$ apt-get install libpcre3 nginx libfcgi-dev libfcgi-ruby1.8

It is worth noting here that I am using Virtuozzo. At this point I cloned my machine and will be doing the Mongrel and Rails specific configuration on my cloned machine for easy Rails deployment.

I now need to get my Rails source code on my machine. Since I am using github, I need to set up my server and github so they can talk to each other. This is done through an ssh key. The ssh keys reside in ~/.ssh/. If you do not have that directory, simply ssh from that machine into another machine and when it asks, add the machine as a know_hosts and it will create the directory for you.

$ cd ~/.ssh/
$ ls

If you already have and id_rsa in that directory, skip this step, otherwise create that key.

$ ssh-keygen -t rsa

Follow the instructions on screen (,, ``).

Now log into github and under ‘account’, copy in the result of the following command into the Public Keys section (make sure not to have any new line characters when you copy and paste).

$ cat

Now that my server and github can talk to each other, I am going to create a directory for my projects and move into it.

$ mkdir ~/projects
$ cd ~/projects/

Now that I have github setup with my machine I need to grab my code.

$ git clone

Configure your Rails app now. Stuff like setting up your database and configuring Rails to work with them… (config/database.yml, etc…)
Note: Since you are using Debian, you may have to specify the socket in config/database.yml to be: socket: /var/run/mysqld/mysqld.sock

Now that my Rails app is configured, I need to configure the server environment. First I am going to configure Mongrel Cluster.

$ cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/mongrel_cluster  
$ vi /etc/init.d/mongrel_cluster

Add the following just above the CONF_DIR variable.


I also changed the user to:


Set up Mongrel Cluster to start at startup.

$ chmod +x /etc/init.d/mongrel_cluster
$ update-rc.d mongrel_cluster defaults

Now I need to set up the Mongrel Cluster correctly.

$ mongrel_rails cluster::configure -e production -p 8000 -N 3 -c ~/projects/myproject -a --user www-data --group www-data

It should create a config file that looks like this:

cwd: /root/projects/myproject
port: 8000
environment: production
group: www-data
user: www-data
pid_file: log/
servers: 3

Putting the config for Mongrel Cluster in /etc like the rest of the config files.

$ mkdir /etc/mongrel_cluster
$ cd /etc/mongrel_cluster/
$ ln -s ~/projects/myproject/config/mongrel_cluster.yml

Now the last thing we need to do is configure NginX. I have attached a sample config file that handles one rails app and phpmyadmin. Back up your current config (/etc/nginx/nginx.conf) file before you mess with it.

Make sure that you change the ownership of your Rails app to www-data so it will run.

$ chown -R www-data:www-data ~/projects/myproject

As a final set to make sure that everything is working, you need to restart everything.

$ /etc/init.d/mongrel_cluster stop
$ /etc/init.d/mongrel_cluster start
$ /etc/init.d/nginx stop
$ /etc/init.d/nginx start

On my machine I was having problems with locales being wrong. Basically I was getting this problem every time I did apt-get install.

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
 LANGUAGE = (unset),
 LC_ALL = (unset),
 LANG = "en_CA.UTF-8"
 are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

In order to solve this on my machine I did the following and then followed the on screen instructions.

$ dpkg-reconfigure locales

My system already had MySQL installed, but in my production.log file I was getting warnings that I was using the built in Rails MySQL something-or-other and that it was not safe to be used in production. They wanted you to install the MySQL C Ruby Bindings. This is how I was able to do it (ignore the NginX error it gives at the end and just restart NginX after).

$ apt-get install libmysqlclient15-dev
$ gem install mysql -- --with-mysql-config=/usr/bin/mysql_config


Attachement: nginx.conf 2.69 KB