Running multiple versions of php under Nginx

We have a situation where we need to run multiple versions of PHP on a single Nginx instance. From the information we have gleaned and the limited knowledge we have on the Nginx -> php-fpm process it looks like we can do it pretty easily.

First we have to install the php7.0-fpm. As we have already installed the php5.6-fpm everything is setup to allow the new version to be installed with a simple command
apt-get install php7.0-fpm

Now when we look at the running processes we can see that we have 2 versions of php-fpm running.

php 5.6 and 7.0 fpm running

php 5.6 and 7.0 fpm running


Next we have to create a website in Nginx to allow it to interact with the new php7.0-fpm service. In the same way we created a virtual host for php5.6 use webmin to create a new virtual host with the following config.
# new test server configuration
#
server {
   listen 80;
   listen [::]:80;
   root /var/www/test2;
   # Add index.php to the list if you are using PHP
   index index.php;
   server_name www.test2.local;
   location / {
      # First attempt to serve request as file, then # as directory, then fall back to displaying a 404.
      try_files $uri $uri/ =404;
   }
   # pass PHP scripts to FastCGI server location
   ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
   }
   # deny access to .htaccess files, if Apache’s document root # concurs with nginx’s one location
   ~ /\.ht {
   deny all;
   }
}
When you create the above use the www.test2.local as the name. As before you will need to create a new directory /var/www/test2 and copy the index.php file we created in the /var/www/test directory to it. You also need to add the www.test2.local to your hosts file or DNS to allow name resolution, once that is done (check previous post if you need) you can now point your browser at http://www.test2.local and you should see the phpinfo output is for php Version 7.0.

PHP 7.0 running

PHP 7.0 running


Pointing your browser at http://www.test.local (if you have set it up with php5.6) will show that it is still running php5.6. So you now have 2 versions of PHP being served from the same Nginx Server (smart!)

This is only part of the solution for us as we also need to add Easycom support into the mix. The same process we followed when adding it to php5.6-fpm is required except we now need to add the easycom.so module from the PHP7.0.0 directory (IMPORTANT NOTE: At the time of writing this post PHP7.0.0 easycom.so is not available for general release, we are running a beta version internally for test purposes only). If you do have a copy of PHP7.0.0 easycom.so you can install it using the following process.

1. Copy the EasycomForPHP/easycom.so to /usr/lib/php/20151012 directory.
2. Create a new easycom.ini in the /etc/php/7.0/mods-available directory.
3. Add the following content and save
; configuration for php easycom module         
; priority=20         
extension=easycom.so 
4. Enable the easycom module
phpenmod easycom
5. Restart the php7.0-fpm service
service php7.0-fpm reload
Now when you point your browser at http://www.test2.local you should see that the Easycom module has been added to the PHP7.0 output.

This took us less than an hour to complete even while writing this post so it shows just how easy it is to get multiple versions of PHP running under a single Nginx server instance. 

If you need help installing any of the above or want to set up a Linux based webserver linking to the IBMi let us know.

Chris…