Run multiple PHP versions (PHP 5 and PHP 7) with NGINX on the same server howto


It is common sysadmin task to have two versions of PHP running on the same physical or Virtual server to be able to run simultaneously old PHP 5.X legacy applications and PHP 7.X written websites / web applications.

In the past this task was much more complicated than today  you had to compile for example Apache and PHP modules from source and enable it through fastcgi.
Today with the raise of NGINX Web Server and its possibility to run much of the PHP Apps running on top of Apache.
For the sake of this tutorial I'll be using Debian Strecth 9.0.
The reason to use NGINX instead of Apache for this tutorial are numerous, it is light weight (uses less resources – CPU ./ Memory) it is secure smaller in size.

1. Install NGINX Web Server


# apt-get install –yes nginx 


2. Install PHP.7.0 FPM module


# apt-get install php7.0-cli php7.0-fpm


3. Install PHP 5.6 FPM Using external deb repository

Debian default repositories does not include support for PHP 5.6, hence we need to add the respective repositories providing PHP 5.6


# apt-get install apt-transport-https


# curl | apt-key add –
echo 'deb stretch main' > /etc/apt/sources.list.d/
apt-get update


Next install PHP 5.6 from just added repos


# apt-get install –yes php5.6-cli php5.6-fpm


4. Check Multiple PHP versions PHP 5 and PHP 7 aree properly installed 


# php7.0 -v
PHP 7.0.15-1 (cli)
# php5.6 -v


Debian has a default set-up for PHP CLI (Console Interface command) pointing to PHP 7.0, e.g.


# php -v
PHP 7.0.15-1 (cli)


If you prefer to use as prefer PHP 5.6 instead you can do it with debian update-alternative cmd:


# update-alternatives –config php


5. Configure both installed PHP -es

Edit /etc/php/7.0/fpm/pool.d/www.conf and look for the listen option. It should equal to /run/php/php7.0-fpm.sock or something alike.
Now do the same for 5.6, it should contain the same with just 5.6 instead of 7.0. Note that it could also be a bind address, i.e. IP address with port (which is performance-wise more suitable for production than sockets). 


6. Configuring NGINX webserver

Nginx configuration files are stored in /etc/nginx 

– On Debian the .deb package structure of nginx is is made that all available virtual hosts for nginx just like for Apache are stored  in sites-available directory and production enabled virtualhosts are symlinks to sites-enabled

– Shared configuration for reuse among multiple domains is stored in inside the snippets directory

– fastcgi.conf file contains FastCGI specific variables that are passed to PHP

– The snippets/fastcgi-php.conf is just a helper file to prepare configuration that is passed to PHP module

It is a good idea to remove any unnecessery configuration from /etc/nginx/sites-enabled 

7. Create configuration for PHP 7.0

To make simple the test the main (root) directory of nginx will be set to have a simple phpinfo(); file.

mkdir /var/www/site-with-php7
echo -e '<?phpnphpinfo();' > /var/www/site-with-php7/index.php 

Then create actual Nginx configuration


# vim /etc/nginx/sites-available/site-with-enabed-php7.X


server {
    listen 8770 default_server;
    listen [::]:8870 default_server;

    server_name _;
    root /var/www/site-with-php7;
    index index.php;
    location / {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock; # adjust for the listen setting discussed above

As seen from configuration PHP 7 will be serving PHP scripts written for php 7  on TCP port 8870


8. Create configuration for PHP 5.6


# mkdir /var/www/site-with-php5.6
# echo -e '<?phpnphpinfo();' > /var/www/site-with-php5.6/index.php


server {
    listen 8756 default_server;
    listen [::]:8856 default_server;

    server_name _;
    root /var/www/site-with-php5.6;
    index index.php;
    location / {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php5.6-fpm.sock; # adjust for the listen setting discussed above

As you see from configuration PHP 5.6 will be serving PHP 5.6 files on TCP port 8756

To enable both NGINX configurations to load enable both nginx vhosts as there is no a2ensite like for enabling NGINX configurations the following  cmd does it

# ln -s /etc/nginx/sites-available/site-with-php5.6 /etc/nginx/sites-enabled/
#  ln -s /etc/nginx/sites-available/site-with-php7.0 /etc/nginx/sites-enabled/

To load the new NGINX Virtualhost configurations, restart next:

# systemctl reload nginx.service


9. Testing NGINX + PHP configuration set-up on port 8870 / 8876


– Test NGINX connection on 8876

lynx -dump http://localhost:8870

– Test NGINX connection on 8870

lynx -dump http://localhost:8870

Both commands should dump you output from PHP 7 (if your server lacks lynx i warmly recommend it, though you can use wget to test).


To sum it up

Even though generally it is a bad idea to have 2 instances of application service be it NGINX / Apache from security point of view, it is sometimes a necessity especially when you
or your customers are unwilling to invest money for upgrade of their websites / application infrastructure and if the clients want to keep obsolete PHP code and mix it with a new.
Still migration will be required as you would perhaps want to have some kind of Load Balancer round robin with another NGINX / Apache or Haproxy to make different applications
open under a separate CDN hostname

Share this on:

More helpful Articles

Download PDFDownload PDF

Tags: , ,

Leave a Reply

CommentLuv badge