Capifony Or deploy symfony project through Capistrano
I have been programming in PHP for a year using the symfony framework and find it a real pleasure. However, there are some site development processes that this framework does not fully cover, and it is not required =)
One of these processes is deployment or deployment and updating of a project on a production server. To perform such a routine operation, many scripts were written and one of the most popular is Capistrano . It is extremely easy to learn, perfect in functionality and extremely flexible in configuration, however, out of the box it is imprisoned under the RoR deployment of applications, for which, in fact, it was created.
Today I will try to tell you how to use Capistrano to deploy symfony projects.
Capistrano - ruby library that performs the routine of rolling out the site and provides flexible means for timely rollback of the project to the previous version (previous deployment).
Capistrano by connecting via SSH operates on remote directories, creating a new directory with the current code and creating the necessary symlinks. Capistrano can copy actual code from any other machine via SSH server, use GIT or SVN repositories. The possibilities of capify do not end there. In principle, if properly configured, the script is able to execute any set of unix commands on any number of remote systems (to which you have access, of course). However, the largest number of scripts and settings Capistrano has mainly for remote deployment of applications, which we are talking about today.
capify - command for creating Capistrano config
The implementation of the deployment process is divided into atomic operations-tasks (tasks), each of which is responsible for a separate aspect of “rolling out”. 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:
As I already said, capistrano was originally created for deploying RoR applications and there are some places that attach it (out of the box) to this particular 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 perform some tasks, such as
To solve all these problems, I slightly expanded the base capistrano deployment scripts, which gave birth to capifony.
You can download the script and participate in its development here: http://github.com/everzet/capifony/tree/master .
So, we have a project that needs to be deployed on a production server. This project we have is stored in a remote git repository on a production server (it may not be git and the code can be stored anywhere where there is access via SSH and in any form that Capistrano knows about, but he knows a lot =)).
To get started, we go to the local directory with the project that needs to be uploaded and whose remote git repository is on the server:
It should be noted that the directories log / , web / uploads are not involved in the deployment, because are shared resources that persist 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’ll talk about another interesting aspect of development along with the wonderful symfony ...
upd:Forgot 1 thing. By standard, when symfony generates a project, it sets the absolute path to symfony in config / ProjectConfiguration.class.php, which can be different on the working machine and production. To solve the problem, on both production and the working machine, add the path to the library directory (including symfony) to include_path (note: 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';
One of these processes is deployment or deployment and updating of a project on a production server. To perform such a routine operation, many scripts were written and one of the most popular is Capistrano . It is extremely easy to learn, perfect in functionality and extremely flexible in configuration, however, out of the box it is imprisoned under the RoR deployment of applications, for which, in fact, it was created.
Today I will try to tell you how to use Capistrano to deploy symfony projects.
What is capify?
Capistrano - ruby library that performs the routine of rolling out the site and provides flexible means for timely rollback of the project to the previous version (previous deployment).
Capistrano by connecting via SSH operates on remote directories, creating a new directory with the current code and creating the necessary symlinks. Capistrano can copy actual code from any other machine via SSH server, use GIT or SVN repositories. The possibilities of capify do not end there. In principle, if properly configured, the script is able to execute any set of unix commands on any number of remote systems (to which you have access, of course). However, the largest number of scripts and settings Capistrano has mainly for remote deployment of applications, which we are talking about today.
capify - command for creating Capistrano config
Standard Deployment Process
The implementation of the deployment process is divided into atomic operations-tasks (tasks), each of which is responsible for a separate aspect of “rolling out”. 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:
capify .
- creates files in the current directory to configure access to the remote system and configure the process. These files are:- Capfile - the main script pulled by the capify command
- config / deploy.rb - process settings included in the Capfile. It is in this file that you need to register all our paths to the servers and settings before executing the following commands
cap deloy:setup
- Deploys on the remote server / servers (there may be several) the file structure for subsequent deployment processescap deploy
- performs deployment. At the same time, a new directory for the project is created, it makes symlinks to shared resources, and then, a current symlink is made to this directory, slipping the current code to the web servercap rollback
- roll back to the previous deployment. In this case, the directory with the current version is deleted (with hard rollback), and the current symlink is interrupted by the previous deployment directory
Why do I need capifony?
As I already said, capistrano was originally created for deploying RoR applications and there are some places that attach it (out of the box) to this particular 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 perform some tasks, such as
symfony plugin:publish-assets
for creating symlinks to assets from plugins, symfony cc
for flushing the cache, and symfony fix-perms
for restoring permissions to the cache and web / uploads folders. To solve all these problems, I slightly expanded the base capistrano deployment scripts, which gave birth to capifony.
You can download the script and participate 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 we have is stored in a remote git repository on a production server (it may not be git and the code can be stored anywhere where there is access via SSH and in any form that Capistrano knows about, but he knows a lot =)).
To get started, we go to the local directory with the project that needs to be uploaded and whose remote git repository is on the server:
- In the root, we execute the command
capify .
that creates the Capfile in the root and deploy.rb in config / - Download the capifony.rb file from the github and put it in config /
- End Capfile appends
load 'config/capifony'
- And the last touch - we prescribe the connection settings to the remote server in deploy.rb:
set :application, "YOUR_APPLICATION_NAME"
set :repository, "YOUR_SSH_SERVER_NAME:/PATH/TO/repos/#{application}.git"
set :deploy_to, "/PATH/TO/www/#{application}.com"
set :scm, "git"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
server "YOUR_SSH_SERVER_NAME", :web, :app, :db
* This source code was highlighted with Source Code Highlighter. - Now we call from the console
cap deploy:setup
to configure the remote file structure andcap deploy
to perform the deployment. And we don’t forget if something suddenly goes wrong during subsequent deployments (the deployment passed, but the site fell, for example) - itcap rollback
will help
It should be noted that the directories log / , web / uploads are not involved in the deployment, because are shared resources that persist 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’ll talk about another interesting aspect of development along with the wonderful symfony ...
upd:Forgot 1 thing. By standard, when symfony generates a project, it sets the absolute path to symfony in config / ProjectConfiguration.class.php, which can be different on the working machine and production. To solve the problem, on both production and the working machine, add the path to the library directory (including symfony) to include_path (note: 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';