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

Hi
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/mod_passenger.so
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/192.168.9.247 with the following contents within ” <VirtualHost *:443> </VirtualHost> “:

  ServerName 192.168.9.247
  DocumentRoot /var/www/192.168.9.247/current/public
      # 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 “192.168.9.247” using below one

 sudo a2ensite 192.168.9.247
 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, "192.168.9.247"
set :repository,  "git://github.com/Hareramrai/deploy_demo_app.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/192.168.9.247"
set :deploy_via, :remote_cache
set :user, "hareror"
set :use_sudo, false
role :web, "192.168.9.247"                          # Your HTTP server, Apache/etc
role :app, "192.168.9.247"                          # This may be the same as your `Web` server
role :db,  "192.168.9.247", :primary =>; true        # This is where Rails migrations will run
role :db,  "192.168.9.247"

# 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 http://github.com/rails/irs_process_scripts

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

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

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

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

  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"
  end

 end

Now update the code at github.

 git add . && git commit -m "initial commit"
 git remote add origin git@github.com:Hareramrai/deploy_demo_app.git
 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 “https://192.168.9.247 “.

You can checkout my github application at https://github.com/Hareramrai/deploy_demo_app

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

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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