📜 ⬆️ ⬇️

Capistrano: Remote Application Deployment

This article describes an example of using the Capistrano tool for remote deployment of applications. An example is an PHP application.

Capistrano is a tool that can perform tasks on a remote machine via ssh access. It is developed in Ruby and is widely used to publish Ruby on Rails applications. However, it can easily be used with other programming languages, such as PHP.

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

What do you need:


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

What do we have


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

Repository address
svn: // svnhost / trunk / myproject /
Repository user: mysvnuser

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

Training



First, install the necessary tool.

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

If I forgot something, look 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 project directory
$ 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


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


The capify command has created 2 files:
1. Capify, which is necessary capistrano; its minimum task is to load config / deploy.rb;
2. config / deploy.rb - a rubi script that stores instructions and capitrano settings.

We do not touch the Capify file, we make all changes in deploy.rb. Copy the code below into config / deploy.rb and modify it to fit your needs.

#
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


On the local machine, execute the command
$ cap deploy:setup

We connect via ssh to the web1234 server, actually ssh myuser @ web1234: 22, and it creates the directories it needs in / home / myuser / myproject / deploy:
** releases,
** shared

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

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

The release_path variable contains the path to the current release of the application.

Change the application code a little (for example, in index.php), commit the changes and repeat the command
$ cap deploy:update
Capistrano will create a new directory, releases / 20090114203040, and make svn checkout from the repository. Then 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 task deploy: update is performed by the 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 in the database, etc. For real projects you have to explore capistrano yourself.

Source: https://habr.com/ru/post/49127/


All Articles