#Nginx + PHP-fpm This repo forms the basis of an easy way to setup horizontally scaling PHP-fpm servers, behind an Nginx load-balancer
##Dependencies
###docker-compose
I use docker-compose
to link the containers.
I had trouble using this on OSX, but found some useful information here: docker/compose#271. Note that I installed Docker using Kitematic.
These issues seemed to have been fixed with some recent updates of Docker to bundle all software into the Docker Toolbox. Everything worked perfectly with the Toolbox on a fresh install.
###Docker Volume
You also need to have a docker volume contianer, (I've called mine src
) to mount the files in /app
and replicate across each of the containers. You can read more about volumes here.
To create the one required here, run:
docker create -v /path/to/docker/app:/src --name src nginx /bin/true
where /path/to/docker/app
is the absolute path to whatever files you want to mount on your host machine, i.e. a php app. You can link to /docker/app
in this repositiory to see how it works. /src
after the colon specifies the absolute path of where these files are mounted to within each container.
##Usage
Simply run docker-compose up -d
from the root of the directory. It will build images and then run services of everything specified in the docker-compose.yml
file.
##Notes
- To maintain predictability for the
docker scale
command, docker prefixesdocker_
to the start of container names specified in the.yml
file and appends_1
to the end of each instance. For example, the namephp1
will appear as a containerdocker_php1_1
. This will appear as an entry in the hosts file ofdocker_server_1
for a maintanable reference (otherwise the reference would be a random hash). - Defining the
docker-compose.yml
with onephp
definition and intending to calldocker-compose scale php=3
(which would create 3 instances, calleddocker_php_1
,docker_php_2
anddocker_php_3
) after startup will cause nginx to fail (and so the instance won't start) becuase it would't have a reference todocker_php_2
anddocker_php_3
as there won't be entries in the hosts file yet. That's why I defined them all indivitually. See first TODO below. - For it to be possible to have Nginx and PHP-fpm on different servers, you need to replicate, identically, the file structure of your application, across all instances. Nginx simply sends the filename to the PHP-fpm server, and not the file itself.
##TODO
- Figure out how to incorperate
docker-compose scale php=3
and remove the duplication of php service declarations in thedocker-compose.yml
file. To do this, we need to be able to configure the servers in nginx'supstream
directive innginx.conf
.