PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. Different applications require various versions of PHP, so in this tutorial, we are going to install multiple versions of PHP on CentOS 7.

Nginx uses PHP-FPM (stands for FastCGI Process Manager), which is an alternative PHP FastCGI implementation with some extra, useful features for heavily loaded websites.

Deploying your cloud server
If you have not already registered with Cloudwafer, you should begin by getting signed up. Take a moment to create an account after which you can easily deploy your own cloud servers.

Once you have signed up, log into your Cloudwafer Client Area with the password provided in your mail and deploy your Cloudwafer cloud server.

Step One – Setup Yum Repositories
The first step is to install and enable the REMI and EPEL yum repositories on your system if they are not already installed. Type the commands below:

yum install epel-release
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

The next step is to install the yum-utils package using the command below:

sudo yum install yum-utils -y

Step Two: Installing Nginx Web Server
We'll be installing the latest version of Nginx, by adding the official Nginx repository. Create a file named /etc/yum.repos.d/nginx.repo

touch /etc/yum.repos.d/nginx.repo

nano /etc/yum.repos.d/nginx.repo

Since we are working On CentOS 7, add the following lines to the file we just created now:

[nginx]  
name=nginx repo 
baseurl=http://nginx.org/packages/centos/7/$basearch/ 
gpgcheck=0 
enabled=1

We can now go ahead and install Nginx using the yum package manager by typing the command below:

yum install nginx

Step Three: Installing Multiple Versions of PHP:
Now, moving on to the core of this guide, use the yum-config-manager command to install multiple versions of PHP along with most required modules as shown.

Install PHP 7.2 Version

sudo yum-config-manager --enable remi-php72
sudo yum install php php-common php-fpm
sudo yum install php-mysql php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo

Install PHP 5.6 Version

sudo yum install php56 php56-php-common php56-php-fpm  
sudo yum install 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

You can check the default version of PHP version installed by typing the command below:

php -v

Step Four: Configuring PHP Version in CentOS with Nginx
To set the default PHP version to be used on the system while using the Nginx Web Server, we will need to configure the different versions of php-fpm that Nginx will work with.

We would also define the user/group of the FastCGI processes as well as the ports they will listen on.

The following configuration files are what we will be editing to achieve our task:

php-fpm (default 7.2) – /etc/php-fpm.d/www.conf

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

PHP 7.2

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

The default values should be apache, change them to nginx as shown below:

user = nginx
group = nginx

Locate the listen parameters, and define the address:port on which FastCGI requests will be received.

listen = 127.0.0.1:9000    [php-fpm]

PHP 5.6

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

Same as PHP 7.2, the default values should be apache, change them to nginx as shown below:

user = nginx
group = nginx

Locate the listen parameters, and define the address:port on which FastCGI requests will be received.

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

Save and close the files.

Next, proceed to restart (and enable to auto-start at system boot) Nginx, MariaDB (if installed) and PHP-FPM.

sudo systemctl enable nginx 
sudo systemctl restart nginx 

PHP 7.2

sudo systemctl enable php-fpm 
sudo systemctl restart php-fpm 

PHP 5.6

sudo systemctl enable php56-php-fpm 
sudo systemctl restart php56-php-fpm 

Note: If you have any issues restarting PHP 5.6, SELinux could be blocking it from starting. Check this guide on temporarily or permanently disabling SELinux.

Step Five: Setup Nginx Server Blocks for Websites
In this step, we are going to configure how Nginx will process requests to websites using server block configuration files which should be located in /etc/nginx/conf.d/. We are going to be using two different websites in this guide.

Website One: cloudwaferlabs.com.ng

sudo nano /etc/nginx/conf.d/cloudwaferlabs.com.ng.conf

Paste the following Configuration for cloudwaferlabs.com.ng

server {
    listen 80;
    server_name cloudwaferlabs.com.ng www.cloudwaferlabs.com.ng;

    root   /var/www/html/cloudwaferlabs.com.ng;
    index index.php index.html index.htm;

    #charset koi8-r;
    access_log /var/log/nginx/access_log;
    error_log   /var/log/nginx/error_log   error;

   location / {
            try_files $uri $uri/ /index.php?$query_string;
    }

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {

            root    /var/www/html/cloudwaferlabs.com.ng;
            fastcgi_pass   127.0.0.1:9000;    #set port for php-fpm to listen on
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include         fastcgi_params;
            include /etc/nginx/fastcgi_params;

    }
}

Save and close the file.

Website Two: test.cloudwaferlabs.com.ng

sudo nano /etc/nginx/conf.d/test.cloudwaferlabs.com.ng.conf

Paste the following Configuration for test.cloudwaferlabs.com.ng

server {
    listen 80;
    server_name test.cloudwaferlabs.com.ng www.test.cloudwaferlabs.com.ng;

    root    /var/www/html/test.cloudwaferlabs.com.ng;
    index index.php index.html index.htm;

    #charset koi8-r;
    access_log /var/log/nginx/access_log;
    error_log  /var/log/nginx/error_log   error;

   location / {
            try_files $uri $uri/ /index.php?$query_string;
    }

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {

            root    /var/www/html/test.cloudwaferlabs.com.ng;
            fastcgi_pass   127.0.0.1:9001;    #set port for php56-php-fpm to listen on
        fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include         fastcgi_params;
            include /etc/nginx/fastcgi_params;

    }

}

Save and close the file.

Add the following lines in the http block in /etc/nginx/nginx.conf.

include /etc/nginx/conf.d/*.conf;
server_names_hash_bucket_size 64;

Step Six: Testing Different PHP Versions

After all over configurations, we are now going to our server is using the two versions of PHP. We will create a very basic info.php script in the document root directories of our websites as shown below:

echo "<?php phpinfo(); ?>" > /var/www/html/cloudwaferlabs.com.ng/info.php

echo "<?php phpinfo(); ?>" > /var/www/html/test.cloudwaferlabs.com.ng/info.php

We need to restart Nginx, php-fpm and php56-php-fpm to apply our changes.

sudo systemctl restart nginx php-fpm php56-php-fpm

We can check that the Nginx configuration files for any syntax errors before doing so with the command below:

sudo nginx -t

Note: If you are running your server locally, you need to set up local DNS using /etc/hosts file as shown below.

ip-server-address cloudwaferlabs.com.ng cloudwaferlabs

ip-server-address test.cloudwaferlabs.com.ng test

Finally, open a web browser and type the following addresses to verify the versions of PHP installed on the system.

cloudwaferlabs.com.ng/info.php

test.cloudwaferlabs.com.ng/info.php

We have successfully installed two versions of PHP (PHP7.2 and PHP5.6) on CentOS.