Ubuntu with Nginx + Ruby 1.9.3 + PostgreSQL 9.1 + Passenger + Capistrano

Here we have a recipe to install and configure a production server to Rails or Rack applications.

As you know, I participated of Rails Rumble on this year and a I created a production server for our project on Linode server. That is cool, but I never had created a great server for real production application, so, I created a recipe to do the production server.

See below:

Update the system

sudo apt-get update  

Install some dependencies

sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libcurl4-openssl-dev curl git-core python-software-properties libsqlite3-0 libsqlite3-dev sqlite3  

Install the Ruby 1.9.3

cd /tmp/    
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p194.tar.gz
tar -xvzf ruby-1.9.3-p194.tar.gz
cd ruby-1.9.3-p194/
./configure
make
sudo make install
echo "gem: --no-ri --no-rdoc" >> ~/.gemrc
sudo gem install bundler

Install the PostgreSQL 9.1

sudo apt-get install postgresql-9.1 postgresql-server-dev-9.1
gem install pg

Add an user for deploy in your postgres

sudo -u postgres createuser some_user
cd /etc/init.d/
sudo -u postgres psql template1
template1=# ALTER USER some_user WITH PASSWORD 'some_password';

Now you need edit this file: vim /etc/postgresql/9.1/main/pg_hba.conf

Where is the 'postgres' your replace by some_user, will be something like that:

# Database administrative login by Unix domain socket
local   all             some_user           pear

And restart your postgres

sudo service postgresql restart

Install the Memcached

sudo apt-get install memcached libmemcached-dev
gem install dalli

Install the Imagemagick

sudo apt-get install imagemagick libmagickwand-dev
gem install rmagick mini_magick

Install the Phusion Passenger

gem install passenger
sudo chown -R `whoami` /opt
passenger-install-nginx-module --auto-download --auto

Create a user for deploy

sudo adduser deploy

Nginx

vim /opt/nginx/conf/nginx.conf

server {
        listen 80;
        server_name www.example.com;
        location / {
                root /home/deploy/www/application_name/current/public;
                passenger_enabled on;
                rails_env production;
        }
}

Nginx as a service

wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
sudo mv init-deb.sh /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults

Now you can do something like that:

sudo service nginx start
sudo service nginx stop
sudo service nginx restart

Capistrano

In your local app add gem 'capistrano' in your Gemfile

In your application dirictory:

bundle install
capify .

This will create the files Capfile and config/deploy.rb where settings are stored deploy, as well as tasks.
Replace this files by gist:3868139 and edit with your data.

Create the database.yml for your server configuration in:

/home/deploy/database.yml

Still in your local app and run this:

cap deploy:setup

This will create the directories in your server.

Now you can make deploy of your application:

cap deploy