diff --git a/manuscript/capistrano_howto.md b/manuscript/capistrano_howto.md index fd7a56d..446e373 100644 --- a/manuscript/capistrano_howto.md +++ b/manuscript/capistrano_howto.md @@ -1,244 +1,258 @@ - -# Prepare Deploy Server -- Create one user and add it to group: - -**sudo groupadd www** - -- Create a new user and add it to this group. - -**sudo adduser deployer** - - -- Add the user to an already existing group: - -```sudo usermod -a -G nginx deployer``` - -Append the following right after - -root ALL=(ALL) ALL in **/etc/sudoers file**. - -**deployer ALL=(ALL:ALL) NOPASSWD:ALL** - - -- Install and configure php5, php5-fpm and nginx - -**sudo yum install nginx** - -**sudo yum install php5** - -**sudo yum install php5-fpm** - -- Set the directory permissions to make it access for the Nginx. -- - Set the ownership of the folder to members of `www` group - -```chgrp -R nginx /usr/share/nginx/html``` - - Set folder permissions recursively - -**sudo chmod -R g+rwX /usr/share/nginx/html** - - Ensure permissions will affect future sub-directories etc. - -**sudo chmod g+s /usr/share/nginx/html** - -- nginx configuration : - -``` - - server { - - listen 80 default_server; - - listen [::]:80 default_server ipv6only=on; - - root /usr/share/nginx/html/current; - - index index.html index.php index.htm; - - # Make site accessible from http://localhost/ - - server_name localhost; - - - location / { - - # First attempt to serve request as file, then - - # as directory, then fall back to displaying a 404. - - try_files $uri $uri/ =404; - - # Uncomment to enable naxsi on this location - - #include /etc/nginx/naxsi.rules - - } - - location ~ \.php$ { - - fastcgi_split_path_info ^(.+\.php)(/.+)$; - - fastcgi_pass unix:/var/run/php5-fpm.sock; - - fastcgi_index index.php; - - include fastcgi_params; - - } - -``` - -- Set fix_path_info=0 in php.ini - - - -###Now move back to Development Server. - -- GoTo the directory where your application source code is located. - -**cd /home/vagrant/myapp/** - -- create a file index.php -- -``` - - -

Sample PHP App

-

Version 1.0

- Current date and time => " . date("r") . "

