Deploying a Rails application on CentOS

I love Ubuntu. “Why not?”, you may ask. Well, the only reason is that I have worked almost exclusively with Ubuntu when deploying Rails applications.

Aside from the point that it’s easier and most tutorials on the internet involve Ubuntu deployment, at my current company, we only use Ubuntu on our staging and production server.

I always had the notion that deployment on other Linux distributions would be hard. It turns out, they may not be that different at all. Here’s the deployment recipe I’m using to deploy on CentOS 6.

Update CentOS

yum update

Install requirements

yum -y groupinstall "Development Tools"

groupinstall "Development Tools" will install necessary tools to create a basic development environment. Next step would be to make sure we don’t miss out on some other tools.

yum -y install ntp zlib zlib-devel sqlite-devel httpd mysql-server mysql-devel curl-devel httpd-devel apr-devel apr-util-devel mlocate manlibxml2-devel libxslt-devel libffi-devel readline-devel gcc gcc-c++ make openssl-devel sqlite-devel
/etc/init.d/ntpd stop
/etc/init.d/ntpd start
chkconfig ntpd on

Install Ruby

We are using ruby version 1.9.3 because Passenger 4.0 can’t work with 2.0.0 yet.

tar zxf ruby-1.9.3-p429.tar.gz
cd ruby-1.9.3-p429
make install
cd ext/openssl
ruby extconf.rb
make install
cd ../zlib
ruby extconf.rb
make install
cd ../readline
ruby extconf.rb
make install
gem update --system

If you are presented by this error:

It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.

A simple fix would be to install libyaml.

curl -O
sudo  rpm -Uvh
sudo yum install libyaml-devel

And then reinstall Ruby.

Install Rails and Passenger

gem install rails passenger

Install the Passenger and Apache module


After installation, you will be presented by some text(see below) you need to add to your Apache config file, which can be found here, /etc/httpd/conf/httpd.conf.

Load Module
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-4.0.5/libout/apache2/
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-4.0.5
PassengerDefaultRuby /usr/local/bin/ruby

You will also be presented by an Apache configuration file that you can modify and use for your virtual host

Virtual Host file
<VirtualHost *:80>
    # !!! Be sure to point DocumentRoot to 'public'!
    DocumentRoot /somewhere/public    
    <Directory /somewhere/public>
       # This relaxes Apache security settings.
       AllowOverride all
       # MultiViews must be turned off.
       Options -MultiViews

Restart Apache

Now if everything goes well, you can restart Apache.

Visit your url and voila! A newly deployed Rails Application!

Visit the Passenger site for more Information about Deploying Rails with Passenger on Apache

 Starting, Stopping and Restarting Apache2

It will be more and more common as we add modules, settings to Apache2 to restart Apache2. You will need to start and stop the server when needed.

You have a few scripts available for this common tasks.

apachectl restart

This will restart the server. If the server isn’t running, it will start it.

apachechtl stop

The server would be stopped immediately

apachectl graceful

The server would be stopped. However, the server would allow connections in progress to be completed. Again, the server would also be restarted it isn’t.

apachectl start

This will start the server.

There is a convenient to start services in most Unix systems, service.

restarts the server

service apache restart

stops the server

service apache stop

starts the server

service apache start

In Fedora based systems, Red Hat and CentOS, there may be a slight change of commands

restarts the server

service httpd restart

stops the server

service httpd stop

starts the server

service httpd start

 Enable Mod Rewrite in Apache2 on Ubuntu

I have been working intensely with Apache2 for a few days and learning the ins and outs of Apache2.

Mod Rewrite is an Apache module used to modify urls in most CMSs like Wordpress.

Configuration for Apache2 can be found at /etc/apache2/. In Apache2, if you need to enable modules you need to copy files from /etc/apache2/mods-available/ to /etc/apache2/mods-enabled/.

Enable Mod Rewrite

Now we are ready to enable Mod Rewrite. Type the following commands:

cd /etc/apache2/
cp mods-available/rewrite.load mods-enabled/rewrite.load

The above would copy the module to the mods-enabled folder. Another option would be to create a symbolic links.

cd /etc/apache2/
ln -s mods-available/rewrite.load mods-enabled/rewrite.load

Restart Apache

All changes would not take effect until we restart Apache2.

service apache2 restart

Mod Rewrite Enabled!


In Debian based systems, modules can be enabled and disabled at will using utilities provided by Debian’s Apache distribution. In our case, if you are using Debian, you can use the following command to load the Rewrite Module.

a2enmod rewrite

Good luck!

 Install PHP and MySQL with APC on Ubuntu

I have been dabbling with VPS’s for a while now and have always wanted recipes I can work with and come back to when I do my setup. I don’t want to search for what packages I need to install and have missed.

Here’s a basic setup I do. This will of course be reserved as a reference for anyone needing to setup PHP on a blank server (Ubuntu 12.04).


Chances are, you would need a web server. My choice has always been Apache.

sudo apt-get install apache2

Here, we are going to install the needed modules for Apache to work with PHP.

sudo apt-get install libapache2-mod-php5


First of all, you would need to install MySQL.

sudo apt-get install mysql-server mysqltuner

mysqltuner is a performance script to tune performance for MySQL.

Secure your MySQL
sudo mysql_secure_installation

It will present you with a few questions, starting with your MySQL password. You will then secure parts of your MySQL installation.


If you’re like me, you would want to get the latest PHP versions available for your system version. You may need to skip this if you want to install the default distributed PHP version for your system.

Add repository for latest PHP version
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php5
sudo apt-get update
sudo apt-get upgrade
Install PHP
sudo apt-get install php5-common php5-mysql php5-xmlrpc php5-cgi php5-curl php5-gd php5-cli php5-fpm php-apc php-pear php5-dev php5-imap php5-mcrypt
Secure PHP Installation(optional, but recommended)
sudo apt-get install php-suhosin

Check your PHP version

php -v

You may get something like this depending on your PHP version:

PHP 5.3

PHP 5.3.6-13ubuntu3.10 with Suhosin-Patch (cli) (built: Mar 11 2013 14:30:28) 
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

PHP 5.4

PHP 5.4.17RC1 (cli) (built: Jun 22 2013 19:27:26) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

PHP 5.5

PHP 5.5.1-1~precise+1 (cli) (built: Jul 22 2013 07:42:13) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
    with Zend OPcache v7.0.2-dev, Copyright (c) 1999-2013, by Zend Technologies

Next Stop

A tutorial on how to host multiple sites will be arranged in a couple of weeks. Stay tuned!