Sunday, May 15, 2011

Install Capistrano for work with PHP, MySQL and Apache

Goals
Use Capistrano with PHP+MySQL+Apache+SVN (LAMP).
Why? For optimization your work when you deploying project.
We will be import project from SVN-repository without '.svn'-files to staging server.
 Use only one command for deploy)

 Start configuration
  1. For view version of the capistrano use: cap --version
  2. Go to in the root of the application, and enter: "capify .". This will create two files: "./Capfile" and "./config/deploy.rb"
    Example:
    cd /home/example/public_html/
    capify . 
  3. Configuration "conf/deploy.rb"
    Set name application (staging):
    set :application, "staging"
    Set path to SVN repository (svn://svn.yoursite.com/staging):
    set :repository, "svn://svn.yoursite.com/staging"
    Set role:
    role :web, "staging.yoursite.com"
    Set path for deploy (/home/staging/public_html)
    set :deploy_to, "/home/#{application}/public_html"
    Set type deploy from SVN
    set :checkout, "export"
    You can add new task, example "deploy:deploy", in other words "cap deploy:deploy"
    namespace :deploy do
    desc "This will deploy your site wo '.svn'-file and copy images files from previous repository"
    task "deploy" do
    run "svn --quiet #{checkout} #{repository} #{release_path}"
    run "ln -nfs #{release_path} #{current_path}"
    run "mkdir -m 777 #{release_path}/files/"
    run "cp -r #{previous_release}/files/* #{release_path}/files/"
    end
    end
  4. For create default capistrano directory structure use: "cap deploy:setup". This will create the following directory structure:
    (deploy_to)/
    (deploy_to)/releases
    (deploy_to)/shared
    (deploy_to)/shared/system
    (deploy_to)/shared/pids
    (deploy_to)/shared/log


    Example:
    cd /home/example/public_html/
    cap deploy:setup
  5. For view deploy commands use: cap -vT
    Example:
    cap deploy # Deploys your project.
    cap deploy:check # Test deployment dependencies.
    cap deploy:cleanup # Clean up old releases.
    cap deploy:cold # Deploys and starts a `cold' application.
    cap deploy:deploy # This will deploy your site and copy files
    cap deploy:finalize_update # [internal] Touches up the released code.
    cap deploy:migrate # Run the migrate rake task.
    cap deploy:migrations # Deploy and run pending migrations.
    cap deploy:pending # Displays the commits since your last deploy.
    cap deploy:pending:diff # Displays the `diff' since your last deploy.
    cap deploy:restart # Restarts your application.
    cap deploy:rollback # Rolls back to a previous version and restarts.
    cap deploy:rollback:cleanup # [internal] Removes the most recently deployed ...
    cap deploy:rollback:code # Rolls back to the previously deployed version.
    cap deploy:rollback:revision # [internal] Points the current symlink at the p...
    cap deploy:setup # Prepares one or more servers for deployment.
    cap deploy:start # Start the application servers.
    cap deploy:stop # Stop the application servers.
    cap deploy:symlink # Updates the symlink to the most recently deplo...
    cap deploy:update # Copies your project and updates the symlink.
    cap deploy:update_code # Copies your project to the remote servers.
    cap deploy:upload # Copy files to the currently deployed version.
    cap deploy:web:disable # Present a maintenance page to visitors.
    cap deploy:web:enable # Makes the application web-accessible again.
    cap invoke # Invoke a single command on the remote servers.
    cap shell # Begin an interactive Capistrano session.

    Extended help may be available for these tasks.
     Type `cap -e taskname' to view it.
  6. Apache VirtualHost config
    Set document root to 'current'-path
    DocumentRoot /home/staging/public_html/current

    Allow symbolic links
    Options FollowSymLinks


    AllowOverride All
    allow from all
  7. How use?
    Open putty (need ssh access).
    Change the directory to your site.
    Run capistrano.
    cd /home/staging/public_html/
    cap deploy:deploy

No comments:

Post a Comment