📜 ⬆️ ⬇️

Capifony. Or deploy symfony project through Capistrano

I’ve been programming PHP for a year now using the symfony framework, and I’m finding it a real pleasure. However, there are some site development processes that this framework does not fully cover, and is not obliged =)
One such process is deployment or deployment and update of a project on a production server. To perform such a routine operation, many scripts have been written and one of the most popular is Capistrano . It is extremely easy to learn, functionally perfect and extremely flexible in customization, however, it is sharpened out of the box under the deployment of RoR applications, which is why it was created.
Today I will try to tell you how to use Capistrano for deployment of symfony projects.

What is capify?


Capistrano is a ruby ​​library that performs a routine for rolling out a site and provides flexible means for timely rollback of a project to a previous version (previous deployment).
Capistrano, by connecting via SSH, operates with remote directories, creating a new directory with the current code and creating the necessary symlinks . The current Capistrano code can copy from any other machine via an SSH server, use the GIT or SVN repositories. Capify does not end there. In principle, the script, if properly configured, is able to execute any set of unix commands on any number of remote systems (to which you have access, of course). However, the greatest number of scripts and settings Capistrano has mainly for remote deployment of applications, which we are talking about today.
capify - command for creating a Capistrano config

Standard Deployment Process


The implementation of the deployment process is divided into atomic operations-tasks, each of which is responsible for a separate aspect of rollout. In this case, the global execution of the deployment behaves as a full-fledged transaction, and if at least one operation is not performed, the whole process is canceled, and the remote file system returns to the state before the deployment.
Basic commands for capistrano:
  1. capify . - creates in the current directory files for the configuration of access to a remote system and setting up the process. These files:
    1. Capfile - the main script, tightened by the command "capify"
    2. config / deploy.rb - process settings, loaded into Capfile. It is in this file that you need to register all our paths to the servers and settings before executing the following commands.

  2. cap deloy:setup - deploys on a remote server / servers (there may be several) the file structure for subsequent deployment processes
  3. cap deploy - performs deployment. At the same time, a new directory for the project is created, symlinks are made in it on shared (shared) resources and then, a symlink of current is made on this directory, slipping the actual code to the web server
  4. cap rollback - roll back to the previous deployment. At the same time, the directory with the current version is deleted (with a hard-core rollback), and the current symlink is interrupted by the previous deployment directory.


Why do you need capifony?


As I said, capistrano was originally created for deploying RoR applications and there are some places that are tied to it (out of the box) for this framework. For example, the structure and list of shared folders in Rails and symfony projects are different. When deploying a symfony project, you also need to do some tasks, such as symfony plugin:publish-assets to create symlinks for plug-in assets, symfony cc to reset the cache, and symfony fix-perms to restore permishenes to cache and / or web uploads folders.
To solve all these problems, I slightly expanded the base deployment scripts of capistrano, which gave birth to capifony
Download the script and take part in its development here: http://github.com/everzet/capifony/tree/master .
')

Using


So, we have a project that needs to be deployed on a production server. This project is stored in a remote git repository on the production server (it may not be git and the code can be stored anywhere, where there is SSH access and in any form that Capistrano knows, but he knows a lot =)).
First, we move to the local directory with the project that needs to be uploaded and the remote git repository of which is located on the server:
  1. At the root, we execute the capify capify . that creates Capfile in the root and deploy.rb in config /
  2. Download the file capifony.rb from the github and put it in the config /
  3. At the end of Capfile we add load 'config/capifony'
  4. And the final touch - we register the connection settings to the remote server in deploy.rb:

    set :application, "YOUR_APPLICATION_NAME" <br> set :repository, "YOUR_SSH_SERVER_NAME:/PATH/TO/repos/#{application}.git" <br> set :deploy_to, "/PATH/TO/www/#{application}.com" <br> set :scm, "git" <br><br>default_run_options[:pty] = true <br>ssh_options[:forward_agent] = true <br><br>server "YOUR_SSH_SERVER_NAME" , :web, :app, :db <br><br> * This source code was highlighted with Source Code Highlighter .


  5. Now we call cap deploy:setup from the console to set up the remote file structure and cap deploy to execute the deployment. And do not forget, if something suddenly goes wrong with subsequent deployments (it has gone through, but the site has fallen, for example) - cap rollback will help


It should be noted that the log / , web / uploads directories do not participate in deployment, since are shared resources that are stored between deployments. Therefore, if you have already accumulated a set of files for web / uploads, you need to manually upload them to the server at /PATH/TO/www/#{application}.com/shared/web/uploads/
That's all. If you liked this article - let me know, maybe next time I will talk about another interesting aspect of development along with a great symfony ...

upd: I forgot 1 thing. According to the standard, when symfony generates a project, it sets in the config / ProjectConfiguration.class.php the absolute path to symfony, which may differ on the working machine and production. To solve the problem both on the production and on the working machine, add the path to the library directory (including symfony) to include_path (for example: include_path = ".: / Php / includes: / opt / local / lib / php"), and the path to ProjectConfiguration.class.php change to relative require_once 'symfony / autoload / sfCoreAutoload.class.php';

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


All Articles