Deploy Rails Applications How to Deploy Rails Application by Capistrano By Shishir Kumar Saha tekSymmetry, LLC
Introduction  <ul><li>Configure Rails Environment  </li></ul><ul><li>Setup Capistrano </li></ul><ul><li>Apache virtual hos...
Configure Rails Environment <ul><li>Ruby </li></ul><ul><li>RubyGems  -  RubyGems is a package manager for the Ruby program...
Setup Capistrano <ul><li>Installation command –  </li></ul><ul><li>sudo gem install capistrano --include-dependencies </li...
<ul><li>How to configure? </li></ul><ul><li>Create a user –  </li></ul><ul><li>useradd username </li></ul><ul><li>passwd u...
About sites-enabled directory <ul><li>What is &quot;sites-enabled&quot; directory in apache configuration path </li></ul><...
<ul><li>Configuration details – </li></ul><ul><li><VirtualHost *:80> </li></ul><ul><li>ServerName sfftest.mooo.com </li></...
<ul><li>Virtual Host refers to the practice of maintaining more than one server on one machine, as differentiated by their...
Virtual host configuration <ul><li><VirtualHost *:80> </li></ul><ul><li>ServerName sfftest.mooo.com </li></ul><ul><li>Serv...
Apache reverse proxy <ul><li>Forward Proxies and Reverse Proxies </li></ul><ul><li>Forward proxy  provide Internet access ...
Mongrel cluster <ul><li>Mongrel_cluster is a plugin that wrappers the mongrel HTTP server and simplifies the deployment of...
Apache reverse proxy and Mongrel cluster <ul><li><Directory &quot;/var/www/rails/saudi_fantasy_football/current/public&quo...
Deploy.rb <ul><li>Directory /config/deploy.rb </li></ul><ul><li>require 'mongrel_cluster/recipes' </li></ul><ul><li>set :a...
Deploy.rb <ul><li># Configure production database </li></ul><ul><li>namespace :database do </li></ul><ul><li>  desc &quot;...
Deploy.rb <ul><li># Configure mongrel cluster configuration </li></ul><ul><li>namespace :mongrel do </li></ul><ul><li>  de...
Multiple virtual hosting <ul><li>Just create a new configuration file in /etc/apache2/sites-enabled/ directory maintaining...
Reference <ul><li>http://www.capify.org/getting-started/from-the-beginning/ </li></ul>
Question?
Upcoming SlideShare
Loading in …5
×

Deploy Rails Application by Capistrano

3,816 views

Published on

