Multiple PHP version with Apache on CentOS 7
Solution 1
install all the necessary repos and packages
big thanks to https://rpms.remirepo.net/wizard/
the following commands assume you already sudo su -
or you will have to add sudo to each of the commands:
yum install httpd -y
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
yum install yum-utils -y
yum install php56 -y
yum install php72 -y
yum install php56-php-fpm -y
yum install php72-php-fpm -y
stop both fpm servers
systemctl stop php56-php-fpm
systemctl stop php72-php-fpm
by default it listens on 127.0.0.1 port 9000, make them listen on different ports
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 two different version of fpm can be started on different ports
systemctl start php72-php-fpm
systemctl start php56-php-fpm
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
make them executable by apache
sudo chmod 755 /var/www/cgi-bin/php56.fcgi
sudo chmod 755 /var/www/cgi-bin/php72.fcgi
create php configuration for apache. by default it runs php56-fcgi handler
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
make test pages, create .htaccess to use php72-fcgi
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
Now you should be able to test it
(http://127.0.0.1/php56)
(http://127.0.0.1/php72)
If you want to startup these instance automatically after server reboot
sudo systemctl enable httpd
sudo systemctl enable php56-php-fpm
sudo systemctl enable php72-php-fpm
Solution 2
As explained by @runwuf, this is possible using the sofware collections available in centos-scl repository or in remi repository.
But using SetHandler to fastcgi proxy seems a better and more modern way, thanks to httpd 2.4:
SetHandler "proxy:fcgi://127.0.0.1:9000"
This is explained in some blog posts:
- My PHP Workstation on Remi's blog
- PHP Configuration Tips on RH developpers' blog
Solution 3
I had to add the following to my php.conf inside the directory statement to make the Apache Server API change to FPM/FastCGI instead of CGI/FastCGI - your solution was almost perfect though! Now if I could just figure out how to make it use a socket instead of TCP, I'd be one happy coder.
# mod_proxy_fcgi options
<IfModule mod_proxy_fcgi.c>
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9072"
</FilesMatch>
</IfModule>
Tabish
Updated on June 16, 2020Comments
-
Tabish almost 4 years
Can anyone here instruct me way to install and configure Multi PhP with one apache instance on CentOS 7, and the proper way to test it..
-
Tabish about 6 yearsyes the url you shared is what i want.. but doesnot show complete procedure.. and i guess it is for ubuntu.. I am a newbie.. Can't do it without help..
-
runwuf about 6 yearsIs there any reason that you need to run two versions of php within the same apache? is it not feasible that you run them in two different apache but the same machine? I think in most use case that is very close to what you need and a much easier and safer solution to manage in a long term.
-
Tabish about 6 yearsactullay it is a task given to me by me teacher.. I want to do it very badly.. I can't just backoff.. Please let me know if you find a solution for cgi mod..!!
-
runwuf about 6 yearsI spent a bit of time to try this out and have it working on centos7, see my new answer below, hope it helps!
-
Remi Collet almost 6 yearscgi wrapper is (IMHO) a deprecated wau, better to use the SetHandler to proxy way, will add another answer.
-
runwuf almost 6 yearsThank you Remi, great info! So to rewrite my proposed solution with
SetHandler
it would be something like this?<Directory /var/www/html/php56> SetHandler "proxy:fcgi://127.0.0.1:9056" </Directory> <Directory /var/www/html/php72> SetHandler "proxy:fcgi://127.0.0.1:9072" </Directory>
-
Javier S over 5 yearsWorked for me (with many changes) in Centos 6.9 and apache 2.2. Upvoted :)
-
Noberto Pessôa over 5 years@RemiCollet In a server with dozens of sites using .htaccess, should mod_php be disabled? What would have to be done is simply rename all ".htaccess" to ".user.ini"?
-
Remi Collet over 5 yearsNo, .user.ini have not the same syntax (no need of the php_flag directive)
-
runwuf about 5 yearsDid you try this? <Directory /var/www/html/php56> SetHandler "proxy:fcgi://127.0.0.1:9056" </Directory> <Directory /var/www/html/php72> SetHandler "proxy:fcgi://127.0.0.1:9072" </Directory>
-
Morgan almost 5 years@runwuf - yes, that is what I did, but that uses a TCP connection instead of a socket... IF I could use a socket, that would be preferable, unfortunately I never figured out the correct way to do so...
-
Craig London over 4 yearsAt my hosting company, there is a machine that has CentOS and PHP 5.6, this was fine, then PHP 7 came out, and I needed to have some vHosts run on PHP 5 and others on PHP 7, so they setup the "SetHandler" in the vHost for PHP 7, but now the X-FRAME-OPTION header I send from my application don't make it through the handler to be sent out to the browser, any suggestions?
-
bebbo over 4 yearseh - don't mix it with other repos (e.g. webtatic^^) - after disabling that other repo, it worked smooth, thanks a lot!