Monday, September 28, 2015

Deploying wordpress using a chef recipe and AWS OpsWorks

Although I am not a fan of Wordpress, many people are. Lately I had to set up multiple instances for my colleges, and even tho installing an instance takes just a few minutes, ends up being is a bit of a hassle to do it all by hand. I thought would be nice to automate Wordpress deployments in my environment.

Initially thought of a golden AMI image, with a set of instructions to update Wordpress and the OS on boot time, but using chef seemed more flexible. I did some research and found a nice cookbook from Kenta Yasukawa based on Apache. I copied most of his work, doing some modifications to use nginx + php fpm instead and other minor changes. My cookbook can be found at my github. You can configure your own user and passwords in the file attributes/default.rb:


Even tho MySQL by default is not exposed to the world, it is safer to establish your own user and passwords - you can clone the repo, modify the files the upload to your own repository or use S3:
default["mysql"]["root"] = "yourrootpass"
default["mysql"]["pass"] = "passforwpuser"
default["mysql"]["user"] = "yourwpuser"
With AWS OpsWorks, we can create our own stack of Wordpress servers. First, go to OpsWorks and create your first stack. Select your preferences, and after clicking in Advanced you will be able to specify the URL for the chef recipe (https://github.com/AndreuAntonio/chef-wp.git) in this case:


Create your layer with your personal preferences. I selected custom, as DB and FE are going to reside within the same server, but there are a lot more possibilities here:


Now edit the layer and specify what recipe you want to use and in what stage:

We will use the recipe Deploy_software_wordpress_nginx-mysql56::default during setup. Remember to check on the Security tab to specify the right security groups for this (i.e. HTTP open).


Now we can start deploying servers. Check the Instances tabs and deploy a new server:


Start the server. It takes a while to spin up. When ready, the status field will become a green online.

Click then on the public IP and Wordpress setup should show up:


To add more Wordpress servers, just deploy new instances and in a few minutes you'll have them running :)

This is just a test lab, for a real production environment there's other things that we should take care of, like load balancing, redundancy, backups etc. Don't take this example as it is to a production environment.