How to Deploy Rails Application by Capistrano

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,816
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
46
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Deploy Rails Application by Capistrano

  1. 1. Deploy Rails Applications How to Deploy Rails Application by Capistrano By Shishir Kumar Saha tekSymmetry, LLC
  2. 2. Introduction <ul><li>Configure Rails Environment </li></ul><ul><li>Setup Capistrano </li></ul><ul><li>Apache virtual host configuration </li></ul><ul><li>Configure mongrel cluster </li></ul><ul><li>Configure multiple virtual hosts </li></ul>
  3. 3. Configure Rails Environment <ul><li>Ruby </li></ul><ul><li>RubyGems - RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries </li></ul><ul><li>Ruby on Rails </li></ul><ul><li>Mongrel - Mongrel is a fast, stand-alone HTTP library and server for Ruby. It is also a Gem. </li></ul><ul><li>Subversion - Subversion is an open-source version control system </li></ul><ul><li>Capistrano - Capistrano is a utility which is used to automate the deployment of Rails applications </li></ul><ul><li>Mysql </li></ul>
  4. 4. Setup Capistrano <ul><li>Installation command – </li></ul><ul><li>sudo gem install capistrano --include-dependencies </li></ul><ul><li>Deploy command – </li></ul><ul><li>Go to your local rails application directory and just write </li></ul><ul><li>cap deploy </li></ul><ul><li>cap deploy:migrations </li></ul><ul><li>That’s all ……… your deploy process is already finished !!! </li></ul>
  5. 5. <ul><li>How to configure? </li></ul><ul><li>Create a user – </li></ul><ul><li>useradd username </li></ul><ul><li>passwd username </li></ul><ul><li>Create a deploy directory – </li></ul><ul><ul><li>cd /var/www/rails </li></ul></ul><ul><ul><li>mkdir new_rails_application </li></ul></ul><ul><li>Change ownership of deploy directory – </li></ul><ul><li>chown –R /var/www/rails/new_rails_application </li></ul><ul><li>Go to Sites-enabled direcctory and create individual virtual host files as below– </li></ul><ul><li>cd /etc/apache2/sites-enabled/ </li></ul><ul><li>001-test_saudi_fantasy_football </li></ul><ul><li>002-test_gulf_hotelz </li></ul><ul><li>These individual file will cover virtual host, mongrel cluster and reverse proxy configuration </li></ul>Apache configuration
  6. 6. About sites-enabled directory <ul><li>What is &quot;sites-enabled&quot; directory in apache configuration path </li></ul><ul><li>Why it needs? </li></ul><ul><li>Some Apache installations (such as the default installs on Debian and Ubuntu) by default define a global virtual host that shares /var/www as the document root. This may lead to problems with your install. If you access your site and see nothing but a directory listing, then you’re affected by this problem. </li></ul><ul><li>The solution is to remove the “default” site from your /etc/apache2/sites-enabled directory. This is called something like default or 000-default . </li></ul>
  7. 7. <ul><li>Configuration details – </li></ul><ul><li><VirtualHost *:80> </li></ul><ul><li>ServerName sfftest.mooo.com </li></ul><ul><li>ServerAlias sfftest.codesymmetry.com </li></ul><ul><li># Presuming your rails app is at /var/www/test </li></ul><ul><li>DocumentRoot /var/www/rails/saudi_fantasy_football/current/public </li></ul><ul><li><Directory &quot;/var/www/rails/saudi_fantasy_football/current/public&quot;> </li></ul><ul><li>Options FollowSymLinks </li></ul><ul><li>AllowOverride None </li></ul><ul><li>Order allow,deny </li></ul><ul><li>Allow from all </li></ul><ul><li></Directory> </li></ul><ul><li># Configure mongrel_cluster </li></ul><ul><li><Proxy balancer://sff_mongrel_cluster> </li></ul><ul><li>BalancerMember http://127.0.0.1:8000 </li></ul><ul><li>BalancerMember http://127.0.0.1:8001 </li></ul><ul><li>BalancerMember http://127.0.0.1:8002 </li></ul><ul><li></Proxy> </li></ul><ul><li>ProxyPass / balancer://sff_mongrel_cluster/ </li></ul><ul><li>ProxyPassReverse / balancer://sff_mongrel_cluster/ </li></ul><ul><li># These directories should always be served up by Apache, since they contain static content. Or just let rails do it. </li></ul><ul><li>ProxyPass /images ! </li></ul><ul><li>ProxyPass /stylesheets ! </li></ul><ul><li>ProxyPass /javascripts ! </li></ul><ul><li>ProxyPass /favicon.ico ! </li></ul><ul><li>RewriteEngine On </li></ul><ul><li># Check for maintenance file and redirect all requests </li></ul><ul><li># ( this is for use with Capistrano's disable_web task ) </li></ul><ul><li>RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f </li></ul><ul><li>RewriteCond %{SCRIPT_FILENAME} !maintenance.html </li></ul><ul><li>RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L] </li></ul><ul><li># Important rule to prevent exposure of subversion files if you are deploying with Capistrano ! </li></ul><ul><li>RewriteRule ^(.*/)?.svn/ - [F,L] </li></ul><ul><li># Rewrite index to check for static </li></ul><ul><li>RewriteRule ^/$ /index.html [QSA] </li></ul><ul><li># Rewrite to check for Rails cached page </li></ul><ul><li>RewriteRule ^([^.]+)$ $1.html [QSA] </li></ul><ul><li># Redirect all non-static requests to cluster </li></ul><ul><li>RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f </li></ul><ul><li>RewriteRule ^/(.*)$ balancer://sff_mongrel_cluster%{REQUEST_URI} [P,QSA,L] </li></ul><ul><li></VirtualHost> </li></ul>
  8. 8. <ul><li>Virtual Host refers to the practice of maintaining more than one server on one machine, as differentiated by their apparent hostname. </li></ul><ul><li>For example, it is often desirable for companies sharing a web server to have their own domains, with web servers accessible as </li></ul><ul><li>www.company1.com </li></ul><ul><li>www.company2.com, </li></ul>Virtual host
  9. 9. Virtual host configuration <ul><li><VirtualHost *:80> </li></ul><ul><li>ServerName sfftest.mooo.com </li></ul><ul><li>ServerAlias sfftest.codesymmetry.com </li></ul><ul><li># Presuming your rails app is at /var/www/test </li></ul><ul><li>DocumentRoot /var/www/rails/saudi_fantasy_football/current/public </li></ul>
  10. 10. Apache reverse proxy <ul><li>Forward Proxies and Reverse Proxies </li></ul><ul><li>Forward proxy provide Internet access to internal clients that are restricted by a firewall. The forward proxy can also use caching (as provided by mod_cache) to reduce network usage. </li></ul><ul><li>ProxyRequests On </li></ul><ul><li>ProxyVia On </li></ul><ul><li><Proxy *> </li></ul><ul><li>Order deny,allow </li></ul><ul><li>Deny from all </li></ul><ul><li>Allow from internal.example.com </li></ul><ul><li></Proxy> </li></ul><ul><li>Reverse proxy provide Internet users access to a server that is behind a firewall. Reverse proxies can also be used to balance load among several back-end servers, or to provide caching for a slower back-end server </li></ul><ul><li>ProxyRequests Off </li></ul><ul><li><Proxy *> </li></ul><ul><li>Order deny,allow </li></ul><ul><li>Allow from all </li></ul><ul><li></Proxy> </li></ul>
  11. 11. Mongrel cluster <ul><li>Mongrel_cluster is a plugin that wrappers the mongrel HTTP server and simplifies the deployment of webapps using a cluster of mongrel servers </li></ul>
  12. 12. Apache reverse proxy and Mongrel cluster <ul><li><Directory &quot;/var/www/rails/saudi_fantasy_football/current/public&quot;> </li></ul><ul><li> Options FollowSymLinks </li></ul><ul><li> AllowOverride None </li></ul><ul><li> Order allow,deny </li></ul><ul><li> Allow from all </li></ul><ul><li> </Directory> </li></ul><ul><li># Configure mongrel_cluster </li></ul><ul><li> <Proxy balancer://sff_mongrel_cluster> </li></ul><ul><li> BalancerMember http://127.0.0.1:8000 </li></ul><ul><li> BalancerMember http://127.0.0.1:8001 </li></ul><ul><li> BalancerMember http://127.0.0.1:8002 </li></ul><ul><li> </Proxy> </li></ul><ul><li>ProxyPass / balancer://sff_mongrel_cluster/ </li></ul><ul><li> ProxyPassReverse / balancer://sff_mongrel_cluster/ </li></ul>
  13. 13. Deploy.rb <ul><li>Directory /config/deploy.rb </li></ul><ul><li>require 'mongrel_cluster/recipes' </li></ul><ul><li>set :application, &quot;saudi_fantasy_football&quot; </li></ul><ul><li>set :repository, &quot;svn://sfftest.mooo.com/saudi_fantasy_football/trunk/development/web&quot; </li></ul><ul><li>set :scm_username, “david@teksymmetry.net&quot; </li></ul><ul><li>set :scm_password, “david&quot; </li></ul><ul><li># If you aren't deploying to /u/apps/#{application} on the target </li></ul><ul><li># servers (which is the default), you can specify the actual location </li></ul><ul><li># via the :deploy_to variable: </li></ul><ul><li>set :deploy_to, &quot;/var/www/rails/#{application}&quot; </li></ul><ul><li># set :app_server, :passenger </li></ul><ul><li>set :use_sudo, false </li></ul><ul><li>set :mongrel_conf, &quot;#{current_path}/config/mongrel_cluster.yml&quot; </li></ul><ul><li># If you aren't using Subversion to manage your source code, specify </li></ul><ul><li># your SCM below: </li></ul><ul><li># set :scm, :subversion </li></ul><ul><li>role :app, &quot;sfftest.mooo.com&quot;, :user => &quot;sff&quot; </li></ul><ul><li>role :web, &quot;sfftest.mooo.com&quot;, :user => &quot;sff&quot; </li></ul><ul><li>role :db, &quot;sfftest.mooo.com&quot;, :user => &quot;sff&quot;, :primary => true </li></ul>
  14. 14. Deploy.rb <ul><li># Configure production database </li></ul><ul><li>namespace :database do </li></ul><ul><li> desc &quot;Create database configuration file&quot; </li></ul><ul><li> task :production do </li></ul><ul><li> run &quot;rm #{current_path}/config/database.yml&quot; </li></ul><ul><li> configuration = { </li></ul><ul><li> &quot;production&quot; => { </li></ul><ul><li> &quot;adapter&quot; => &quot;mysql&quot;, </li></ul><ul><li> &quot;database&quot; => &quot;saudi_fantasy_football_production&quot;, </li></ul><ul><li> &quot;encoding&quot; => &quot;utf8&quot;, </li></ul><ul><li> &quot;pool&quot; => 5, </li></ul><ul><li> &quot;timeout&quot; => 5000, </li></ul><ul><li> &quot;username&quot; => &quot;sfftest&quot;, </li></ul><ul><li> &quot;password&quot; => “sfftest&quot; </li></ul><ul><li> } </li></ul><ul><li> } </li></ul><ul><li> put configuration.to_yaml, &quot;#{current_path}/config/database.yml&quot; </li></ul><ul><li> end </li></ul><ul><li>end </li></ul><ul><li>after &quot;deploy:update&quot;, &quot;database:production&quot; </li></ul>
  15. 15. Deploy.rb <ul><li># Configure mongrel cluster configuration </li></ul><ul><li>namespace :mongrel do </li></ul><ul><li> desc &quot;Configure mongrel cluster&quot; </li></ul><ul><li> task :configure do </li></ul><ul><li> configuration = { </li></ul><ul><li> &quot;log_file&quot; => &quot;log/mongrel.log&quot;, </li></ul><ul><li> &quot;port&quot; => &quot;8000&quot;, </li></ul><ul><li> &quot;pid_file&quot; => &quot;tmp/pids/mongrel.pid&quot;, </li></ul><ul><li> &quot;environment&quot; => &quot;production&quot;, </li></ul><ul><li> &quot;servers&quot; => 3 </li></ul><ul><li> } </li></ul><ul><li> put configuration.to_yaml, &quot;#{current_path}/config/mongrel_cluster.yml&quot; </li></ul><ul><li> end </li></ul><ul><li>desc &quot;Restart mongrel&quot; </li></ul><ul><li> task :restart do </li></ul><ul><li> sudo &quot;mongrel_cluster_ctl restart -c #{current_path}/config/mongrel_cluster.yml -v&quot; </li></ul><ul><li> end </li></ul><ul><li>end </li></ul><ul><li>after &quot;deploy:update&quot;, &quot;mongrel:configure&quot; </li></ul>
  16. 16. Multiple virtual hosting <ul><li>Just create a new configuration file in /etc/apache2/sites-enabled/ directory maintaining the naming order ie., 003-test_gulf_auto </li></ul>
  17. 17. Reference <ul><li>http://www.capify.org/getting-started/from-the-beginning/ </li></ul>
  18. 18. Question?

×