This article is meant to help you configure and deploy your projects developed with Symfony2 into a Shared Hosting environment, so if you need to deploy using capifony on a VPS, please read this article.

Pre-requisites

capifony requires you have a ssh connection to your server, so make sure your current hosting plan supports this feature and that you enable shell access for the domain.

Installation

I assume that you already have Ruby & RubyGems installed on your local environment (MacOS X has this out of the box – simply update gems with `sudo gem update –system`).

To install capifony and capistrano, simply run:

gem install capifony

This command will install capifony and it’s dependencies (capistrano for example) to your local host. NOTE: Your local host is the only place where you need to install capifony & capistrano.

It’s also good practice to create and copy the SSH key to speed up the connections to your remote repositories.

Generate key with:

ssh-keygen -t dsa

Next, copy your public key to the remote server that you want to connect to (I am going to assume your SSH user is called `demoUser`):

cat .ssh/id_dsa.pub | ssh demoUser@your.domain.com -p 22123 "cat >> .ssh/authorized_keys2"

Then just try to connect with:

ssh demoUser@your.domain.com -p 22123

If all goes well the remote server will just let you in without password prompt.

You may also install capistrano_rsync_with_remote_cache gem in your local computer. This is not mandatory but it is highly recommended, since it creates a cache on the server which will allow to sync only the files that actually had changed since the last deploy, reducing time and bandwidth spent on remote connections. You can do this by running the command:

gem install capistrano_rsync_with_remote_cache

Create the configuration

capifony needs two files to be located inside the project root in your local machine, Capfile and app/config/deploy.rb. These files are created by capifony itself, by running the following command:

$ cd /path/to/your/project
$ capifony .

Configuration

Next we need to configure capifony for our application and server settings.

Repository

In this case we will use git as a repository. You can use a local repository

 set :repository,   "file:///path/to/your/local/project/repo"
 set :scm,          git

or use a remote repository instead, like github:

set :repository,    "git://github.com/namespace/project.git"
set :csm,           git

Server connection

These settings are for the ssh connections, used to execute remote commands and by rsync.

set :serverName, "sg111.servergrove.com" # The server's hostname
set :domain,     "example.com"
set :user,       "myusername"
ssh_options[:port] = 22123

Deploy settings

The following settings define the behavior of capifony.

# Remote location where the project will be stored
set :deploy_to,       "/var/www/vhosts/yourdomain.com/symfony_projects/"

# Deploy strategy
set :deploy_via,      :rsync_with_remote_cache

# Roles
role :web,        domain
role :app,        domain
role :db,         domain, :primary => true

# The number of releases which will remain on the server
set :keep_releases,   3

set  :use_sudo,      false

# Update vendors during the deploy
set :update_vendors,  true

# Set some paths to be shared between versions
set :shared_files,    ["app/config/parameters.ini"]
set :shared_children, [app_path + "/logs", web_path + "/uploads", "vendor"]

Result
This is the full configuration file

set :serverName,   "sg111.servergrove.com" # The server's hostname
set :repository,   "file:///path/to/your/local/project/repo"

set :domain,      "example.com"
set :deploy_to,   "/var/www/vhosts/yourdomain.com/symfony_projects/" # Remote location where the project will be stored
ssh_options[:port] = "22123"

set :scm,         :git
set :deploy_via,  :rsync_with_remote_cache
set :user,        "myusername"

# Roles
role :web,        domain
role :app,        domain
role :db,         domain, :primary => true

set  :keep_releases,  3 # The number of releases which will remain on the server
set  :use_sudo,       false

# Update vendors during the deploy
set :update_vendors, true

# Set some paths to be shared between versions
set :shared_files,    ["app/config/parameters.ini"]
set :shared_children, [app_path + "/logs", web_path + "/uploads", "vendor"]

Remote setup

Once you have fully configured capifony, you need to setup your deployment on the remote server, and this is as simple as running a single command on your local machine. You only need to execute this command once, before your first deploy.

$ cap deploy:setup

Deploy

Same thing with the deploy process.  All the hard work has been done with the configuration so we just need to run the deploy command:

$ cap deploy

This will create a new release of the project in the remote server, and in case they exists, it will remove old releases, keeping the quantity previously configured with :keep_releases.

That’s it, this tool is really simple to use and we can assure that will be very helpful for you.

For further knowledge you can head to the capifony reference.

We want to thank Ryan Weaver from KnpLabs for the configuration reference and Konstantin Kudryashov (aka everzet) for the initial Capifony article (his install steps used in this article).