"; - ?> - - - -``` - -- Initiate Git - -Initiate the repository - -**git init** - -Add the files to the repository and Commit the changes - -**git add .** - - -**git commit -m "first commit"** - -Add your Github repository link - -Example: git remote add origin git@github.com:[user name]/[repo-name].git - -**git remote add origin git@github.com:user1/myapp.git** - -push changes - -**git push origin master** - -- Initiating Capistrano - -move inside your app directory & hit command - -```cap install``` - -Output: -``` -mkdir -p config/deploy -create config/deploy.rb -create config/deploy/staging.rb -create config/deploy/production.rb -mkdir -p lib/capistrano/tasks -create Capfile -Capified -``` - - -- Configure Capistrano : - -edit **config/deploy.rb** and add the following content -``` - - set :log_level, :debug - - set :application, 'myapp' - - set :scm, :git - - set :repo_url, 'https://github.com//.git' - - set :branch, "master" - - set :deploy_to, “/usr/share/nginx/html” - - set :pty, true - set :format, :pretty - set :stages, [:staging, :production] - set :default_stage, :production - -``` - -- Edit config/deploy/production.rb and add the following content : - -``` - set :stage, :production - - role :app, %w{deployer@} - - server '', user: 'deployer', roles: %w{app} - - set :ssh_options, { - - forward_agent: false, - - user: 'deployer', - - keys: %w(/root/.ssh/id_rsa), - - auth_methods: %w(publickey password), - } - -``` - -- Create a rake file for php5-fpm restart : - - -**cat lib/capistrano/tasks/php_restart.rake** - -``` -desc "php5-fpm" - -task :php5fpm_restart do - -on roles(:app) do |h| - -if test("sudo service php5-fpm stop && sudo service php5-fpm start") - -info "restarted #{h}" - -else - -error "not restarted #{h}" - -end - -end - -end - -``` - -- Deployment on Production : - -**cap production deploy php5fpm_restart** - -- We can see the output on browser by typing public ip of server. - -- Now we want to deploy a new version of php app (say version 1.1) - -repeat procedure for & deploy , see the output - -- you can revert back changes using command - -**cap production deploy:rollback php5fpm_restart** +--- +#Work With Capistrano :- +--- +# Prepare Deploy Server:- +- Create one user and add it to group: + +```sudo groupadd www``` + +- Create a new user and add it to this group. + +```sudo adduser deployer``` + + +- Add the user to an already existing group: + +```sudo usermod -a -G nginx deployer``` + +Append the following right after + +```root ALL=(ALL) ALL in **/etc/sudoers file**.``` + +```**deployer ALL=(ALL:ALL) NOPASSWD:ALL**``` + + +- Install and configure php5, php5-fpm and nginx + +```**sudo yum install nginx**``` + +```**sudo yum install php5**``` + +```**sudo yum install php5-fpm**``` + +###- Set the directory permissions to make it access for the Nginx. + +Set the ownership of the folder to members of `www` group + +```chgrp -R nginx /usr/share/nginx/html``` + +Set folder permissions recursively + +```**sudo chmod -R g+rwX /usr/share/nginx/html**``` + +Ensure permissions will affect future sub-directories etc. + +```**sudo chmod g+s /usr/share/nginx/html**``` + +###- nginx configuration + +``` + + server { + + listen 80 default_server; + + listen [::]:80 default_server ipv6only=on; + + root /usr/share/nginx/html/current; + + index index.html index.php index.htm; + + # Make site accessible from http://localhost/ + + server_name localhost; + + + location / { + + # First attempt to serve request as file, then + + # as directory, then fall back to displaying a 404. + + try_files $uri $uri/ =404; + + # Uncomment to enable naxsi on this location + + #include /etc/nginx/naxsi.rules + + } + + location ~ \.php$ { + + fastcgi_split_path_info ^(.+\.php)(/.+)$; + + fastcgi_pass unix:/var/run/php5-fpm.sock; + + fastcgi_index index.php; + + include fastcgi_params; + + } + +``` + +- Set fix_path_info=0 in php.ini + + + +##Now move back to Development Server:- + +- GoTo the directory where your application source code is located. + +```**cd /home/vagrant/myapp/**``` + +###- create a file index.php + +``` + + +

Sample PHP App

+

Version 1.0

+ Current date and time => " . date("r") . "

