run multiple php version in nginx

How to Run Multiple PHP Versions in NGINX

PHP is a popular web development framework that is used by millions of web developers and bloggers to run their websites. Website administrators sometimes need to run multiple PHP versions on a single server in order to support different website requirements. In this article, we will learn how to run multiple PHP versions in NGINX.


How to Run Multiple PHP Versions in NGINX

Here are the steps to run multiple PHP versions in NGINX.


1. Install EPEL & Remi Repository

First we will begin by installing EPEL & Remi repository. Open terminal and run the following commands.

# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Next run yum-utils package that extends yum functionalities and allows you to provide yum-config-manager command. It allows you to enable & disable yum repositories.

# yum install yum-utils


2. Install NGINX

Next we install NGINX. If you have already installed it on your system you can skip this step.

Open terminal and run the following command to create /etc/yum.repos.d/nginx.repo. Add the following lines to this newly created file.

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


--------------- On RHEL 7 ---------------
[nginx] 
name=nginx repo 
baseurl=http://nginx.org/packages/rhel/7.x/$basearch/ 
gpgcheck=0 
enabled=1 

Run the following command to install NGINX.

# yum install nginx


3. Install PHP

We have assumed you already have database required to run your websites. If not, then please install database software like MySQL, PostgreSQL, etc. for that purpose, since it is not easy to run a website without a database system.

Next, we will use yum-config-manager to install multiple PHP versions. We will install PHP 7.1 and 5.6. Run the following command to install PHP 7.1 version.

# yum-config-manager --enable remi-php71  [Default]
# 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

Run the following command to install PHP 5.6.

# 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


4. Configure PHP-FPM and PHP56-PHP-FPM

Now we will configure PHP-FPM to run both the versions of PHP. Basically we need to define user & group of FastCGI processes to NGINX. We need to also configure the 2 PHP to run on separate ports to avoid conflict.

These are the two configuration files that we will edit.

  • php-fpm (default 7.1) – /etc/php-fpm.d/www.conf
  • php56-php-fpm – /opt/remi/php56/root/etc/php-fpm.d/www.conf

Open the files in text editor with the following commands.

# vi /etc/php-fpm.d/www.conf   [PHP 7.1]
# vi /opt/remi/php56/root/etc/php-fpm.d/www.conf  [PHP 5.6] 

In both the files, the default values for user and group will be apache, change them to nginx.

user = nginx
group = nginx

Also change the value of listen parameter as shown. In this case, PHP 7.1 runs on port 9000 and PHP 5.6 runs on 9001.

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

Save and close the file. Once the changes have been made run the following commands to restart your systems. Restart NGINX as well as PHP

# systemctl enable nginx 
# systemctl start nginx 

---------------- PHP 7.1 ---------------- 
# systemctl enable php-fpm 
# systemctl start php-fpm 

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


5. Setup Websites

We will run 2 websites – example1.com and example2.com that run on PHP 7.1 and PHP 5.6 respectively.

For this purpose, create the following directories.

---------------- Website 1 ----------------
# mkdir -p /var/www/html/example1.com/ 
# mkdir -p /var/log/nginx/example1.com/ 
 

---------------- Website 2 ----------------
# mkdir -p /var/www/html/example2.com/
# mkdir -p /var/log/nginx/example2.com/ 

Run the following command to change ownership and permissions.

---------------- Website 1 ----------------
# chown -R root:nginx /var/www/html/example1.com/ 
# chmod -R 755 /var/www/html/example1.com/ 
# chown -R root:nginx /var/log/nginx/example1.com/
# chmod -R 660 /var/log/nginx/example1.com/ 

---------------- Website 2 ----------------
# chown -R root:nginx /var/www/html/example2.com/ 
# chmod -R 755 /var/www/html/example2.com/
# chown -R root:nginx /var/log/nginx/example2.com/ 
# chmod -R 660 /var/log/nginx/example2.com/


6. Create NGINX blocks

We will create separate configuration files for each website at /etc/nginx/conf.d. Open terminal and run the following commands for this purpose.

# vi /etc/nginx/conf.d/example1.com.conf
# vi /etc/nginx/conf.d/example2.com.conf

Website 1

Add the following lines to it. Replace example1.com with your first website’s domain name.

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

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

        #charset koi8-r;
        access_log /var/log/nginx/example1.com/example1_access_log;
        error_log   /var/log/nginx/example1.com/example1_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/example1.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;

        }
}

Website 2

Add the following lines for website 2. Replace example2.com with the domain for second website.

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

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

        #charset koi8-r;
        access_log /var/log/nginx/example2.com/example2_access_log;
        error_log  /var/log/nginx/example2.com/example2_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/example2.com/;
                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 files. Open the main NGINX configuration file /etc/nginx/nginx.conf in text editor and add the following line in it to include the above configuration files in NGINX.

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


7. Test PHP configuration

To test the 2 PHP websites, we create 2 simple info.php files, one for each website, with the following commands.

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

Restart NGINX and PHP to apply all changes. The first command will check NGINX configuration for errors.

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

Now open browser and visit each of the following URLs in a separate tab or one after the other.

http://example1.com/index.php
http://example2.com/index.php

Each of the above URLs should show you the PHP configuration of that website.

In this article, we have learnt how to run multiple PHP version in NGINX.

Also read:

How to Create & Execute JAR File in Linux
How to Fix Plain HTTP Request was sent to HTTPS
How to Block USB Storage Devices in Linux
How to Disconnect Inactive or Idle SSH Sessions
How to Enable Debugging Mode in SSH

Leave a Reply

Your email address will not be published.