Instructions for a production installation of Ruby on Rails on Ubuntu 8.04 Hardy Heron
C Emacs Lisp
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
monit
nginx-upstream-fair Added nginx-upstream-fair source code Aug 8, 2008
nginx
.bashrc
.emacs
README
fw
my.cnf

README

###################################################################
#
# INTRODUCTION
#
###################################################################

What you are looking at is detailed instructions for setting up a Ruby on Rails production environment on Ubuntu 8.04 Hardy Heron. The instructions have primarily been tested on a Gold VPS at the swedish hosting company GleSYS.se (see http://glesys.se/serverhotell/vps). I would like to take this opportunity to thank the kind folks at GleSYS for supporting me in this project and for providing me with excellent hosting. The instructions are generic and should work on any clean Ubuntu 8.04 install. They have been tested successfully at http://slicehost.com as well.

The instructions assume familiarity with tools such as Linux, ssh, Ruby on Rails, Capistrano, Mongrel, and MySQL. They draw heavily on the excellent book "Deploying Rails Applications" by Ezra Zygmuntowicz. I strongly encourage you to check out the book if you haven't already. Like with all Pragmatic Bookshelf books, it can be conveniently downloaded as a PDF.

As of August the 10:th 2008, the instructions install the following versions of the technology stack:

mysql 5.0.51a
nginx 0.6.31 (compiled with fair proxy balancer)
monit 4.8.1
ruby 1.8.6, patchlevel 111
gem 1.2.0

Make sure you fully understand the instructions as you go through them step by step. Also, in the end, don't forget to customize the config files to fit your needs, especially the Nginx, Monit, and Mongrel Cluster ones.

If you find mistakes or have suggestions for improvement, please get in touch with me at peter@marklunds.com.

Peter Marklund
http://marklunds.com

Friday, August 10, 2008
Stockholm

###################################################################
#
# BASIC SERVER SETUP
#
###################################################################

############################
# Set Root Password (if you haven't already)
############################

ssh root@<server-ip-number>
passwd

############################
# Install Editor (Optional)
############################

apt-get install emacs22
export EDITOR=emacs

############################
# Add the Rails Deploy User
############################

adduser deploy
# Give deploy sudo access
adduser deploy sudo
# Uncomment sudo group line
visudo

############################
# Setup ssh keys
############################

su deploy
# Press enter three times
ssh-keygen
exit
exit

# Locally - add your server to .ssh/config
Host = <name-of-your-server>
Hostname = <server-ip-number-or-domain>
User = deploy

# Convenient function for adding ssh keys, you can add it to your ~/.bashrc
function authme {
    ssh $1 'cat >>.ssh/authorized_keys' <~/.ssh/id_rsa.pub
}
source ~/.bashrc

authme <name-of-your-server>
# You should now be able to login without password
ssh <name-of-your-server>

############################
# Upgrade packages
############################

# Check out which apt-get sources you are dealing with
sudo emacs /etc/apt/sources.list
# You might want to add hardy-security so that the file becomes:
# deb http://archive.ubuntu.com/ubuntu/ hardy main restricted universe
# deb http://archive.ubuntu.com/ubuntu/ hardy-updates main restricted universe
# deb http://security.ubuntu.com/ubuntu hardy-security main restricted universe

# Update
sudo apt-get update
sudo apt-get upgrade

############################
# Install essential tools
############################

sudo apt-get install build-essential man

############################
# Install Git and Subversion
############################

# We include Subversion since not everyone uses Git, yet, you know
sudo apt-get install git-core subversion subversion-tools

############################
# Setup deploy User Environment
############################

cd
git clone git://github.com/peter/rails-on-ubuntu.git
mv .bashrc .bashrc.orig
cp rails-on-ubuntu/.bashrc .
source ~/.bashrc
cp rails-on-ubuntu/.emacs .

############################
# Set the Timezone and Keep the Time Synced
############################

sudo dpkg-reconfigure tzdata
# Check time is correct
date
sudo apt-get install ntp

############################
# Set the hostname
############################

sudo hostname your-hostname
sudo emacs /etc/hosts
sudo emacs /etc/hostname
# Check hostname is correct
hostname

############################
# Change ssh to run on non-standard port
############################

# Choose some unused port number such as 7243 (not 80 or 443) between 1024 and below 65537
# Make sure to make a note of the number.
sudo emacs /etc/ssh/sshd_config
sudo /etc/init.d/ssh restart
exit
# Add to the section for your server in ~/.ssh/config:
# Port = <your-port-number>
ssh <name-of-your-server>

############################
# Configure a firewall that only allows incoming http, https, and ssh traffic
# (remembering that ssh port was changed)
############################

sudo apt-get install iptables
sudo mkdir /usr/local/scripts
sudo cp ~/rails-on-ubuntu/fw /usr/local/scripts
# Edit the firewall config and change <your-ssh-port> to your port number
sudo emacs /usr/local/scripts/fw
sudo /sbin/iptables-restore < /usr/local/scripts/fw
# Test the firewall
exit
# Should not be able to connect to default port
telnet <ip-of-your-server> 22
# Should be able to connect this time
telnet <ip-of-your-server> <your-ssh-port>
# Note: if you have locked yourself out of your server at GleSYS#
# you can reset the firewall in the web admin panel.
ssh <name-of-your-server>

###################################################################
#
# RUBY ON RAILS STACK
#
###################################################################

############################
# Ruby
############################

# Install Ruby
sudo apt-get install ruby-full
# Check that Ruby is there
which ruby
#=> /usr/bin/ruby
ruby -v
#=> ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
ruby -ropenssl -rzlib -rreadline -e "puts :success"
#=> success

############################
# RubyGems
############################

# Install RubyGems
cd /usr/local/src
# Get latest stable recommended release of RubyGems from rubygems.org
sudo wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
sudo tar xzf rubygems-1.2.0.tgz
cd rubygems-1.2.0
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
which gem
#=> /usr/bin/gem
gem --version
#=> 1.2.0

############################
# MySQL
############################

# Install MySQL
sudo aptitude install mysql-server mysql-client libmysqlclient15-dev
# Check mysql is there
which mysql
#=> /usr/bin/mysql
mysql --version
#=> mysql  Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (i486) using readline 5.2
mysql
show databases;
exit

# Install MySQL C Driver for Ruby
sudo gem install mysql
# NOTE: if you have issues compiling the MySQL driver related to make, try:
# sudo apt-get install make

############################
# Ruby Gems that we Need
############################

# Install a few gems used in Rails deployment
sudo gem install --no-rdoc --no-ri rake rails capistrano mongrel_cluster

############################
# Nginx
############################

# Install Nginx with Fair Proxy Balancer
sudo apt-get install libpcre3-dev libpcre3 openssl libssl-dev
cd /usr/local/src
sudo wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
sudo tar xzvf nginx-0.6.31.tar.gz
sudo cp -r ~/rails-on-ubuntu/nginx-upstream-fair .
# If you want to live on the edge and get the latest nginx-upstream-fair, then use:
# sudo git clone git://github.com/gnosek/nginx-upstream-fair.git
cd nginx-0.6.31
sudo ./configure --with-http_ssl_module --add-module=/usr/local/src/nginx-upstream-fair
sudo make
sudo make install
which nginx
#=> /usr/local/nginx/sbin/nginx

# Configure Nginx and start it
sudo cp ~/rails-on-ubuntu/nginx/nginx.conf /usr/local/nginx/conf
sudo cp -r ~/rails-on-ubuntu/nginx/vhosts /usr/local/nginx/conf
sudo cp ~/rails-on-ubuntu/nginx/nginx.init /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults
sudo /etc/init.d/nginx start

############################
# Sending SMTP from localhost
############################

# Install Postfix so we can send SMTP from localhost.
# You can get away with accepting the defaults during install.
sudo apt-get install postfix

############################
# Monit
############################

# Monit - install, configure, and start
sudo apt-get install monit
sudo cp ~/rails-on-ubuntu/monit/monitrc /etc/monit
sudo chmod 700 /etc/monit/monitrc
sudo cp -r ~/rails-on-ubuntu/monit/monit.d /etc
# Replace <your-email> with your email address
sudo emacs /etc/monit/monitrc
# Replace my_app with the name of your app
sudo emacs /etc/monit.d/mongrel.monitrc 
# Review the files under /etc/monit.d and make sure you understand the settings and that
# they are appropriate for your server.
# Change to startup=1
sudo emacs /etc/default/monit
sudo /etc/init.d/monit start
sudo monit status

###################################################################
#
# DEPLOYMENT
#
###################################################################

############################
# Deployment Setup
############################

# Setup directory where your Rails app will be deployed to on the server
sudo mkdir -p /var/www/apps
sudo chown deploy.deploy /var/www/apps

# Setup the Capistrano file structure from your local machine
exit
cd <path-to-your-local-rails-app>
# If you haven't capified your up, run "capify ."
# If you don't have Capistrano: sudo gem install capistrano
# There is an example app here: http://svn.marklunds.com/my_app/
# Edit config/deploy.rb, for an example, see:
# http://svn.marklunds.com/my_app/config/deploy.rb
cap deploy:setup
# Capistrano sets up the files as root, however later we need to access them as the deploy
# user. Change ownership to the deploy user:
cap invoke COMMAND="sudo chown -R deploy.deploy /var/www/apps/my_app"

############################
# Setup the database
############################

# Configure MySQL and create databases
ssh <name-of-your-server>
sudo /etc/init.d/mysql stop
sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
sudo cp ~/rails-on-ubuntu/my.cnf /etc/mysql
sudo /etc/init.d/mysql start
mysqladmin -u root -p<mysql-password> create my_app_production

############################
# Deploy
############################

# Deploy cold from your local machine
exit
cd <path-to-your-local-rails-app>
# Add a file to your scm at config/mongrel_cluster.yml, see:
# http://svn.marklunds.com/my_app/config/mongrel_cluster.yml
cap deploy:cold

Hit http://<your-server-ip-or-domain> in your browser.

Good luck!