I've had some difficulty pulling together disparate pieces of information from questionable sources across the internet on the best way to go about building a Ruby on Rails stack on top of Debian 3.1 (Sarge). This operating system is widely considered one of the best for web servers. After banging my head against the wall for a while, I decided I'd just start from scratch and build this stack from the ground up.
Web Host and Operating System
Choosing the right host can be difficult. Shared Hosting has for the most part gone by the wayside and virtualization-based (VPS) hosting has become a cost-competitive replacement with plenty of added benefits. RimuHosting offers great VPS options with many OSes to choose from.
Through my experience with various flavors of Linux, Debian based systems have proven to be solid and reliable. Debian 3.1 (Sarge) is my preferred choice of OS for web application server stacks for its proven robustness and security. Whether you go with RimuHosting or not, the rest of this post assumes you're using Debian 3.1.
Virtualization at home
One thing that can be helpful in putting together a server stack, is having a sandbox to play around in. With the multitude of options available in the virtualization space today, it's becoming easier and easier to run any number of virtual machines on a single physical computer. Since I'm on a Mac, I run Parallels Desktop and mess with a local virtual machine before ever touching my production system.
Starting from a Debian 3.1 (Sarge) base install
At this point, I will assume you have a Debian 3.1 (Sarge) base system installed and you are comfortable installing software from source code. The first thing we need to do is add some source locations for Debian. Open the file @/etc/apt/sources.list@ in your favorite editor and replace the contents with the following:
deb http://http.us.debian.org/debian sarge main contrib non-free
deb http://non-us.debian.org/debian-non-US sarge/non-US main contrib non-free
deb http://security.debian.org sarge/updates main contrib non-free
Save the file and run the following commands (after calling su, you'll need to enter your root password):
su
apt-get update
apt-get upgrade
It's a good idea to install @sudo@ so that you don't have to always be running around your system as the root user. Bad things can happen when you're root and you type in an unintentional command. Having @sudo@ means you only use the power of root on a per-command bases, i.e. less possibility of error. Here's what to do:
su
apt-get install sudo
Now, open up @/etc/sudoers@ and add yourself like so:
your_username ALL=(ALL) ALL
Save the file and you are now a sudoer.
Your Debian system should now be ready to handle the forthcoming stack.
Nginx front-end web server
For a front-end web server, I've chosen to use nginx. Despite its relative obscurity, it has proven to be a lightweight, high performance replacement for something like Apache. You can however run whatever front-end web server you like.
Dependencies
We'll be downloading/compiling/installing three libraries (pcre, zlib, and openssl) before we install nginx:
apt-get install zlib1g-dev libgcrypt11-dev libpcre3-dev libssl-dev
Installation
Now it's time to install nginx. I will be installing from source, so you'll need to be comfortable with this. If there is a newer version of this source code (since this writing), feel free to get that instead. Here we go:
wget http://sysoev.ru/nginx/nginx-0.5.30.tar.gz
tar zxvf nginx-0.5.30.tar.gz
cd nginx-0.5.30
./configure --pid-path=/usr/local/nginx/logs/nginx.pid --sbin-path=/usr/local/sbin/nginx --with-md5=/usr/lib --with-sha1=/usr/lib --with-http_ssl_module --with-http_dav_module
make
sudo make install
Startup/Shutdown script
We'll need to add the startup/shutdown script for nginx:
wget http://notrocketsurgery.com/files/nginx -O /etc/init.d/nginx
sudo chmod 750 /etc/init.d/nginx
And let's make sure nginx is started upon a system reboot:
update-rc.d nginx defaults
Ruby, RubyGems, Rails, and Mongrel
Ruby 1.8.6
It's time to install ruby. I chose do do this from source so that you'll have the latest version 1.8.6. Here we go:
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.gz
tar xzvf ruby-1.8.6.tar.gz
cd ruby-1.8.6
./configure
make
sudo make install
RubyGems
Next we'll need a Ruby package manager called RubyGems. This will be our gateway to installing Ruby on Rails as well as a whole plethora of other libraries. I've chosen to install this from source for the same reason as Ruby, here's how:
wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
tar xzvf rubygems-0.9.4.tgz
cd rubygems-0.9.4
sudo ruby setup.rb
sudo gem update --system
Ruby on Rails
Last but certainly not least, time to install Ruby on Rails. It's pretty simple too:
sudo gem install rails --include-dependencies
Mongrel
Mongrel will be our Application Server(s). The way it works is, you'll start up a cluster of Mongrel instances (bootstrapping your Rails app). Nginx will then proxy requests to one of these instances (once we've configured everything). This is the last piece to a great stack upon which we can place our rails web applications:
sudo gem install mongrel mongrel_cluster --include-dependencies
Mongrel Startup/Shutdown script
Just as we've done with nginx above, we need a script that will handle starting and stopping our mongrel_cluster:
sudo ln -s /usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-0.1.3/resources/mongrel_cluster /etc/init.d/mongrel_cluster
Let's also make sure our clusters are started after a system reboot:
sudo /usr/sbin/update-rc.d mongrel_cluster defaults
To be continued...
This concludes Part 1. Very soon, I will complete Part 2, which will guide you through installing Capistrano for deployment as well as Mongrel, Capistrano and nginx configuration. Cheers!