By default PHP on Elastic Beanstalk runs on Apache, which is also a dependancy of Hostmanager. The goal of this project is to provide an easy way to replace Apache with Nginx/PHP-FPM. Addon packages are also supported, enabling support for Varnish, SSL, rsyslog forwarding and more. To use, transfer the build script onto a fresh Beanstalk AMI instance, and execute using bash.
Elastic Beanstalk is a great service, but as the concurrency increases, apache chews up a lot of resources. Nginx with PHP-FPM is a lethal combination for a dynamic web server, and perfect for large scale websites. Optional Varnish support adds a fast cache, and can increase content delivery speed up to 300%!
Deployment hooks are also available. Just add preDeploy.sh/postDeploy.sh to the root of your app, and they will be ran on deployment. An example would be:
#!/bin/bash
#
# AWS Elastic Beanstalk Nginx/PHP-FPM Configuration
#
# @author James Leckenby <[email protected]>
# @link http://jleck.co.uk
# @copyright 2013 James Leckenby
# @license MIT License
# @version 1.0
#
# Navigate to containg folder
FOLDER=`dirname ${0##*/}`
cd $FOLDER
# Run commands
composer install
# Delete self or deploy will fail
rm -f ${0##*/}
Several options are available when running the build script:
-a|--addons
comma seperated list of addons to install
-v|--help
show help text
-v|--version
show script version
Addons can be installed using the --addons
oprion. Accepts GIT clone urls as well as official addons, for example:
sudo ./build --addons composer,git://github.com/user/repo.git
Note that cloned repo must contain a build
file with install commands, or the script will error. Files must also return 0 or the script will error. Official addons include:
Composer is a popular dependency manager for PHP. Make you you run composer install
in postDeploy.sh to download all required packages. More information can be found here.
Rsyslog forwarding is impelemented for logentries. You'll need to create a token based input and then configure the logentries.token
Elastic Beanstalk environment property with that token.
Memcached is an in-memory key-value store for small chunks of arbitrary data. More information can be found here.
The ssl option creates a copy of the default Nginx config with ssl enabled. Encryption is done via a self-signed SSL certificate (valid for 1 year from the build script run date). The purpose of this self-signed SSL cert is to encrypt traffic between the Elastic Load Balancer (ELB) and your instance; you will still want to install a trusted SSL certificate on your Elastic Load Balancer. HTTPS traffic will be encrypted between the browser and ELB using your trusted certificate, and re-encrypted between the ELB and AWS instance using the self-signed certificate.
Varnish is a web application accelerator, which can increase content delivery speed up to 300%. When installed, it sits infront of Nginx on port 80, while Nginx is moved to run on 8080. More information can be found here.
- Create a new EC2 instance using a Beanstalk AMI, Tested on ami-95c6c0e1 (PHPBeanstalk64-2012.09.01T01.59.38.000), but should work with any PHP Beanstalk AMI.
- SSH into the instance and run the script, optionally specifying any adons to install:
wget https://raw.github.com/jleck/elasticbeanstalk-nginx-php/master/build
chmod +x build
sudo ./build --addons composer,memcache,varnish
- Exit SSH, and create AMI image from theinstance.
- Set your application's custom AMI ID to the new image, and enjoy the power of Nginx! :)