"; + ?> + + + +``` + +###- Initiate Git + +- .Initiate the repository + +```git init`` + +- Add the files to the repository and Commit the changes + +```git add .``` + +- git commit + + +**```git commit -m "first commit"```** + +- Add your Github repository link + +Example: git remote add origin git@github.com:[user name]/[repo-name].git + +**```git remote add origin git@github.com:user1/myapp.git```** + +- push changes + +**```git push origin master```** + +###- Initiating Capistrano + +move inside your app directory & hit command + +```cap install``` + +**Output:** + +``` +mkdir -p config/deploy + +create config/deploy.rb + +create config/deploy/staging.rb + +create config/deploy/production.rb + +mkdir -p lib/capistrano/tasks + +create Capfile + +Capified +``` + + +###- Configure Capistrano + +edit **config/deploy.rb** and add the following content +``` + + set :log_level, :debug + + set :application, 'myapp' + + set :scm, :git + + set :repo_url, 'https://github.com//.git' + + set :branch, "master" + + set :deploy_to, “/usr/share/nginx/html” + + set :pty, true + set :format, :pretty + set :stages, [:staging, :production] + set :default_stage, :production + + + +- Edit config/deploy/production.rb and add the following content : + +``` + set :stage, :production + + role :app, %w{deployer@} + + server '', user: 'deployer', roles: %w{app} + + set :ssh_options, { + + forward_agent: false, + + user: 'deployer', + + keys: %w(/root/.ssh/id_rsa), + + auth_methods: %w(publickey password), + } + +```` + +Create a rake file for php5-fpm restart : + +**```cat lib/capistrano/tasks/php_restart.rake```** + + + desc "php5-fpm" + + task :php5fpm_restart do + + on roles(:app) do |h| + + if test("sudo service php5-fpm stop && sudo service php5-fpm start") + + info "restarted #{h}" + + else + + error "not restarted #{h}" + + end + + end + + end + + +###- Deployment on Production + +**```cap production deploy php5fpm_restart```** + +- We can see the output on browser by typing public ip of server. + +- Now we want to deploy a new version of php app (say version 1.1) + + repeat procedure for & deploy , see the output + +- you can revert back changes using command + +**```cap production deploy:rollback php5fpm_restart```** + +##Workflow model for capistrano:- + + +![](https://github.com/ashwini9860/images/raw/master/cap-model.png) \ No newline at end of file diff --git a/manuscript/capistrano_ror.md b/manuscript/capistrano_ror.md index a637803..409260b 100644 --- a/manuscript/capistrano_ror.md +++ b/manuscript/capistrano_ror.md @@ -1,100 +1,68 @@ -###example 2 +#Deploy with ruby rail app:- ruby rails application deployment using capistrano:- -**deployment server:-** +##Deployment server +- update system:- -Preparing The Deployment Serve:-r + yum -y update +- Install the bundle containing development tools : -- Updating And Preparing The Operating System + yum groupinstall -y 'development tools' -- Setting Up Ruby Environment and Rails + - EPEL repository: -- Downloading And Installing App. & HTTP Servers + sudo su -c 'rpm -Uvh88 http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm' -- Creating The Nginx Management Script + yum -y update -- Configuring Nginx For Application Deployment +- install some additional libraries and tools:- -- Downloading And Installing Capistrano + yum install -y curl-devel nano sqlite-devel libyaml-devel -- Creating A System User For Deployment +- Setting Up Ruby Environment and Rails:- -**development server:-** + curl -L get.rvm.io | bash -s stable -- nstalling Capistrano Inside The Project Directory + source /etc/profile.d/rvm.sh -- Working With config/deploy.rb Inside The Project Directory + rvm reload -- Working With config/deploy/production.rb Inside The Project Directory + rvm install 2.1.0 -- Deploying To The Production Server -- git repository +- Rails needs a JavaScript interpreter, run the following to download and install nodejs:- -###deployment server + yum install -y nodejs -update system:- +- using RubyGems' gem to download and install rails:- -**yum -y update** + gem install bundler rails -Install the bundle containing development tools : +- swaping ig server size less than 1GB:- + Create a 1024 MB SWAP space: -**yum groupinstall -y 'development tools'** + sudo dd if=/dev/zero of=/swap bs=1M count=1024 - EPEL repository: + sudo mkswap /swap -**sudo su -c 'rpm -Uvh88 http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm'** + sudo swapon /swap -**yum -y update** +- download and install passenger:- -install some additional libraries and tools:- -**yum install -y curl-devel nano sqlite-devel libyaml-devel** + gem install passenger -Setting Up Ruby Environment and Rails:- +- Run the following to start compiling Nginx with native Passenger module:- -**curl -L get.rvm.io | bash -s stable** - -**source /etc/profile.d/rvm.sh** - -**rvm reload** - -**rvm install 2.1.0** - -Rails needs a JavaScript interpreter, run the following to download and install nodejs:- - -**yum install -y nodejs** - -using RubyGems' gem to download and install rails:- - -**gem install bundler rails** - -swaping ig server size less than 1GB:- - -Create a 1024 MB SWAP space: - -**sudo dd if=/dev/zero of=/swap bs=1M count=1024** - -**sudo mkswap /swap** - -**sudo swapon /swap** - -download and install passenger:- - - -**gem install passenger** - -Run the following to start compiling Nginx with native Passenger module:- - -**passenger-install-nginx-module** + passenger-install-nginx-module select language, then option 1 to download & continue -create nginx script: +- create nginx script: **nano /etc/rc.d/init.d/nginx** @@ -255,17 +223,16 @@ create nginx script: ``` -set permission:- +- set permission:- -**chmod +x /etc/rc.d/init.d/nginx** + chmod +x /etc/rc.d/init.d/nginx -configure nginx for deployment:- +- configure nginx for deployment:- -**nano /opt/nginx/conf/nginx.conf** + **nano /opt/nginx/conf/nginx.conf** -these line add in the file:- -``` - passenger_app_env development; + these line add in the file:- +passenger_app_env development; location / { @@ -280,38 +247,37 @@ these line add in the file:- passenger_enabled on; -``` -restart nginx:- +- restart nginx:- -**/etc/init.d/nginx restart** + etc/init.d/nginx restart -###development server:- +##Development server:- -Create a sample Rails application:- +- Create a sample Rails application:- **rails new my_app** -Enter the application directory:- + Enter the application directory:- -**cd my_app** + cd my_app -Create a sample resource:- +- Create a sample resource:- -**rails generate scaffold Task title:string note:text** + rails generate scaffold Task title:string note:text -Create a sample database:- +- Create a sample database:- -**RAILS_ENV=development rake db:migrate** + RAILS_ENV=development rake db:migrate -test application set correctly: +- test application set correctly: -Enter the application directory +- Enter the application directory -**cd my_app** + cd my_app -Run a simple server +- Run a simple server -**rails s** + rails s You should now be able to access it by @@ -321,38 +287,38 @@ In order to terminate the server process **Press CTRL+C** -create git repo:- +- create git repo:- -**git init** + git init -**git add .** + git add . -**git commit -m "..."** + git commit -m "..." -**git remote url add** + git remote url add -**git push origin master** + git push origin master -capistrano installation inside app dir:- +- capistrano installation inside app dir:- -**cap install** + cap install it will created following file structure for you - **mkdir -p config/deploy** + mkdir -p config/deploy - **create config/deploy.rb** + create config/deploy.rb - **create config/deploy/staging.rb** + create config/deploy/staging.rb -**create config/deploy/production.rb** + create config/deploy/production.rb -**mkdir -p lib/capistrano/tasks** + mkdir -p lib/capistrano/tasks -**Capifile** + Capifile -configure deploy.rb:- +- configure deploy.rb:- **nano config/deploy.rb** @@ -404,13 +370,12 @@ configure deploy.rb:- # end # end - ``` -configuration productio.rb:- +- configure production.rb:- **nano config/deploy/production.rb** -``` + # Define roles, user and IP address of deployment server # role :name, %{[user]@[IP adde.]} @@ -431,8 +396,8 @@ configuration productio.rb:- user: 'deployer', } -``` -deploy now:- -**cap production deploy** +- deploy now:- + + cap production deploy diff --git a/manuscript/deploy_html_site.md b/manuscript/deploy_html_site.md index 3210279..37aefc1 100644 --- a/manuscript/deploy_html_site.md +++ b/manuscript/deploy_html_site.md @@ -5,7 +5,6 @@ Change into directory where your application source code is located. ``` - cd /path/to/myapp/ ``` @@ -20,14 +19,15 @@ Create index.html ``` Add and commit to git repo on Github + ``` -git add index.html +- git add index.html -git commit -a -m "adding index page" +- git commit -a -m "adding index page" -git push origin master +- git push origin master or -git push github master (if your remote is called github) +- git push github master (if your remote is called github) ``` @@ -56,17 +56,27 @@ edit **config/deploy.rb** and add the following content # -*- coding: utf-8 -*- set :log_level, :info + set :application, 'myapp' + set :scm, :git -set :repo_url, '' + +set :repo_url, <"REPLACE_THIS_WITH_YOUR_REPO_URL"> + set :branch, "master" + set :deploy_to, "/usr/share/nginx/html" + set :stages, [:staging, :production] + set :default_stage, :production -``` +edit:- -- Edit config/deploy/production.rb and add the following content : +``` +- config/deploy/production.rb +``` +and add the following content : ``` set :stage, :production diff --git a/manuscript/deploy_php_app.md b/manuscript/deploy_php_app.md index a19e837..3dab2ce 100644 --- a/manuscript/deploy_php_app.md +++ b/manuscript/deploy_php_app.md @@ -98,7 +98,11 @@ e.g. http://192.168.5.10/info.php This should show you a sample PHP page. If not you will have to look at nginx logs or php5-php-fpm logs and debug the issue. **Possible Issues: ** - * php5-php-fpm is not installed/started. Logs for php5-php-fpm installed using remi are located in /opt/remi/php55/root/var/log/php-fpm/ directory + + * php5-php-fpm is not installed/started. + +- Logs for php5-php-fpm installed using remi are located in /opt/remi/php55/root/var/log/php-fpm/ directory + * nginx is not configured properly to handle php requests or to hand off to php5-php-fpm. Check the configs inside /etc/nginx/sites-enabled/*.conf ## Deploy PHP App @@ -190,3 +194,6 @@ git push origin master #### Rollback to previous version ``` cap production deploy:rollback nginx_restart ``` + +###Capistrano workflow model:- +![](https://github.com/ashwini9860/images/raw/master/cap-model.png)SS diff --git a/manuscript/install_capistrano.md b/manuscript/install_capistrano.md index 3fe4499..6d70e91 100644 --- a/manuscript/install_capistrano.md +++ b/manuscript/install_capistrano.md @@ -1,8 +1,9 @@ +---- # CAPISTRANO - +---- Capistrano is a remote server automation tool.It supports the scripting and execution of arbitrary tasks, and includes a set of sane-default deployment workflows. -## Installing Capistrano on Centos 6.7 +## Installing Capistrano on Centos 6.7:- Capistrano requires ruby version >=2.x.x to be installed @@ -20,44 +21,46 @@ yum -y install patch libyaml-devel libffi-devel glibc-headers autoconf gcc-c++ g ### Installing Ruby with RVM - -#### Add rvm signature -``` +- #### Add rvm signature command curl -sSL https://rvm.io/mpapis.asc | sudo gpg2 --import - curl -L get.rvm.io | bash -s stable -``` -#### Load rvm + +- #### Load rvm ``` source /etc/profile.d/rvm.sh ``` -#### Install Ruby using rvm +- #### Install Ruby using rvm ```rvm install 2.1.4``` -#### Verify ruby version +- #### Verify ruby version ```ruby -v``` ### Install Capistrano using gem +- install capistrano + ``` gem install capistrano ``` -deploy nonrail app with Capistrano +- deploy nonrail app with Capistrano ``` gem install railsless-deploy ``` -Install capistrano extensions +- Install capistrano extensions ```gem install capistrano-ext``` #### Create ssh keypair +For secure login to production/staging server use ssh key pair. to generate ssh key use below command. + ``` ssh-keygen -t rsa diff --git a/manuscript/troubleshooting_issues.md b/manuscript/troubleshooting_issues.md index e69de29..31f14f0 100644 --- a/manuscript/troubleshooting_issues.md +++ b/manuscript/troubleshooting_issues.md @@ -0,0 +1,13 @@ +#Troubleshooting Method:- +###Which checks are performed If at primary level? +--- +- checks that you're using git as a scm +- checks that ssh private key file exists locally +- checks if ssh-agent process is running locally +- checks that ssh-add process can communicate with ssh-agent +- checks that ssh private keys are loaded to ssh-agent +- checks that remote code repository is accessible from local machine +- checks passwordless ssh login is used for all servers +- checks forward_agent capistrano option is set to true for all servers +- checks ssh-agent is actually forwared to all the servers +- checks remote code repository is accessible from all the servers \ No newline at end of file