{"id":15917,"date":"2014-10-30T16:04:27","date_gmt":"2014-10-30T10:34:27","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=15917"},"modified":"2014-10-30T16:04:27","modified_gmt":"2014-10-30T10:34:27","slug":"deploying-a-simple-php-application-using-capistrano-on-aws-ec2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/deploying-a-simple-php-application-using-capistrano-on-aws-ec2\/","title":{"rendered":"Deploying a simple php Application using Capistrano on AWS EC2"},"content":{"rendered":"<p><strong>Introduction :<\/strong><br \/>\nIn this article we will see how we can automate the deployment using Capistrano,<br \/>\nand how we can roll back to previous version if needed.<br \/>\nAlso in order to connect to deployment server we&#8217;ll use the key based authentication .<\/p>\n<p><strong>What is Capistrano ?<\/strong><br \/>\nCapistrano is a remote server automation tool.<br \/>\nIt supports the scripting and execution of arbitrary tasks, and includes a set of sane-default deployment workflows.<\/p>\n<p><strong>Advantages :<\/strong><\/p>\n<ul>\n<li>Reliably deploy web application to any number of machines simultaneously, in sequence or as a rolling set.<\/li>\n<li>Rollback in case of previous version needed .<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: left\">Lets have a quick overview of how our setup will look like.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap_deployment.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15889\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap_deployment.png\" alt=\"cap_deployment_block_diagram\" width=\"649\" height=\"381\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap_deployment.png 649w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap_deployment-300x176.png 300w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/a><\/p>\n<p>We&#8217;ll be having one development server, one deployment server and one Git Repo .<br \/>\n(Signup to Github and create one repository if you don&#8217;t have the Git Account.)<\/p>\n<p>We&#8217;ll be installing packages in development as well as in Deployment server as mentioned in diagram below.<\/p>\n<p>Lets do these step by step :<\/p>\n<ul>\n<li>Provision an EC2 instance for development server or use an existing in case if you have setup already.(in our case its t2.micro instance and Ubuntu 14.04 ).<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li>Login to machine and install following necessary packages for setting up Capistrano.<\/li>\n<\/ul>\n<p># Install build essential packages necessary for Ruby.<br \/>\n<code>aptitude install -y build-essential<\/code><\/p>\n<p># Some additional, commonly used tools for github and subversion etc.:<br \/>\n<code>aptitude install -y cvs subversion git-core libyaml-dev mercurial<\/code><\/p>\n<p># Setup Ruby Version Manager<br \/>\n<code>curl -L get.rvm.io | bash -s stable<\/code><\/p>\n<p># Using RVM create System Environment variable:<br \/>\n<code>source \/etc\/profile.d\/rvm.sh<\/code><\/p>\n<p># Download and install Ruby using RVM:<br \/>\n<code>rvm reload<br \/>\nrvm install 2.1.0<\/code><\/p>\n<ul>\n<li>Now Install Capistrano :<\/li>\n<\/ul>\n<p><code>gem install capistrano --no-ri --no-rdoc<\/code><\/p>\n<ul>\n<li>Provision an EC2 instance for Configure the deployment server.<\/li>\n<\/ul>\n<p><\/p>\n<ul>\n<li>Create one user and add it to group:<\/li>\n<\/ul>\n<p><code>sudo addgroup www<\/code><\/p>\n<p>Create a new user and add it to this group.<\/p>\n<p><code>sudo adduser deployer<\/code><\/p>\n<p># Add the user to an already existing group:<br \/>\n<code>sudo adduser deployer www<\/code><\/p>\n<ul>\n<li>Append the following right after root ALL=(ALL) ALL in \/etc\/sudoers file.<br \/>\ndeployer ALL=(ALL:ALL) NOPASSWD:ALL<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li><code>Install and configure php5, php5-fpm and nginx<br \/>\nsudo apt-get install nginx<br \/>\nsudo apt-get install php5<br \/>\nsudo apt-get install php5-fpm<\/code><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li>Set the directory permissions to make it access for the Nginx.<\/li>\n<p>\n&nbsp;# Set the ownership of the folder to members of `www` group<br \/>\n<code>sudo chown -R :www \/usr\/share\/nginx\/html<\/code>\n<\/ul>\n<p># Set folder permissions recursively<br \/>\n<code>sudo chmod -R g+rwX \/usr\/share\/nginx\/html<\/code><\/p>\n<p># Ensure permissions will affect future sub-directories etc.<br \/>\n<code>sudo chmod g+s \/usr\/share\/nginx\/html<\/code><\/p>\n<ul>\n<li>nginx configuration :<br \/>\n<code>server {<br \/>\nlisten 80 default_server;<br \/>\nlisten [::]:80 default_server ipv6only=on;<br \/>\nroot \/usr\/share\/nginx\/html\/current;<br \/>\nindex index.html index.php index.htm;<br \/>\n# Make site accessible from http:\/\/localhost\/<br \/>\nserver_name localhost;<\/li>\n<\/ul>\n<p>location \/ {<br \/>\n# First attempt to serve request as file, then<br \/>\n# as directory, then fall back to displaying a 404.<br \/>\ntry_files $uri $uri\/ =404;<br \/>\n# Uncomment to enable naxsi on this location<br \/>\n# include \/etc\/nginx\/naxsi.rules<br \/>\n}<br \/>\nlocation ~ \\.php$ {<br \/>\nfastcgi_split_path_info ^(.+\\.php)(\/.+)$;<br \/>\nfastcgi_pass unix:\/var\/run\/php5-fpm.sock;<br \/>\nfastcgi_index index.php;<br \/>\ninclude fastcgi_params;<br \/>\n}<\/code><\/p>\n<ul>\n<li> Set fix_path_info=0 in php.ini<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li>Now move back to Development Server.<br \/>\nGoTo the directory where your application source code is located.<br \/>\n<code>cd Git-Ishant\/ishantkumar\/<\/code><br \/>\ncreate a file index.php (remove old index.html)<\/li>\n<\/ul>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15896\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap1.png\" alt=\"capistrano1\" width=\"671\" height=\"378\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap1.png 671w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap1-300x169.png 300w\" sizes=\"(max-width: 671px) 100vw, 671px\" \/><\/a><\/p>\n<ul>\n<li> Initiate Git<\/li>\n<p># Initiate the repository<br \/>\n<code>git init<\/code>\n<\/ul>\n<p># Add the files to the repository and Commit the changes<\/p>\n<p><code>git commit -m \"first commit\"<\/code><\/p>\n<p># Add your Github repository link<br \/>\n# Example: git remote add origin git@github.com:[user name]\/[repo-name].git<br \/>\n<code>git remote add origin git@github.com:user1\/my_app.git<\/code><\/p>\n<p># Create an RSA\/SSH key.<br \/>\n<code>ssh-keygen -t rsa<\/code><\/p>\n<p>View the contents of the key and add it to your Github<br \/>\nby copy-and-pasting from the current remote session by<br \/>\nvisiting: https:\/\/github.com\/settings\/ssh<\/p>\n<p><code>cat \/root\/.ssh\/id_rsa.pub<\/code><\/p>\n<p>View the contents of the key and add it to your Github<br \/>\nby copy-and-pasting from the current remote session by<br \/>\nvisiting: https:\/\/github.com\/settings\/ssh<br \/>\nAlso Copy the contents of it to authorized_keys of user deployer.<\/p>\n<p><code>git push -u origin master<\/code><\/p>\n<ul>\n<li>Initiating Capistrano<\/li>\n<\/ul>\n<p><code>cap install<\/code><\/p>\n<p># mkdir -p config\/deploy<br \/>\n# create config\/deploy.rb<br \/>\n# create config\/deploy\/staging.rb<br \/>\n# create config\/deploy\/production.rb<br \/>\n# mkdir -p lib\/capistrano\/tasks<br \/>\n# Capified<\/p>\n<p>Configure Capistrano :<br \/>\nedit config\/deploy.rb and add the following content<br \/>\n<code>lock '3.2.1'<br \/>\nset :log_level, :debug<br \/>\nset :application, 'app'<br \/>\nset :scm, :git<br \/>\nset :repo_url, 'https:\/\/github.com\/\/.git'<br \/>\nset :branch, \"master\"<\/code><\/p>\n<p>set :deploy_to, &#8220;\/usr\/share\/nginx\/html&#8221;<\/p>\n<p>set :pty, true<br \/>\nset :format, :pretty<br \/>\nset :stages, [:staging, :production]<br \/>\nset :default_stage, :production<\/p>\n<p>Edit config\/deploy\/production.rb and add the following content :<\/p>\n<p><code>set :stage, :production<br \/>\nrole :app, %w{deployer@}<br \/>\nserver '', user: 'deployer', roles: %w{app}<br \/>\nset :ssh_options, {<br \/>\nforward_agent: false,<br \/>\nuser: 'deployer',<br \/>\nkeys: %w(\/root\/.ssh\/id_rsa),<br \/>\nauth_methods: %w(publickey password),<br \/>\n}<br \/>\n<\/code><br \/>\nCreate a rake file for php5-fpm restart :<\/p>\n<p>cat lib\/capistrano\/tasks\/php_restart.rake<br \/>\n<code>desc \"php5-fpm\"<br \/>\ntask :php5fpm_restart do<br \/>\non roles(:app) do |h|<br \/>\nif test(\"sudo service php5-fpm stop &amp;&amp; sudo service php5-fpm start\")<br \/>\ninfo \"restarted #{h}\"<br \/>\nelse<br \/>\nerror \"not restarted #{h}\"<br \/>\nend<br \/>\nend<br \/>\nend<\/code><\/p>\n<ul>\n<li>Deployment on Production :<br \/>\n<code>cap production deploy php5fpm_restart<\/code><\/li>\n<\/ul>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap6.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15897\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap6.png\" alt=\"cap6\" width=\"1297\" height=\"371\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap6.png 1297w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap6-300x85.png 300w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap6-1024x292.png 1024w\" sizes=\"(max-width: 1297px) 100vw, 1297px\" \/><\/a><\/p>\n<p>We can see the output on browser by typing public ip of EC2 instance.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap7.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15898\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap7.png\" alt=\"cap7\" width=\"1299\" height=\"212\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap7.png 1299w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap7-300x48.png 300w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap7-1024x167.png 1024w\" sizes=\"(max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<ul>\n<li>Now we want to deploy a new version of php app (say version 1.4)<\/li>\n<\/ul>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap8.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15899\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap8.png\" alt=\"cap8\" width=\"673\" height=\"288\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap8.png 673w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap8-300x128.png 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/a><\/p>\n<ul>\n<li>commit the changes and push it to Git.<\/li>\n<\/ul>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15900\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap2.png\" alt=\"cap2\" width=\"772\" height=\"136\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap2.png 772w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap2-300x52.png 300w\" sizes=\"(max-width: 772px) 100vw, 772px\" \/><\/a><\/p>\n<ul>\n<li>From Development Server , run this command in the directory where you have installed Capistrano.<br \/>\n<code><br \/>\ncap production deploy php5fpm_restart<\/code><\/li>\n<\/ul>\n<p>Copy EC2 instance public ip in browser&#8217;s address bar.<br \/>\nCheck the output on the browser.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap10.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15901\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap10.png\" alt=\"cap10\" width=\"1296\" height=\"188\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap10.png 1296w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap10-300x43.png 300w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap10-1024x148.png 1024w\" sizes=\"(max-width: 1296px) 100vw, 1296px\" \/><\/a><\/p>\n<ul>\n<li>If you want to rollback the deployment to its previous version<br \/>\n<code>cap production deploy:rollback php5fpm_restart<\/code><\/li>\n<\/ul>\n<p>You will be rolled back to previous version that is Version 1.3.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap13.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15902\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap13.png\" alt=\"cap13\" width=\"1305\" height=\"169\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap13.png 1305w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap13-300x38.png 300w, \/blog\/wp-ttn-blog\/uploads\/2014\/10\/cap13-1024x132.png 1024w\" sizes=\"(max-width: 1305px) 100vw, 1305px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>References :<\/p>\n<p>http:\/\/capistranorb.com\/<\/p>\n<p>https:\/\/github.com\/<\/p>\n<p>&nbsp;<\/p>\n<p>Thanks<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<br \/>\nIshant Kumar<br \/>\n<a href=\"http:\/\/aws.amazon.com\/certification\/certification-levels\/certified-solutions-architect-associate\/\" target=\"_blank\" rel=\"nofollow\">AWS Certified Solution Architect \u2013 Associate<\/a><br \/>\nAWS Administrator @ Intelligrape<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction : In this article we will see how we can automate the deployment using Capistrano, and how we can roll back to previous version if needed. Also in order to connect to deployment server we&#8217;ll use the key based authentication . What is Capistrano ? Capistrano is a remote server automation tool. It supports [&hellip;]<\/p>\n","protected":false},"author":125,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1174],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15917"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/125"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=15917"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15917\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=15917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=15917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=15917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}