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

Currency Converter in Ruby on Rails

We can convert money from a currency unit to another currency unit using a web service provided by websericex.net.
The conversion rate is very volatile , so we have to depend on a good resource and websericex.net is not only a good resource but also a free service to use.

I have already written a tips about it at mindfire solutions tips section.

You can checkout the details at currency converter

You can also checkout my running application at heroku and github source code at https://github.com/Hareramrai/CurrencyConverter

Please put your queries below if you have any?

Thanks for reading the post 🙂 ………..

Setup Ruby on Rails on ubuntu 12.04 with mysql database

Hi
Today I am going to install ruby on rails with mysql database on Ubuntu 12.04.

We can install MySQL server by using this command

hareram@hareram:~$ 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

  hareram@hareram:~$  mysql -u root -p

Now check if your system has already installed curl or not by running the below command.

  hareram@hareram:~$ curl

If you see the below one then curl is not installed on your system.

The program 'curl' is currently not installed.  You can install it by typing:
sudo apt-get install curl

Then install curl on your system using the below command

   hareram@hareram:~$ sudo apt-get install curl

We are installing the curl because we need to install rvm using curl.

Now we are ready to install rvm .
We can install rvm using the below command

  hareram@hareram:~$\curl -L https://get.rvm.io | bash -s stable

If you will get warning like below one

  * WARNING: You're using ~/.profile, make sure you load it,
    add the following line to ~/.bash_profile if it exists
    otherwise add it to ~/.bash_login:

      source ~/.profile

Then add “source ~/.profile” to bash_profile file.
Also run the below command

  hareram@hareram:~$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bashrc

Now check the requirements for installing ruby using below command

   hareram@hareram:~$ rvm requirements

After running the above command you will get the additional list of application need to be installed before ruby is installed

# For ruby:
sudo apt-get --no-install-recommends install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev libgdbm-dev ncurses-dev automake libtool bison subversion pkg-config libffi-dev

Run the above command to install the other application before installing the ruby.

Now we can install the ruby 1.9.3 using the below command

  hareram@hareram:~$ rvm install 1.9.3

Now the set default ruby using

 hareram@hareram:~$ rvm use 1.9.3 --default

Now install current version rails using the below command

  hareram@hareram:~$ gem install rails 

Now creata a demoapp using rails

  hareram@hareram:~$ rails new demoapp -d mysql

We have passed “-d mysql” for using mysql database for application.
If you get the below error of mysql2 gem install

 An error occurred while installing mysql2 (0.3.11), and Bundler cannot continue.
 Make sure that `gem install mysql2 -v '0.3.11'` succeeds before bundling.

Then you have to install the below application to complete the mysql2 gem installation.

 sudo apt-get install libmysql-ruby
 sudo apt-get install libmysqlclient-dev

Now Ruby on Rails with mysql database are installed on your system and you are ready to fly in the world of Ruby on Rails.
Thanks for reading post ….. 🙂
Please put your queries if you have any?