How to run multiple PHP versions with apache on CentOS 7 linux

In today tutorial, we want to show how to run multiple PHP versions with apache on CentOS 7 linux.

it’s very practical and common to have multiple php versions and run it simultaneously with apache on a single server.

maybe you have a php script and want to test it with multiple php version. in such case this article is for you.

Lets explain how it’s possible to run multiple versions.

when we request a .php page from apache, it will refer to “SetHandler application/x-httpd-php” to know which module should be loaded to handle php script. the point is that here, php is under apache control.
in our scenario, things go different. here we use standalone php process called php-fpm. php-fpm is a php daemon that is configured to respond to FCGI requests.
so we start and stop php-fpm and apache independently.

Here is our environment:

OS: CentOS 7 linux on VMWare
Firewall: enabled
SELinux: enforcing
PHP versions: 5.6 and 7.2

1- Install prerequisites

before installing and running two php versions, we need to install apache and some repository. so execute these commands:

# yum install httpd
# yum install epel-release
# yum install yum-utils

2- Install multiple php versions

php-fpm is available in remi repository. so we install this repo and then after multiple php versions:

# yum install
# yum install php56 
# yum install php72
# yum install php56-php-fpm
# yum install php72-php-fpm

then make sure both versions are stopped:

# systemctl stop php56-php-fpm
# systemctl stop php72-php-fpm

3- Configure SELinux

to allow selinux run php-fpms scripts, run these commands:

# semanage port -a -t http_port_t -p tcp 9072
# semanage port -a -t http_port_t -p tcp 9056

4- Configure php-fpm

by default, each php-fpm version is listening on port 9000. because we want to run multiple php versions, we need to change default port:

# sed -i 's/:9000/:9056/' /etc/opt/remi/php56/php-fpm.d/www.conf
# sed -i 's/:9000/:9072/' /etc/opt/remi/php72/php-fpm.d/www.conf

now run two php-fpms:

# systemctl start php72-php-fpm
# systemctl start php56-php-fpm

now we need to make script wrapper to call php56-cgi and php72-cgi:

# cat > /var/www/cgi-bin/php56.fcgi << EOF
# #!/bin/bash
# exec /bin/php56-cgi

# cat > /var/www/cgi-bin/php72.fcgi << EOF
# #!/bin/bash
# exec /bin/php72-cgi

then we set executable bit on both scripts:

# sudo chmod 755 /var/www/cgi-bin/php56.fcgi
# sudo chmod 755 /var/www/cgi-bin/php72.fcgi

5- Configure apache

Here we create two path. one for php-fpm56 and another for php-fpm72. you can change these paths with your own:

# cat > /etc/httpd/conf.d/php.conf << EOF
# ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
# AddHandler php56-fcgi .php
# Action php56-fcgi /cgi-bin/php56.fcgi
# Action php72-fcgi /cgi-bin/php72.fcgi

# <Directory /var/www/html/php56>
# DirectoryIndex index.php
# AllowOverride all
# Require all granted
# </Directory>
# <Directory /var/www/html/php72>
# DirectoryIndex index.php
# AllowOverride all
# Require all granted
# </Directory>

then we put two php script on these two paths to test:

# mkdir -p /var/www/html/php56
# mkdir -p /var/www/html/php72
# echo "<?php phpinfo(); ?>" > /var/www/html/php56/index.php
# echo "<?php phpinfo(); ?>" > /var/www/html/php72/index.php
# echo "AddHandler php72-fcgi .php" > /var/www/html/php72/.htaccess

6- Start services

Now we enable and start apache and php-fpm services:

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

7- Configure firewall

we need to open port 80 to access apache. so run these commands:

# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload

Now refer to these address and you must see php_info page with two different versions: