Setup production server with apache, passenger and SSL in ruby on rails

Today I am going to setup production server in my local machine with Apache,Passenger server using Capistrano.Also going to use SSL with Apache.

Now we are going to create simple scaffold-ed application using ruby on rails. Which only has a single table users and it stores the simple user data. Now Create “deploy_demo_app” by running the below command

 hareror@hareram-u11:~/Projects/RubyOnRails/GitHub$ rails new deploy_demo_app -d mysql

 hareror@hareram-u11:~/Projects/RubyOnRails/GitHub$ cd deploy_demo_app && echo "rvm gemset use deploy_demo_app" >.rvmrc

 hareror@hareram-u11:~/Projects/RubyOnRails/GitHub/deploy_demo_app$rails g scaffold user name:string email:string date_of_birth:date

 hareror@hareram-u11:~/Projects/RubyOnRails/GitHub/deploy_demo_app$ rake db:create

 hareror@hareram-u11:~/Projects/RubyOnRails/GitHub/deploy_demo_app$ rake db:migrate

Now initialize the application using git because we are using git as a scm. Then the run the below command to initialize the git git repository.

 hareror@hareram-u11:~/Projects/RubyOnRails/GitHub/deploy_demo_app$ git init .

Also add the these two “rvm-capistrano” and “capistrano” gems to Gemfile.Then the capify the application using below one

 hareror@hareram-u11:~/Projects/RubyOnRails/GitHub/deploy_demo_app$ capify .

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

 hareror@hareram-u11:~/Projects/RubyOnRails/GitHub/deploy_demo_app$ gem install passenger

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

hareror@hareram-u11:~/Projects/RubyOnRails/GitHub/deploy_demo_app$ 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 /home/hareror/.rvm/gems/ruby-1.9.3-p327@deploy_demo_app/gems/passenger-3.0.19/ext/apache2/
PassengerRoot /home/hareror/.rvm/gems/ruby-1.9.3-p327@deploy_demo_app/gems/passenger-3.0.19
PassengerRuby /home/hareror/.rvm/wrappers/ruby-1.9.3-p327@deploy_demo_app/ruby

Now add the first line in ” /etc/apache2/mods-available/passenger.load” file and last two line in “/etc/apache2/mods-available/passenger.conf”.
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 within ” <VirtualHost *:443> </VirtualHost> “:

  DocumentRoot /var/www/
      # This relaxes Apache security settings.
      AllowOverride all
      # MultiViews must be turned off.
      Options -MultiViews
  SSLEngine on
  SSLCertificateFile /etc/ssl/certs/server.crt
  SSLCertificateKeyFile  /etc/ssl/private/server.key

The server.crt and server.key files are used for SSL setup.
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 to back rails app and change the “deploy.rb” file with below contents:

require 'rvm/capistrano'
set :rvm_type, :user
set :rvm_ruby_string, 'ruby-1.9.3-p327@deploy_demo_app'
set :application, ""
set :repository,  "git://"
set :scm, :git 
set :scm_username, "hareramrai"
set :branch, "master"
set :git_enable_submodules, 1
set :rails_env, "production"
default_environment["RAILS_ENV"] = 'production'
#set :rake , "/usr/bin/env/rake"
set :deploy_to, "/var/www/"
set :deploy_via, :remote_cache
set :user, "hareror"
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
role :db,  ""

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

# if you're still using the script/reaper helper you will need
# these

# If you are using Passenger mod_rails uncomment this:
 namespace :rvm do
   task :trust_rvmrc do
     run "rvm rvmrc trust #{release_path}"

 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", "rvm:trust_rvmrc"
  #  after "deploy:update_code", "deploy:migrate"
  after 'deploy:update_code' do
    run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
    run "cd #{release_path}; RAILS_ENV=production rake db:create"
    run "cd #{release_path}; RAILS_ENV=production rake db:migrate"


Now update the code at github.

 git add . && git commit -m "initial commit"
 git remote add origin
 git pull origin master
 git push origin master

Now create the certificates for SSL. We are going the create Self-Signed Certificate.
First Generating a Certificate Signing Request (CSR) using below commands:

openssl genrsa -out server.key 1024
openssl req -new -key server.key -out server.csr

To create the self-signed certificate, run the following command at a terminal prompt:

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

You have to install the key file server.key and certificate file server.crt for that use the below commands:

sudo cp server.crt /etc/ssl/certs
sudo cp server.key /etc/ssl/private 

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 “ “.

You can checkout my github application at

Thanks for reading the post 🙂 ………
Please put your queries below if you have any?


One thought on “Setup production server with apache, passenger and SSL in ruby on rails

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s