Capistrano: Remote Application Deployment

    This article describes an example of using the Capistrano tool to deploy applications remotely. An example is an application in PHP.

    Capistrano is a tool that can perform tasks on a remote machine via ssh access. It was developed in the Ruby language and is widely used for publishing Ruby on Rails applications. However, it can be easily used together with other programming languages, for example, PHP.

    For more detailed familiarization and practical use, you should read additional documentation and practice. I just want to demonstrate the capabilities of capistrano.

    What do you need:


    1. the presence of ssh-access to the hosting on which the application will be published;
    2. a project in any version control system; default is subversion ;
    3. the local developer's machine (that is, with which remote publishing is carried out) should work under Linux, Mac OS or BSD , Windows will not work.

    What do we have


    PHP application with the structure
    myproject /
    ** lib /
    ** config /
    ** public /
    **** index.php

    Address in the
    svn repository : // svnhost / trunk / myproject /
    Repository user: mysvnuser

    Hosting
    1. host: web1234
    2 .user: myuser
    3. hosting project directory: / home / myuser / myproject
    4. website root web directory: / home / myuser / myproject / public_html

    Training



    First, install the necessary tool.

    On the local machine we install (for ubuntu):
    1. chop: sudo apt-get install ruby ​​rubygems
    2. capistrano: sudo gem install capistrano,
    3. svn client: sudo apt-get install subversion

    If I forgot something, see here .

    On the remote server, create the directory / home / myuser / myproject / deploy, where the project will be extracted from the repository.

    Go



    We do everything on the local machine.

    Go to the directory with the project. Run the “capify.” Command . The capify command created 2 files: 1. Capify, which is necessary for capistrano; its minimal task is to load config / deploy.rb; 2. config / deploy.rb - a ruby ​​script that stores instructions and settings for capistrano. We do not touch the Capify file; we make all changes in deploy.rb. Copy the code below to config / deploy.rb and modify it to your needs. On the local machine, we execute the Kapistrano command, we connect via ssh to the web1234 server, in fact ssh myuser @ web1234: 22, and creates the directories it needs in / home / myuser / myproject / deploy: ** releases, ** shared
    $ cd /var/www/myproject
    $ ll
    drwxr-xr-x 2 alex alex 4096 2009-01-14 20:56 config
    drwxr-xr-x 2 alex alex 4096 2009-01-14 20:56 lib
    drwxr-xr-x 2 alex alex 4096 2009-01-14 20:56 public



    $ capify .
    [add] writing `./Capfile'
    [add] writing `./config/deploy.rb'
    [done] capified!








    # Имя приложения
    set :application, "myproject"
    # Вызов функции set создаст переменную application и присвоит ей значение "myproject"

    # В проекте мы используем только один сервер, выполняющий роль веб-сервиса.
    role :web, "web1234" # хост удалённого сервера
    set :user, 'myuser' # пользователь удалённого сервера
    set :use_sudo, false # не запускать команды под sudo

    # Директория приложения на удалённом хостинге
    set :app_dir, "/home/#{user}/#{application}/" # /home/myuser/myproject/
    # Запись "/home/#{user}/#{application}/" аналогична "/home/$user/$application/" в PHP

    # Директория, куда будет делаться checkout из репозитория
    set :deploy_to, "#{app_dir}/deploy" # /home/myuser/myproject/deploy

    # Настройки репозитория
    set :scm, :subversion # используем subversion
    set :scm_user, 'mysvnuser' # имя пользователя репозитория
    set :scm_url, "svn://svnhost/trunk/#{application}" # svn://svnhost/trunk/myproject/
    # Формируем команду svn checkout --username mysvnuser svn://host/trunk/myproject
    set :repository, Proc.new { "--username #{scm_user} #{scm_url}"}

    task :after_symlink, :roles => :web do
    run "ln -nfs #{release_path}/public #{app_dir}/public_html"
    end



    $ cap deploy:setup





    We
    $ cap deploy:update
    execute the command Kapistrano executes the following commands on the remote server:
    1. creates a new directory in / home / myuser / myproject / deploy / releases , for example, releases / 20090114102030
    2. does svn checkout svn: // svnhost / trunk / myproject / in this directory
    3. creates a symlink / home / myuser / myproject / deploy / current on / home / myuser / myproject / deploy / releases / 20090114102030
    4. creates a symlink / home / myuser / myproject / public_html on / home / myuser / myproject / deploy / releases / 20090114102030 / public

    Point 4 is implemented in lines The variable release_path contains the path to the current release of the application.
    task :after_symlink, :roles => :web do
    run "ln -nfs #{release_path}/public #{app_dir}/public_html"
    end



    Change the application code a bit (for example, in index.php), commit the changes and repeat the command
    $ cap deploy:update
    Kapistrano will create a new directory releases / 20090114203040 and make svn checkout from the repository in it. Then it will install the deploy / current symlink on deploy / releases / 20090114203040 / , and publick_html on deploy / releases / 20090114203040 / public .

    You will already have this directory structure:
    deploy /
    ** releases
    **** 20090114102030
    ******** lib /
    ******** config /
    ******** public /
    ** ********** index.php
    **** 20090114203040
    ******** lib /
    ******** config /
    ******** public /
    ************ index.php
    ** current -> releases / 20090114203040
    ** shared
    public_html -> deploy / releases / 20090114203040 / public

    It should be noted that the deploy: update task is performed by a capistrano in a transaction.

    This is the minimum that is needed for remote publication of the application. In this example, you do not need to restart the web server (or several servers), make changes to the database, etc. For real projects, you have to study capistrano yourself.

    Also popular now: