OpsWorks: Deploying your PHP Application Without Restarting Apache

For quite a while now I have been trying to exploit AWS OpsWorks to deploy software updates to our MetaSearch Gateway with less manual work. Before using OpsWorks, I built AWS servers from custom AMI, deployed app using git push to remote, and remote deployment hook pulled latest code updates to a folder; and if there were any secondary servers, these were updated with rsync.

Now, AWS OpsWorks is great set of tools, based on famous Chef (currently 11.4 is supported). With little efforts I was able to include and configure necessary PHP extensions to a default PHP Web app:
  • APC
  • memcached
  • Mongo
  • Geoip
Since AWS supports also elastic load balancer, I have two 24/7 instances running behind ELB, and third server (load-based) waiting for load to increase. Deployments are quite easy, with a push of a button, code is pulled from remote git and symlinks are updated. Sounds good? Well, too good to be true. By default, after deploying your app, Apache restarts itself; and naturally this caused some unexpected results.

Solution

Every time I ran a deployment to my PHP app, there was small app downtime. Part of this is caused by ELB health checks, which triggered removing unhealthy server from ELB. I started looking around, why this happens, and after extensive checking of deployment logs I found that every time you deploy your code, Apache is forced to restart. Culprit is the default php deployment recipe https://github.com/aws/opsworks-cookbooks/blob/master-chef-11.4/deploy/recipes/php.rb - this includes 2 additional recipes:

#include_recipe "mod_php5_apache2"
#include_recipe "mod_php5_apache2::php"

There is no reason to include them, since PHP and virtual hosts are already configured at the Configure stage. I have created custom chef script for PHP deployment: deploy/recipes/php.rb .

Once enabled custom cookbooks in the Stack settings, updated custom cookbooks and ran redeploy, my PHP app is deployed to a destination without restart, and there was no downtime anymore.

Hope you find this tip useful!


Comments

Popular posts from this blog

Stubbing and Mocking Static Methods with PHPUnit

Enable HTTP/2 Support in AWS ELB

How To Attach Your EBS volume to multiple EC2 instances