1+n PHP Versions and Projects Via LaraDock - by Peter Quill - Medium
1+n PHP Versions and Projects Via LaraDock - by Peter Quill - Medium
Search
Listen Share
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.
The Process
• Clone LaraDock.
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.
• 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:
• Do the same for PHP 7.1 and (if in need) other supported PHP versions.
• Make NGINX aware of the new php-fpm services via depends_on directive.
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 the fastcgi_pass directive is pointing to
a valid php-fpm upstream.
php74.local
server_name php74.local;
root /var/www/php74.local/public;
fastcgi_pass php-upstream;
php72.local
server_name php72.local;
root /var/www/php72.local/public;
fastcgi_pass php-fpm-7.2:9000;
php71.local
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
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
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.
Good luck!
Peter
Quill
Follow