1+n PHP versions and projects via LaraDock
I found myself in a peculiar situation — working with multiple, legacy and greenfield, PHP projects thus in need to have my local development environment supporting 7.1.x, 7.2.x and 7.4.x.
My choice for a local PHP development environment is Laradock, love this project. It does support multiple project setup but sadly, at the moment of writing, does not support multiple PHP versions. There’s an open issue from 2017 with recent (2019) updates which talks about this challenge.
This was a must-have for me.
The Process
- Clone LaraDock.
- Get your
.env
in order. - Amend
docker-compose.yml
to introduce newphp-fpm
services. - Configure NGINX sites to use different
php-fpm
services. - PHP extensions and Composer.
Let’s discuss every step in more details.
Clone Laradock
The setup was done and tested with v9.5 tag.
If you are running LaraDock from a master branch, or later on, decide to update to a newer tag, make sure to stash local docker-compose.yml
changes before you pull, and pop afterwards.
$ git clone git@github.com:laradock/laradock.git
$ cd laradock
$ git checkout v9.5
Get your .env in order
By default .env
is well configured and whit out any change can cover the majority of web projects. If change is needed it’s usually project-specific.
My common tweaks are:
- PHP version
- MySQL version
- Node version
- Enable Python
Do explore the .env
file and various configuration flags. I bet (if you aren’t familiar with LaraDock) you’ll be amazed how much this single, Docker-based development environment can cover for you.
Amend docker-compose.yml
The goal is to introduce 1+n php-fpm
services which run different versions of PHP.
By default, you get one php-fpm
service where PHP version is controlled with PHP_VERSION
flag in the .env
file.
This is your “master” php-fpm
service and should run the newest PHP version, the same version applies to PHP CLI inside workspace
container. At the moment of writing, that’s 7.4, thus PHP_VERSION=7.4
Let’s create our “child” php-fpm
container(s) with hard-coded PHP version. As previously stated I had a requirement for PHP 7.1.x, 7.2.x and 7.4.x.
In the docker-compose.yml
, copy php-fpm
entry and paste below original entry with the following tweaks:
### Key differences for PHP-FPM 7.2
php-fpm-7.2:
args:
- LARADOCK_PHP_VERSION=7.2
volumes:
- ./php-fpm/php7.2.ini:/usr/local/etc/php/php.ini
Alternatively, use the amended docker-compose.yml
or explore the diff between tweaked and original versions to visualise what are we doing here.
- Do the same for PHP 7.1 and (if in need) other supported PHP versions.
- Make NGINX aware of the new
php-fpm
services viadepends_on
directive.
Build and run
$ docker-compose up -d --build workspace nginx
If this is your first build, it will take time.
Confirm seeing all, including additionally configured, php-fpm
services.
$ docker ps --filter "name=php" --format "{{.Names}}"
laradock_php-fpm_1
laradock_php-fpm-7.2_1
laradock_php-fpm-7.1_1
Configure NGINX sites
The only thing we need to change is thefastcgi_pass
directive, everything else is a standard host setup.
# For .env PHP_VERSION
fastcgi_pass php-upstream;# For PHP 7.2
fastcgi_pass php-fpm-7.2:9000;# For PHP 7.1
fastcgi_pass php-fpm-7.1:9000;
For a quick test, let’s create 3 local domains in /etc/hosts
127.0.0.1 php74.local
127.0.0.1 php72.local
127.0.0.1 php71.local
Create (or copy from here) NGINX host files in ./laradock/nginx/sites/
. I’ve used the provided Laravel example template as my projects were indeed Laravel projects. That being said, it does not matter as long as thefastcgi_pass
directive is pointing to a valid php-fpm
upstream.
As a result of using laravel.conf.example
we only need to amend server_name
, root
and fastcgi_pass
directives to get our quick-test local domains to respond.
server_name php74.local;
root /var/www/php74.local/public;
fastcgi_pass php-upstream;
server_name php72.local;
root /var/www/php72.local/public;
fastcgi_pass php-fpm-7.2:9000;
server_name php71.local;
root /var/www/php71.local/public;
fastcgi_pass php-fpm-7.1:9000;
Create index.php
in the root
folders of all host files with a one-liner <?php phpinfo();
as, at this point, we just want to confirm the PHP version.
$ tree -I laradock
.
├── php71.local
│ └── public
│ └── index.php
├── php72.local
│ └── public
│ └── index.php
└── php74.local
└── public
└── index.php
Restart NGINX container for configurations to take effect.
$ docker-compose restart nginx
The Moment of Truth: visit URLs or curl
and look for X-Powered-By.
$ curl -I http://php71.local
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 07 Feb 2020 08:10:31 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.1.33$ curl -I http://php72.local
$ curl -I http://php74.local
PHP extensions and Composer
For the PHP_VERSION=7.4
you can and you should use the workspace
container as it comes with Composer and all other goodies (as configured in .env
file) composed.
$ docker-compose exec workspace bash
% composer …
For the hard-coded PHP 7.1.x and 7.2.x containers, you’ll need to bash inside and install Composer, enable PHP extensions from whit in.
$ docker-compose exec php-fpm-7.1 bash
% ... install composer ...
% docker-php-ext-install pcntl
% docker-php-ext-install pspell
mcrypt
as a deprecated extension does not ship with docker-php-ext-install
anymore, as I needed it, here’s the recipe.
$ docker-compose exec php-fpm-7.2 bash
% apt-get install libmcrypt-dev libreadline-dev -y
% pecl install mcrypt-stable
% docker-php-ext-enable mcrypt
At this point, you should have working 1+n PHP versions and projects setup powered by LaraDock. The next step is to git-clone your projects, create NGINX host files and away you go.
Good luck!