Hi all as I worked on setting up 4 to 5 servers, I thought its better to document the stuff so that I/developers can refer it, So I am documenting the step-by-step process for setting up the slicehost server for rails application work with nginx as a web server and mongrel as a rails application server.

Update

To update your system:

yum update

MySQL

to install mysql client and server apps

yum install mysql-server

The client package “mysql” will automatically be installed as a dependency.

Apache

yum install httpd

PHP5

To install php with soap, xml and mysql plugins:

yum install php php-soap php-xml php-mysql

some other php plugin utilities that are common:

yum install php-mbstring php-gd

Ruby

yum install ruby

Utilities

other useful utilities:

yum install nano wget elinks subversion vi

Nginx

install nginx web server:

yum install nginx

Mongrel Cluster

install mongrel_cluster:

gem install mongrel_cluster –include-dependencies

Setup your nginx configuration for your rails application and mongrel_cluster (proxy).

Below you found sample config file for nginx.conf (normally located in “/etc/nginx/conf/nginx.conf”)

user  deploy;
worker_processes  1;
error_log   logs/error.log debug;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
  include        conf/mime.types;
  default_type   application/octet-stream;
  sendfile        on;
  #tcp_nopush     on;
  keepalive_timeout  65;
  tcp_nodelay        on;
  gzip  on;
  gzip_min_length  1100;
  gzip_buffers     4 8k;
  gzip_types       text/plain;
  upstream mongrel {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
  }
  server {
    listen       80;
    server_name  example.com;
    root /var/www/apps/example/current/public;
    index  index.html index.htm;
    location / {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }
      if (!-f $request_filename) {
        proxy_pass http://mongrel;
        break;
      }
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
  }
}

Restart the nginx server:

/etc/init.d/nginx restart

Mongrel Configuration:

Now Go to the application directory and create a configuration for Mongrel:

mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 2 -c /home/APP/production/APP/current

it will create the config/mongrel_cluster.yml file; basically you do not need to edit it.

You can test cluster with

To Start:

mongrel_rails cluster::start

To Stop:

mongrel_rails cluster::stop

To Restart:

mongrel_rails cluster::restart

Capistrano Configuration:

Then create a configuration for Capistrano:

cap –apply-to .

we need to modify the generated file config/deploy.rb:

require 'mongrel_cluster/recipes'

#you set the APP name with the cap command
set :application, "APP"
#a path to your repository
set :repository, "svn+ssh://USERNAME@SVN_SERVER/projects/#{application}/trunk"

role :web, "SERVER"
role :app, "SERVER"
role :db, "SERVER", :primary => true

#where to deploy (copy the files) on the server; I created a special user APP for the application (if you do not like it, replace the /home/#{application} part with your path
set :deploy_to, "/home/#{application}/production/#{application}"
set :mongrel_conf, "#{current_path}/config/mongrel_cluster.yml"
#if the server login name is different to the development computer login name; in my case the user name is the APP name
set :user, "APP"

Make necessary changes to the config/database.yml file.

Capistrano Deployment:

Create the basic structure on the server:

cap deploy:setup

For the first deployment you can use cold deploy:

cap cold_deploy

After that for next deployments you have to use:

cap deploy

You can also run migration from capistrano with:

cap deploy:migrate*

* Note: It wont work for me, it migrating to the previous release available in the releses folder, So I suggest you manually run migration on server.

That’s it, your server is ready to run.
Hope this documentation will be helpful to you, Enjoy ;-).

Posted by Bhushan Ahire, filed under Ruby On Rails. Date: July 29, 2008, 9:38 am |

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.