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
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 http://rpms.remirepo.net/enterprise/remi-release-7.rpm # 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 # EOF # cat > /var/www/cgi-bin/php72.fcgi << EOF # #!/bin/bash # exec /bin/php72-cgi # EOF
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> # EOF
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:
17 thoughts on “How to run multiple PHP versions with apache on CentOS 7 linux”
hello…followed these instructions and not getting anywhere…. can I pay you to setup my server in this manner?
Hi. yes. send me your email to contact you by.
Why I just run only both php version 5.6?
Check to see if you have done exactly as described in this article. may be some where you have done it wrong
When I change port to 9072 in php 72 and check status for php 72 it still return active (running). But I run php 72 in website it return 503. Please help me!
you should investigate in logs to see root cause of problems.
I realize that the line
# echo “AddHandler php72-fcgi .php” > /var/www/html/php72/.htaccess
it’s a error please confirm , white space in . php
please can someone confirm my suspected! thanks in advance
Thanks monfi for your report. i corrected it.
i can get runned by execute php -S localhost:9072 –> listening port php72 , thanks you all! from Ecuador!
Environment: CentOS 7 – Apache 2.4.6
already running Drupal site on php 5.4
I’m creating a new site (with it’s own domain), so I set virtual hosts >> sites-enabled and sites-available
This new site needs to run on php 7.2
I followed all the steps; both sites come up, but cannot manage to run 2 different php versions.
Both of them are running the default php version: 5.4.16
Any advice on what I might be doing wrong is greatly appreciated.
I’m trying to set this up on a test server, so it’s not available to the public)
… continue form my above post
So, if I navigate to
A) /var/www/php72/ folder and I run:
sudo php -S localhost:9072
PHP 5.4.16 Development Server started at [date]
Listening on http://localhost:9072/
Document root is /var/www/php72
B) /var/www/php54/ folder and I run
sudo php -S localhost:9054
PHP 5.4.16 Development Server started at [date]
Listening on http://localhost:9054
Document root is /var/www/php54
Another way that I see that both instance are running php 5.4.16:
If I navigate to each folder (A & B) above, and I run
php -r ‘require_once(“index.php”);’ (access to the server is only through cli, -I don’t have browser access)
on both cases the php version listed is
PHP Version => 5.4.16
You have made a mistake in the last line of section 5.
echo “AddHandler php72-fcgi.php” > /var/www/html/php72/.htaccess
it must be:
echo “AddHandler php72-fcgi .php” > /var/www/html/php72/.htaccess
There is no difference between my mistake and your correction
Yes there is, a space after fcgi is required.
its work like a charm, thanks..
but i have little problem when trying to install mysql module etc.
its not load properly when i check on php info.
can u show me how to install it ?
yum install php74 php74-php-fpm php74-php-pear php74-php-common php74-php-cli php74-php-json php74-php-pdo php74-php-opcache php74-php-mbstring php74-php-sodium php74-php-xmlrpc php74-php-pgsql php74-php-pecl-mcrypt php74-php-soap php74-php-xml php74-php-gd php74-php-json php74-php-xml php74-php-mysqlnd
yum install php56 php56-php-fpm php56-php-pear php56-php-common php56-php-cli php56-php-json php56-php-pdo php56-php-opcache php56-php-mbstring php56-php-sodium php56-php-xmlrpc php56-php-pgsql php56-php-pecl-mcrypt php56-php-soap php56-php-xml php56-php-gd php56-php-json php56-php-xml php56-php-sqlsrv php56-php-mysqlnd
For version is configured properly but bot version are unable to laod mysql and mysqli extension.