Setup apache, passenger, ruby on rails on Ubuntu RACKSPACE with mysql database

Hi , I am going write about setup of apache, passenger and ruby on rails on Ubuntu rackspace.
I am going to use MySQL as a database.

Generate SSH keys from your local machine

hareror@hareram-u11:~/>ssh-keygen -t rsa -C ""

#Generating public/private rsa key pair.
#Enter file in which to save the key (/home/hareror/.ssh/id_rsa):
hareror@hareram-u11:~/>[hit enter]

#Enter passphrase (empty for no passphrase):

hareror@hareram-u11:~/>[enter a passphrase]
#Enter same passphrase again:
hareror@hareram-u11:~/>[enter passphrase again]

Update and upgrade the RACKSPACE Server

For updating and upgrading the RACKSPACE server you need to access the server by SSH.
You can use the below command to access the server and replace with your RACKSAPCE server IPv4 address. You can get it from your RACKSPACE admin control panel.

hareror@hareram-u11:~/> ssh

Run the below command for updating & upgrading the server package and os of the Ubuntu server.

root@servername:~# apt-get update

root@servername:~# apt-get upgrade

Creat User and setup SSH keys on Sever

We need to create a new user on RACKSPACE server for deployment of RoR Application.
For creating a new user use the below command and provide the information required after running the command

root@servername:~# adduser deploy

Now create a .ssh directory inside the “/home/deploy/” and changed it’s permission using the below command.

root@servername:~# mkdir /home/deploy/.ssh
root@servername:~# chmod 700 /home/deploy/.ssh

We have to upload the public ssh key of your local machine to RACKSPACE server. So that we don’t have to enter password at every time. For doing this we have to a file “/home/deploy/.ssh/authorized_keys” and copy the content of “/.ssh/” and paste in the file “/home/deploy/.ssh/authorized_keys”. For doing this you can any editor like “nano”.

Now we have to set the permissions and ownerships for the user’s keys and home directory.

root@servername:~# chmod 400 /home/deploy/.ssh/authorized_keys
root@servername:~# chown deploy:deploy /home/deploy -R

Also we have to grant sudo privileges to the deploy user.To grant the privileges, edit the file “/etc/sudoers”. Actually we have to add the below line to grant the privileges.

deploy  ALL=(ALL) ALL

Now restrat the ssh service using the below command.

root@servername:~# service ssh restart

Install Some Ubuntu Packages

Before installing the Ruby and Rails we need to install some Ubuntu packages.Use the below command for installing these packages.

root@servername:~# sudo apt-get install curl git-core build-essential zlib1g-dev libssl-dev libreadline-gplv2-dev libyaml-dev libcurl4-openssl-dev

Now we are ready to install Ruby using wget command. We are creating the directory named
“downloads” inside deploy directory for keeping the source file of ruby.

root@servername:~# mkdir downloads && cd downloads
root@servername:~/downloads# wget
root@servername:~/downloads# tar -zxf ruby-2.0.0-p247.tar.gz
root@servername:~/downloads# cd ruby-2.0.0-p247
root@servername:~/downloads/ruby-2.0.0-p247# ./configure
root@servername:~/downloads/ruby-2.0.0-p247# make
root@servername:~/downloads/ruby-2.0.0-p247# sudo make install

Now we can install rubygem using the below commands for managing the gems.

root@servername:~/downloads# wget
root@servername:~/downloads# tar -zxf rubygems-2.0.6.tgz
root@servername:~/downloads# cd rubygems-2.0.6
root@servername:~/downloads/rubygems-2.0.6# sudo ruby setup.rb 

Then, create ~/.bashrc and add the line in the file.

export PATH=$PATH:/usr/local/lib/ruby/gems/2.0.0

Now we need to install bundler and rake gem using the below commands.

root@servername:~# sudo gem install bundler
root@servername:~# sudo gem install rake


We can install MySQL server by using this command

root@servername:~# sudo apt-get install mysql-server

Then follow the message shown by mysql and finally it will asked for password and repeat password.Give same the password for both.
Now the MySQL server is installed. You can checkout by running below command

  root@servername:~#  mysql -u root -p

Then we need to install a package to use the mysql2 gem .

root@servername:~#sudo apt-get install libmysqlclient15-dev

It will be better to create a new database user for using this credential in RoR application.
First login to Mysql as root then use the below command to create a user.

mysql> GRANT ALL PRIVILEGES ON *.* TO 'deploy'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

It will create a new user with name “deploy” and password as “password”. So we can now use it in database.yml file.

Install Apache2 with Passenger

We are going to use apache2 as a web server. So use the below command for installing it.

root@servername:~# sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert

Then go for configuring the apache with passenger and install the passenger gem.

root@servername:~# sudo gem install passenger

Now compile the Phusion Passenger binaries, which include the various Phusion Passenger agent executables and the Phusion Passenger Apache module.

root@servername:~# sudo passenger-install-apache2-module

Now we got the below output.

The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-4.0.10/buildout/apache2/
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-4.0.10
PassengerDefaultRuby /usr/local/bin/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific

Now add the above lines in “/etc/apache2/apache2.conf” file.

Now we are ready to setup virtual host and we need to create a virtual host so that apache can handle the requests and pass off to passenger.

Now create a file /etc/apache2/sites-available/ with the following contents.

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

Now the enable the site “” using below one

 sudo a2ensite
 sudo /etc/init.d/apache2 reload
 sudo chown www-data:www-data /var/www
 sudo chmod g+w /var/www

Now capify your any existing Rails application and change the deploy.rb file like the below as a sample code. Please change the git application with yours git repo application.

require 'bundler/capistrano'

set :application, "demoapp"

default_run_options[:pty] = true
set :ssh_options, { :forward_agent => true }

set :repository,  ""
set :scm, :git
set :scm_username, "hareramrai"
set :branch, "master"
set :git_enable_submodules, 1
set :rails_env, "production"
set :deploy_to, "/var/www/#{application}"
set :deploy_via, :remote_cache

set :user, "deploy"
set :use_sudo, false

role :web, ""                      # Your HTTP server, Apache/etc
role :app, ""                      # This may be the same as your `Web` server
role :db,  "", :primary => true    # This is where Rails migrations will run

# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"

namespace :deploy do
  task :start, :roles => :app do
    run "touch #{current_path}/tmp/restart.txt"

  task :stop, :roles => :app do
    # Do nothing.

  desc "Restart Application"
  task :restart, :roles => :app do
    run "touch #{current_path}/tmp/restart.txt"

  after 'deploy:update_code' do
    run "cd #{release_path}; RAILS_ENV=production rake db:create"
    run "cd #{release_path}; RAILS_ENV=production rake db:migrate"
    run "cd #{release_path}; RAILS_ENV=production rake db:seed"
    run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"


Now we are ready for capistrano setup for that run the below command :

 cap deploy:setup 

Now deploy the application using this

 cap deploy 

Now we can check the own currently deployed application at “ “. This URL is an sample URL please change it with your URL.