Friday, May 16, 2014

Deploy code in php

Deploy Code in php:

For deploy we will in need of install Capistrano which is a tool for deployment and also some of its essential package.

we can use aptitude or sudo apt-get for install .

aptitude install -y build-essential
aptitude install -y cvs subversion git-core libyaml-dev mercurial
curl -L get.rvm.io | bash -s stable  
source /etc/profile.d/rvm.sh
rvm reload
rvm install 2.1.0   
gem install capistrano --no-ri --no-rdoc
After this we have to create user at deployment server and have to add this in a group.

sudo addgroup www

# Create a new user:
# Usage: sudo adducer [user name]
sudo adduser deployer

# Follow on-screen instructions to user-related
# information such as the desired password.

# Add the user to an already existing group:
# Usage: sudo adducer [user name] [group name]
sudo adduser deployer www

Now give the permission to sudoers

# User privilege specification
root     ALL=(ALL:ALL) ALL
deployer ALL=(ALL:ALL) ALL


For permission we have to set this

# Set the ownership of the folder to members of `www` group
sudo chown -R :www  /var/www

# Set folder permissions recursively
sudo chmod -R g+rwX /var/www

# Ensure permissions will affect future sub-directories etc.
sudo chmod g+s      /var/www
Now on our server we have to set git and have to push data

# !! These commands are to be executed on
#    your development machine, from where you will
#    deploy to your server.
#    Instructions might vary slightly depending on
#    your choice of operating system.

# Initiate the repository
git init

# Add all the files to the repository
git add .

# Commit the changes
git commit -m "first commit"

# Add your Github repository link 
# Example: git remote add origin git@github.com:[user name]/[proj. name].git
git remote add origin git@github.com:user123/my_app.git

# Create an RSA/SSH key
# Follow the on-screen instructions
ssh-keygen -t rsa

# View the contents of the key and add it to your Github
# by copy-and-pasting from the current remote session by
# visiting: https://github.com/settings/ssh
# To learn more about the process,
# visit: https://help.github.com/articles/generating-ssh-keys
cat /root/.ssh/id_rsa.pub

# Set your Github information
# Username:
# Usage: git config --global user.name "[your username]"
# Email:
# Usage: git config --global user.email "[your email]"
git config --global user.name  "user123"    
git config --global user.email "user123@domain.tld"

# Push the project's source code to your Github account
git push -u origin master
Now initiate Capistrano and edit in deploy.rb and production.rb files

cap install

# mkdir -p config/deploy
# create config/deploy.rb
# create config/deploy/staging.rb
# create config/deploy/production.rb
# mkdir -p lib/capistrano/tasks
# Capified
nano config/deploy.rb
# !! When editing the file (or defining the configurations),
#    you can either comment them out or add the new lines.
#    Make sure to **not** to have some example settings
#    overriding the ones you are appending.

# Define the name of the application
set :application, 'my_app'

# Define where can Capistrano access the source repository
# set :repo_url, 'https://github.com/[user name]/[application name].git'
set :scm, :git
set :repo_url, 'https://github.com/user123/my_app.git'

# Define where to put your application code
set :deploy_to, "/var/www/my_app"

set :pty, true

set :format, :pretty

# Set your post-deployment settings.
# For example, you can restart your Nginx process
# similar to the below example.
# To learn more about how to work with Capistrano tasks
# check out the official Capistrano documentation at:
# http://capistranorb.com/

# namespace :deploy do
#   desc 'Restart application'
#   task :restart do
#     on roles(:app), in: :sequence, wait: 5 do
#       # Your restart mechanism here, for example:
#       sudo "service nginx restart"
#     end
#   end
# end
nano config/deploy/production.rb

 
# Define roles, user and IP address of deployment server
# role :name, %{[user]@[IP adde.]}
role :app, %w{deployer@162.243.74.190}

# Define server(s)
# Example:
# server '[your droplet's IP addr]', user: '[the deployer user]', roles: %w{[role names as defined above]}
# server '162.243.74.190', user: 'deployer', roles: %w{app}
server '162.243.74.190', user: 'deployer', roles: %w{app}

# SSH Options
# See the example commented out section in the file
# for more options.
set :ssh_options, {
    forward_agent: false,
    auth_methods: %w(password),
    password: 'user_deployers_password',
    user: 'deployer',
}   
and then send it to production

cap production deploy

No comments:

Post a Comment