Ruby on Rails
HowToDeployToCentosVPS (Version #12)

How To Deploy a Rails app to a Plesk CentOS VPS
Chris Hulbert
chris.hulbert@remove-this-bit.gmail.com
Please email me if this guide is useful! Also feel free to add corrections.

This is for installing a rails application on one of those CentOS/Plesk VPS servers. My version of CentOS was 4.4 when doing all this. The architecture looks like this:

Shopping list

  • Install Nano
  • Install YUM
  • Install GCC
  • Install Ruby
  • Install Ruby Gems
  • Install Rails
  • Install FCGI
  • Install Mongrel
  • Install Pen
  • Install and configure your app
  • Configure Mongrels and Pen to start on start-up
  • Configure Apache

Install Nano
This is essential, unless you know how to use vi to edit the configuration files. Me – I’d rather be spending time with my wife than learning how to use yet another editor.

cd tilde
wget http://www.nano-editor.org/dist/v2.0/RPMS/nano-2.0.1-1.i386.rpm
rpm -i nano-2.0.1-1.i386.rpm

Install YUM
This installs YUM, the package manager, a prerequisite to installing GCC.

cd tilde
mkdir yum
cd yum
rpm —import /usr/share/doc/centos-release-4/RPM-GPG-KEY
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/yum-2.4.3-1.c4.noarch.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/libxml2-python-2.6.16-6.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/python-elementtree-1.2.6-4.2.1.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/python-sqlite-1.1.7-1.2.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/rpm-python-4.3.3-18_nonptl.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/python-urlgrabber-2.9.8-2.noarch.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/sqlite-3.3.3-1.2.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/sqlite-devel-3.3.3-1.2.i386.rpm
rpm -i libxml2-python-2.6.16-6.i386.rpm
rpm -i python-elementtree-1.2.6-4.2.1.i386.rpm
rpm –i sqlite-3.3.3-1.2.i386.rpm
rpm -i sqlite-devel-3.3.3-1.2.i386.rpm
rpm -i python-sqlite-1.1.7-1.2.i386.rpm
rpm -i rpm-python-4.3.3-18_nonptl.i386.rpm
rpm -i python-urlgrabber-2.9.8-2.noarch.rpm
rpm -i yum-2.4.3-1.c4.noarch.rpm

Install GCC
This installs the C compiler

yum install gcc

Install Ruby
This is to install the ruby interpreter:

cd tilde
mkdir ruby
cd ruby
wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.5.tar.gz
tar xvzf ruby-1.8.5.tar.gz
cd ruby-1.8.5
./configure
make
make install

Now you’ll want to test ruby:

irb
RUBY_VERSION should give ‘1.5’
Quit

Install Ruby Gems
Now install ruby gems:

cd tilde
mkdir rubygems
cd rubygems
wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
tar xvzf rubygems-0.9.2.tgz
cd rubygems-0.9.2
ruby setup.rb

Now you’ll want to test ruby gems:

gem

Install Rails
Install rails. I had to repeat this a couple times for it to ‘find’ the server for some reason. The no-rdoc and no-ri are to save disk space – you don’t want help files filling up your tiny VPS.

gem i rails -y —no-rdoc —no-ri

Install FCGI
If you’re using FCGI instead of mongrel, you’ll need this. If you’re using mongrel, you won’t need it, but I installed it “just in case”. Your choice (my recommendation: don’t bother). But here it is anyway:

cd tilde
mkdir fcgi
cd fcgi
wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
tar xzvf fcgi-2.4.0.tar.gz
cd fcgi-2.4.0
./configure
make
make install
gem i fcgi

Install Mongrel
Mongrel should need no introduction:

gem i mongrel

Choose: ‘1. mongrel 1.0.1 (ruby)’, ‘y’es to all the dependencies, and ‘1. fastthread 1.0 (ruby)’
Now you’ll want to test rails, just to see that ruby/rails/mongrel are all working:

cd tilde
rails testrails
cd testrails
mongrel_rails start –p 81

Then go to your web browser and do: http://your-servers-ip-address:81/
You should see ‘welcome to rails’ – if so, cheer!
Then Ctrl-C to quit mongrel, and delete the testing stuff:

cd tilde
rm –r -d testrails

Install Pen
Pen is the load balancer that will sit between apache and your pair of mongrels. To install pen (http://siag.nu/pen/):

cd tilde
mkdir pen
cd pen
wget ftp://siag.nu/pub/pen/pen-0.17.1.tar.gz
tar xzvf pen-0.17.1.tar.gz
cd pen-0.17.1
./configure
make
make install

Then do ‘pen’ to test, which should bring up its help screen.

Install and configure your app
Use PLESK to create a database and a user/password to access it.
Now to upload your application. I used ‘WINSCP’ from windows for this, uploading to tilde/my_app_name or wherever you want. Also you’ll want to upload your mysql dump file to the server too.
To import the mysql database, use the username and password that you just created with Plesk:

mysql –u -p < your-mysql-database-script.sql

Then you’ll want to test the database was imported.

mysql –u cms_homehub –p
type the password when it asks
use your-database-name;
select asterisk _ from my-table-name;

It should return stuff, which is good.
Make sure you’ve set up a different session store to the normal rails PStore. This is very important, but is outside the scope of this document. Look in the rails wiki, its there. I prefer cookiestore if you can figure out how to use it. And make sure you’ve set up your database indices!
Then go into the /config folder, and edit the database.yml file:
nano database.yml
Make the production section look like this:


production:
adapter: mysql
database: yourdatabase
username: yourusername
password: yourpassword
host: localhost
socket: /var/lib/mysql/mysql.sock

and give your app a test with mongrel, to see if it works (do this from your rails app folder):

mongrel_rails start –e production –p 82

Then go to your web browser and do: http://your-servers-ip-address:82/
Your application should be visible – if so, great! Pity its only one mongrel, without a web server in front of it. But it’s a beginning! Ctrl-C to quit mongrel.

Configure Mongrels and Pen to start on start-up
Then we need to figure out how to run the 2 mongrels on startup, so that when your server gets rebooted at midnight, your website will come back to life no worries. Keep in mind that this startup script is CentOS / Redhat specific.

cd /etc/init.d
nano mongrels

Type the following into nano:


#!/usr/local/bin/ruby

  1. This starts/stops the mongrels and pen
  2. Chris Hulbert 11-Apr-2007
  3. based on http://source.mihelac.org/articles
  4. /2007/03/27/customized-mongrel-startup-script
    #
  5. chkconfig: – 85 15
  6. description: mongrels manages Mongrel Servers and pen
    #
    if ‘stop’, ‘restart’.include? ARGV.first
    puts “Stopping mongrels…”
    `killall ruby`
    puts “Stopping pen…”
    `killall pen`
    end
    if ‘start’, ‘restart’.include? ARGV.first
    puts “Starting mongrel 0…”
    `mongrel_rails start -d -p 8000 -e production -c /your/rails/app -P log/mongrel0.pid -a localhost`
    puts “Starting mongrel 1…”
    `mongrel_rails start -d -p 8001 -e production -c /your/rails/app -P log/mongrel1.pid -a localhost`
    puts “Starting pen…”
    exec “pen -H localhost:8009 localhost:8000 localhost:8001”
    end
    unless ‘start’, ‘stop’, ‘restart’.include? ARGV.first
    puts “Usage: mongrels {start|stop|restart}”
    end

Then make it runnable:
chmod a+x mongrels

Install it as a startup script:
/sbin/chkconfig —level 345 mongrels on

Start them:
./mongrels start

Configure Apache
We now need to configure apache to talk to Pen.
For reference, there is some interesting reading here:
http://download1.sw-soft.com/Plesk/Plesk7.1/Doc/html/plesk-7-reloaded-admin/apas02.html
http://mongrel.rubyforge.org/docs/apache.html

Firstly find where plesk has installed your web site:
cd /
find | grep .php

For me, this told me that its installed in /var/www/vhosts/mywebsite/
We need to edit two config files: httpd.include and vhost.conf

httpd.include
nano /var/www/vhosts/mywebsite/conf/httpd.include
At the end of this file, add this line directly BEFORE the ‘</VirtualHost>’ line:


Include /var/www/vhosts/mywebsite/conf/vhost.conf

vhost.conf
nano /var/www/vhosts/mywebsite/conf/vhost.conf
Create this file if it does not already exist. Add the following 3 lines:


ProxyPass / http://localhost:8009/
ProxyPassReverse / http://localhost:8009/
ProxyPreserveHost on

Now restart apache:
apachectl graceful

Hopefully that should be it – it worked for me!

How To Deploy a Rails app to a Plesk CentOS VPS
Chris Hulbert
chris.hulbert@remove-this-bit.gmail.com
Please email me if this guide is useful! Also feel free to add corrections.

This is for installing a rails application on one of those CentOS/Plesk VPS servers. My version of CentOS was 4.4 when doing all this. The architecture looks like this:

Shopping list

  • Install Nano
  • Install YUM
  • Install GCC
  • Install Ruby
  • Install Ruby Gems
  • Install Rails
  • Install FCGI
  • Install Mongrel
  • Install Pen
  • Install and configure your app
  • Configure Mongrels and Pen to start on start-up
  • Configure Apache

Install Nano
This is essential, unless you know how to use vi to edit the configuration files. Me – I’d rather be spending time with my wife than learning how to use yet another editor.

cd tilde
wget http://www.nano-editor.org/dist/v2.0/RPMS/nano-2.0.1-1.i386.rpm
rpm -i nano-2.0.1-1.i386.rpm

Install YUM
This installs YUM, the package manager, a prerequisite to installing GCC.

cd tilde
mkdir yum
cd yum
rpm —import /usr/share/doc/centos-release-4/RPM-GPG-KEY
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/yum-2.4.3-1.c4.noarch.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/libxml2-python-2.6.16-6.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/python-elementtree-1.2.6-4.2.1.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/python-sqlite-1.1.7-1.2.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/rpm-python-4.3.3-18_nonptl.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/python-urlgrabber-2.9.8-2.noarch.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/sqlite-3.3.3-1.2.i386.rpm
wget http://mirror.centos.org/centos-4/4/os/i386/CentOS/RPMS/sqlite-devel-3.3.3-1.2.i386.rpm
rpm -i libxml2-python-2.6.16-6.i386.rpm
rpm -i python-elementtree-1.2.6-4.2.1.i386.rpm
rpm –i sqlite-3.3.3-1.2.i386.rpm
rpm -i sqlite-devel-3.3.3-1.2.i386.rpm
rpm -i python-sqlite-1.1.7-1.2.i386.rpm
rpm -i rpm-python-4.3.3-18_nonptl.i386.rpm
rpm -i python-urlgrabber-2.9.8-2.noarch.rpm
rpm -i yum-2.4.3-1.c4.noarch.rpm

Install GCC
This installs the C compiler

yum install gcc

Install Ruby
This is to install the ruby interpreter:

cd tilde
mkdir ruby
cd ruby
wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.5.tar.gz
tar xvzf ruby-1.8.5.tar.gz
cd ruby-1.8.5
./configure
make
make install

Now you’ll want to test ruby:

irb
RUBY_VERSION should give ‘1.5’
Quit

Install Ruby Gems
Now install ruby gems:

cd tilde
mkdir rubygems
cd rubygems
wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
tar xvzf rubygems-0.9.2.tgz
cd rubygems-0.9.2
ruby setup.rb

Now you’ll want to test ruby gems:

gem

Install Rails
Install rails. I had to repeat this a couple times for it to ‘find’ the server for some reason. The no-rdoc and no-ri are to save disk space – you don’t want help files filling up your tiny VPS.

gem i rails -y —no-rdoc —no-ri

Install FCGI
If you’re using FCGI instead of mongrel, you’ll need this. If you’re using mongrel, you won’t need it, but I installed it “just in case”. Your choice (my recommendation: don’t bother). But here it is anyway:

cd tilde
mkdir fcgi
cd fcgi
wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
tar xzvf fcgi-2.4.0.tar.gz
cd fcgi-2.4.0
./configure
make
make install
gem i fcgi

Install Mongrel
Mongrel should need no introduction:

gem i mongrel

Choose: ‘1. mongrel 1.0.1 (ruby)’, ‘y’es to all the dependencies, and ‘1. fastthread 1.0 (ruby)’
Now you’ll want to test rails, just to see that ruby/rails/mongrel are all working:

cd tilde
rails testrails
cd testrails
mongrel_rails start –p 81

Then go to your web browser and do: http://your-servers-ip-address:81/
You should see ‘welcome to rails’ – if so, cheer!
Then Ctrl-C to quit mongrel, and delete the testing stuff:

cd tilde
rm –r -d testrails

Install Pen
Pen is the load balancer that will sit between apache and your pair of mongrels. To install pen (http://siag.nu/pen/):

cd tilde
mkdir pen
cd pen
wget ftp://siag.nu/pub/pen/pen-0.17.1.tar.gz
tar xzvf pen-0.17.1.tar.gz
cd pen-0.17.1
./configure
make
make install

Then do ‘pen’ to test, which should bring up its help screen.

Install and configure your app
Use PLESK to create a database and a user/password to access it.
Now to upload your application. I used ‘WINSCP’ from windows for this, uploading to tilde/my_app_name or wherever you want. Also you’ll want to upload your mysql dump file to the server too.
To import the mysql database, use the username and password that you just created with Plesk:

mysql –u -p < your-mysql-database-script.sql

Then you’ll want to test the database was imported.

mysql –u cms_homehub –p
type the password when it asks
use your-database-name;
select asterisk _ from my-table-name;

It should return stuff, which is good.
Make sure you’ve set up a different session store to the normal rails PStore. This is very important, but is outside the scope of this document. Look in the rails wiki, its there. I prefer cookiestore if you can figure out how to use it. And make sure you’ve set up your database indices!
Then go into the /config folder, and edit the database.yml file:
nano database.yml
Make the production section look like this:


production:
adapter: mysql
database: yourdatabase
username: yourusername
password: yourpassword
host: localhost
socket: /var/lib/mysql/mysql.sock

and give your app a test with mongrel, to see if it works (do this from your rails app folder):

mongrel_rails start –e production –p 82

Then go to your web browser and do: http://your-servers-ip-address:82/
Your application should be visible – if so, great! Pity its only one mongrel, without a web server in front of it. But it’s a beginning! Ctrl-C to quit mongrel.

Configure Mongrels and Pen to start on start-up
Then we need to figure out how to run the 2 mongrels on startup, so that when your server gets rebooted at midnight, your website will come back to life no worries. Keep in mind that this startup script is CentOS / Redhat specific.

cd /etc/init.d
nano mongrels

Type the following into nano:


#!/usr/local/bin/ruby

  1. This starts/stops the mongrels and pen
  2. Chris Hulbert 11-Apr-2007
  3. based on http://source.mihelac.org/articles
  4. /2007/03/27/customized-mongrel-startup-script
    #
  5. chkconfig: – 85 15
  6. description: mongrels manages Mongrel Servers and pen
    #
    if ‘stop’, ‘restart’.include? ARGV.first
    puts “Stopping mongrels…”
    `killall ruby`
    puts “Stopping pen…”
    `killall pen`
    end
    if ‘start’, ‘restart’.include? ARGV.first
    puts “Starting mongrel 0…”
    `mongrel_rails start -d -p 8000 -e production -c /your/rails/app -P log/mongrel0.pid -a localhost`
    puts “Starting mongrel 1…”
    `mongrel_rails start -d -p 8001 -e production -c /your/rails/app -P log/mongrel1.pid -a localhost`
    puts “Starting pen…”
    exec “pen -H localhost:8009 localhost:8000 localhost:8001”
    end
    unless ‘start’, ‘stop’, ‘restart’.include? ARGV.first
    puts “Usage: mongrels {start|stop|restart}”
    end

Then make it runnable:
chmod a+x mongrels

Install it as a startup script:
/sbin/chkconfig —level 345 mongrels on

Start them:
./mongrels start

Configure Apache
We now need to configure apache to talk to Pen.
For reference, there is some interesting reading here:
http://download1.sw-soft.com/Plesk/Plesk7.1/Doc/html/plesk-7-reloaded-admin/apas02.html
http://mongrel.rubyforge.org/docs/apache.html

Firstly find where plesk has installed your web site:
cd /
find | grep .php

For me, this told me that its installed in /var/www/vhosts/mywebsite/
We need to edit two config files: httpd.include and vhost.conf

httpd.include
nano /var/www/vhosts/mywebsite/conf/httpd.include
At the end of this file, add this line directly BEFORE the ‘</VirtualHost>’ line:


Include /var/www/vhosts/mywebsite/conf/vhost.conf

vhost.conf
nano /var/www/vhosts/mywebsite/conf/vhost.conf
Create this file if it does not already exist. Add the following 3 lines:


ProxyPass / http://localhost:8009/
ProxyPassReverse / http://localhost:8009/
ProxyPreserveHost on

Now restart apache:
apachectl graceful

Hopefully that should be it – it worked for me!