Home Linux CentOS How to Install Different PHP Versions (5.6,7.1 or 7.2) | Multiple Websites...

How to Install Different PHP Versions (5.6,7.1 or 7.2) | Multiple Websites | Nginx | CentOS

Key Points:
1. How to install different PHP versions (5.6,7.1 or 7.2).
2. How can I configure different PHP versions with multiple websites in Nginx on the same server?

Different PHP versions InstallDifferent PHP versions Install

In some scenarios, developers need to run different PHP versions for multiple websites in Nginx on the CentOS 7 server. So what we need in this scenario?? We need the following things to achieve this task.

Required

  1. Nginx web browser
  2. Mysql DB
  3. PHP different versions (here using PHP 5.6 and PHP 7.2)
  4. Multiple Websites

Step 1. Firstly, we will install Nginx web server. Execute these commands one by one and assuming you are using root account or have the sudo privileges. For CentOS server :

 # yum install epel-release.noarch
# yum install nginx
# systemctl start nginx
# systemctl enable nginx

Step 2. Install the MySQL db on the server. Visit the MySQL Yum Repository to download the latest version of MySQL or just use the following wget command as it is on terminal to download MySQl 5.7 version.

# wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
# rpm -ivh mysql57-community-release-el7-9.noarch.rpm
# yum install mysql-server
# systemctl start mysqld

Step 3. Install different PHP versions on the server. Here we are installing PHP 5.6 and PHP 7.2 version (set as default PHP). We are using here Remi Repository to download the different versions of PHP as it depends upon the EPEL Repository, so install both of these two repositories.

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

Install PHP 7.2 Version

If you need to install PHP any version then need to enable the remi-php with specific php version as shown below:

 # yum-config-manager --enable remi-php72
# yum install php php-common php-fpm
# 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

To check the installed and default php version, use the command:

# php -v

Install PHP 5.6 Version

Now we will install second php version i.e. PHP 5.6 on the same server.

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

Step 4. Next step is to configure the installed PHP-FPM and PHP56-PHP-FPM. This step will tell Nginx to use, PHP-FPM’s different versions with different ports. Need to define the user, group and port of the FastCGI processes.

Configuring PHP-FPM For PHP 7.2

open the below file and make changes as mentioned below.

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

need to change the default user and group from apache to nginx. And also set the listen port.

user = nginx
group = nginx
listen = 127.0.0.1:9000

Configuring PHP FPM for PHP 5.6

Open the file & make changes as mentioned below but remember, listen port number must be different from the PHP FPM 7.2.

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

user = nginx
group = nginx
listen = 127.0.0.1:9001

We have done configuration part, need to restart the services & enable the services on system startup. For Nginx Service:

# systemctl start nginx
# systemctl enable nginx

MySQL Server Service :

# systemctl start mysqld
# systemctl enable mysqld

PHP-FPM (PHP 7.2) Service:

# systemctl enable php-fpm 
# systemctl start php-fpm

For PHP-FPM (PHP 5.6):

# systemctl enable php56-php-fpm 
# systemctl start php56-php-fpm

If you gets error when start the second instance PHP-FPM (PHP 5.6), there might be the possibility of SELinux is blocking it. You can set the Selinux mode to permissive.

To check SELinux status:

#getenforce

if it is enforcing, then set it to permissive mode:

# setenforce 0

Step 5:  Configure Multiple Websites

Here we will test 2 websites named site1.com and site2.com. First we will create directory site1.com and site2.com under the /var/www/html and then will create a test file i.e. index.php

site1.com website:

# mkdir -p  /var/www/html/site1.com
# mkdir -p /var/log/nginx/site1.com
# chmod -R 755 /var/www/html/site1.com/

Create the info.php file with basic php info code to test the site1 website running with different PHP version.

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

Now configure the nginx server block for site1.com website.For the configuration file, path is /etc/nginx/conf.d/.

 # vi  /etc/nginx/conf.d/site1.com.conf

Then copy the following block and paste in the opened file.

server {
listen 80;
server_name site1.com www.site1.com;

root /var/www/html/site1.com/;
index index.php index.html index.htm;

#charset koi8-r;
access_log /var/log/nginx/site1.com/site1_access_log;
error_log /var/log/nginx/site1.com/site1_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/site1.com/;
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;
}
}

site2.com website :

Now we will configure our second website.

# mkdir -p /var/www/html/site2.com
# mkdir -p /var/log/nginx/site2.com
# chmod -R 755 /var/www/html/site2.com/

Here now also create the info.php file with basic PHP code to display PHP version.

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

Go to directory /etc/nginx/conf.d/ to configure website with Nginx. Open the site2 website configuration file and paste the following code.

# vi  /etc/nginx/conf.d/site2.com.conf
server {
listen 80;
server_name site2.com www.site2.com;

root /var/www/html/site2.com/;
index index.php index.html index.htm;

#charset koi8-r;
access_log /var/log/nginx/site2.com/site2_access_log;
error_log /var/log/nginx/site2.com/site2_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:9001
location ~ .php$ {

root /var/www/html/site1.com/;
fastcgi_pass 127.0.0.1:9001; #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;
}
}

We can check the Nginx file for the syntax error by running the following command.

# nginx -t
Command to check nginx syntax errorCommand to check nginx syntax error

Now finally need to restart the service Nginx, php-fpm and php56-php-fpm.

 # systemctl restart  nginx  php-fpm  php56-php-fpm 

So now we are just one step behind the final step for the local environment. For local DNS working, we need to make changes in /etc/hosts file.

192.168.150.131 www.site1.com
192.168.150.131 www.site2.com

Finally, we have done it. Just browse the sites and check the PHP version for both the sites.

http://www.site1.com/info.php
http://www.site2.com/info.php

PHP 7.2 versionPHP 7.2 version
PHP 5.6 versionNPHP 5.6 version




Congratulations !!! You have configured different PHP versions with multiple websites in Nginx web server on the CentOS 7 server.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here