1. Home
  2. Linux
  3. Run multiple versions of PHP for different domains in NGNIX

Run multiple versions of PHP for different domains in NGNIX

I recently purchased EduExpression WordPress plugin for a client to setup an examination software. I was able to install and configure the plugin without much problem. However, the problem started occurring when the student login was accessed – the plugin page went blank when the student attempted to take an exam. Well, I have a support right? I contacted the support team and they said, the plugin does not work with PHP 7.1 and it only supports PHP 5.6. That’s my bad – because I had my server setup with PHP 7.1 and all of my client website’s running on PHP 7.1. Now to make EduExpression to work, I need to setup PHP 5.6 version. Downgrading PHP is never a good option, but I had to do this to make this plugin work for the client. So how about setting up PHP 5.6 only for a particular domain and leave the rest to run on PHP 7.1. Well, in this tutorial I’ll explain how to setup multiple versions of PHP environment and link specific version of PHP with a particular domain in NGINX.

Environment

OS: CentOS 7, WebServer: Nginx, PHP: v7.1(Default), PHP FastCGI: PHP-FPM

Note: Above environment is already setup and running successfully.

Step 1: Installing multiple versions of PHP ie., PHPv5.6 along with PHP7.1

Here I use yum command to install multiple versions of PHP along with necessary modules as shown below:

Installing PHP5.6 version

# yum install php56 php56-php-common php56-php-mysql php56-php-pecl-memcache php56-php-pecl-memcached php56-php-gd php56-php-mbstring php56-php-mcrypt php56-php-xml php56-php-pecl-apc php56-php-cli php56-php-pear php56-php-pdo

Install PHP-FPM for PHP 5.6 version

PHP on Nginx works through PHP-FPM (FastCGI Process manager). PHP-FPM is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

# yum install php56-php-fpm

Well, we have installed PHP 5.6, but we should ensure the default version of PHP is still 7.1.

# php -v
PHP 7.1.22 (cli) (built: Sep 12 2018 07:22:13) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
 with Zend OPcache v7.1.22, Copyright (c) 1999-2018, by Zend Technologies

Configure PHP-FPM and PHP56-PHP-FPM

Most important part of the this tutorial is to configure two different PHP-FPM for two PHP versions. Here we shall configure two different versions of PHP-FPM that Nginx will work with. The main thing during configuration is the user/group of FastCGI processes and the ports they listen on.

PHP-FPM (Default 7.1) configuration file

/etc/php-fpm.d/www.conf

Make sure user/group set to nginx (Default value is apache).

user = nginx
group = nginx

Find the listen parameter and set  it to port 9000 on which FastCGI requests will be received.

listen = 127.0.0.1:9000	[php-fpm]
PHp56-PHP-FPM configuration file

/opt/remi/php56/root/etc/php-fpm.d/www.conf

Make sure user/group set nginx (Default value is apache).

user = nginx
group = nginx

Find the listen parameter and set it to port 9001 on which FastCGI requests will be received.

listen = 127.0.0.1:9001	[php56-php-fpm]

Enable PHP-FPM, PHP56-PHP-FPM and NGINX auto start at system boot on CentOS 7

After completing the above configurations, you need to enable the PHP-FPM, PHP56-PHP-FPM and Nginx to auto start at system boot.

# systemctl enable nginx

# systemctl enable php-fpm

# systemctl enable php56-php-fpm
Turn Off SELinux

#sudo setenforce 0

Configure domains with corresponding PHP-FPM for PHP executions

Open the corresponding domain configuration file of Nginx (/etc/nginx/conf.d/) and look for the line fastcgi_pass  and change it accordingly to link the right version of PHP it has to execute.

Domain 1 Nginx configuration file: /etc/nginx/conf.d/example1.conf

server {
 listen :80;
 server_name example1.com;
 server_name www.example1.com;

 root /home/example1/public_html;
 index index.php index.html index.htm;

 location / {
      try_files $uri $uri/ /index.php?q=$uri$args;
 }
 location ~ \.php$ {
      try_files $uri =404;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
      include fastcgi_params;
 }
}

Domain 2 Nginx configuration file: /etc/nginx/conf.d/example2.conf

server {
 listen :80;
 server_name example2.com;
 server_name www.example2.com;

 root /home/example2/public_html;
 index index.php index.html index.htm;

 location / {
       try_files $uri $uri/ /index.php?q=$uri$args;
 }
 location ~ \.php$ {
       try_files $uri =404;
       fastcgi_pass 127.0.0.1:9001;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param SCRIPT_NAME $fastcgi_script_name;
       include fastcgi_params;
 }
}

Check the Nginx configuration files for any syntax errors before restarting Nginx

# nginx -t

Restart Nginx, PHP-fpm, PHp56-PHP-FPM

# systemctrl restart nginx, php-fpm php56-php-fpm

Test different PHP versions for different domains

Create different phpinfo files under the two different domains root directory

# echo "<?php phpinfo(); ?>" > /home/example1/public_html/info.php

# echo "<?php phpinfo(); ?>" > /home/example2/public_html/info.php

Now open info.php in the browser to verify example1.com using PHP 7.1 and example2.com using PHP 5.6.

http://example1.com/info.php

PHP 7.1 version

http://example2.com/info.php

PHP 5.6 version

That’s It! I just deployed EduExpression plugin on example2.com which runs on PHP 5.6.

Updated on October 30, 2018

Was this article helpful?

Related Articles

Leave a Comment