Deploying symfony2 project with capifony at hostgator shared hosting(subdomain)

I was recently working on a Symfony project and I needed to deploy it to a shared hosting at Hostgator. The default PHP version in Hostgator shared hosting is 5.2.17 (thats in mine). Some of you might know that Hostgator provides support for other versions of PHP as well (5.3 and 5.4). I use Capifony to deploy my symfony projects. So I had to made some adjustments to the deploy recipe that helped me successfully deploy a symfony2 (2.3.7) to a shared hosting in Hostgator. And I put here the full procedure to successfully setup capifony for deploy at Hostgator. (This was a staging deploy to a subdomain. If you need to deploy to your primary domain, I’m afraid Hostgator does not allow you to change the DocumentRoot for your primary domain. See this link to know more on changing DocumentRoot at Hostgator)

1. Get SSH access to your hosting server.

Make sure you get the SSH access to the hosting server. You can lodge a ticket asking for it to Hostgator OR request it from the billing panel. They will enable SSH access for your account through a non standard port 2222. The credentials are same as your cPanel credentials. Most likely you will be deploying from your Github account OR Bitbucket account. Either way you will need to generate a deployment key and add it to your account. So SSH into the server and generate a key. You can find how to do this here for Github and here for BitBucket. Remember you can add 2 types of keys. One account key that will have full access to all repos in your account, and deployment keys that will have readonly access to a particular depositories. Adding a deployment key is lot more safer.

2. Add a subdomain

Login to your cPanel and create a subdomain. Its good if you create the DocumentRoot outside your public_html directory so direct folder access to the subdomain directories from your primary domain is disabled by default. Most likely it will be `/home/username/mysubdomain`

3. Cook your capifony recipe

I assume you already have capifony installed and capified your symfony2 project. Now you need to alter your deploy.rb to adapt to hostgator. Here is mine

set :application, "yourappname"
set :domain,      "#{application}.yourdomain.com"
set :deploy_to,   "/home/yourusername/subdomain/"
set :app_path,    "app"
 
set :repository,  "path-to-your-repo"
set :scm,         :git
set :model_manager, "doctrine"
 
set :user,	"your-account-username"
set :password,	"your-account-password"
set :port,	"2222"
set :use_sudo, 	false
 
role :web,        domain
role :app,        domain, :primary => true
 
set  :keep_releases,  3
 
set :php_bin,	"/opt/php54/bin/php"
set :use_composer, true
set :update_vendors, true
set :copy_vendors, true
 
set :shared_files,      ["app/config/parameters.yml"]
set :shared_children,     [app_path + "/logs",app_path + "/cache" ,web_path + "/uploads"]
 
set :dump_assetic_assets, true
 
# Be more verbose by uncommenting the following line
 logger.level = Logger::MAX_LEVEL
 
task :upload_parameters do
  origin_file = "/local/path/to/parameters.yml"
  destination_file = shared_path + "/app/config/parameters.yml" 
 
  try_sudo "mkdir -p #{File.dirname(destination_file)}"
  top.upload(origin_file, destination_file)
end
 
task :update_htaccess do 
  temp_file = #{release_path}/.htaccess.tmp
  htaccess_file = #{release_path}/.htaccess
  directive = "AddType application/x-httpd-php54 .php"
  try_sudo 'echo #{directive} | cat - #{htaccess_file} > #{temp_file} && mv #{temp_file} #{htaccess_file}'
end
after "deploy:setup", "upload_parameters"
after "deploy:finalize_update", "update_htaccess"

The point to be noted here is :php_bin, this makes composer run using PHP 5.4.
Another key point is to update the .htaccess file to tell apache to use PHP5.4 to server our pages. The update_htaccess recipe just does that.

Leave a Reply

Your email address will not be published. Required fields are marked *