Multiple PHP version with Apache on CentOS 7

30/03/2025

499 lượt xem

Install all the necessary repos and packages

The following commands assume you already sudo su - or you will have to add sudo to each of the commands:

typescript
1yum install httpd -y
2yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
3yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
4yum install yum-utils -y
5yum install php56 -y
6yum install php72 -y
7yum install php56-php-fpm -y
8yum install php72-php-fpm -y

Stop both fpm servers

typescript
1systemctl stop php56-php-fpm
2systemctl stop php72-php-fpm

By default it listens on 127.0.0.1 port 9000, make them listen on different ports

typescript
1sed -i 's/:9000/:9056/' /etc/opt/remi/php56/php-fpm.d/www.conf
2sed -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

typescript
1systemctl start php72-php-fpm
2systemctl start php56-php-fpm

make script wrapper to call php56-cgi and php72-cgi

typescript
1cat > /var/www/cgi-bin/php56.fcgi << EOF
2#!/bin/bash
3exec /bin/php56-cgi
4EOF
5 
6cat > /var/www/cgi-bin/php72.fcgi << EOF
7#!/bin/bash
8exec /bin/php72-cgi
9EOF

Make them executable by apache

typescript
1sudo chmod 755 /var/www/cgi-bin/php56.fcgi
2sudo chmod 755 /var/www/cgi-bin/php72.fcgi

Create php configuration for apache. by default it runs php56-fcgi handler

typescript
1cat > /etc/httpd/conf.d/php.conf << EOF
2ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
3AddHandler php56-fcgi .php
4Action php56-fcgi /cgi-bin/php56.fcgi
5Action php72-fcgi /cgi-bin/php72.fcgi
6 
7<Directory /var/www/html/php56>
8 DirectoryIndex index.php
9 AllowOverride all
10 Require all granted
11</Directory>
12<Directory /var/www/html/php72>
13 DirectoryIndex index.php
14 AllowOverride all
15 Require all granted
16</Directory>
17EOF

Make test pages, create .htaccess to use php72-fcgi

typescript
1mkdir -p /var/www/html/php56
2mkdir -p /var/www/html/php72
3echo "<?php phpinfo(); ?>" > /var/www/html/php56/index.php
4echo "<?php phpinfo(); ?>" > /var/www/html/php72/index.php
5echo "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

typescript
1sudo systemctl enable httpd
2sudo systemctl enable php56-php-fpm
3sudo systemctl enable php72-php-fpm

Bài viết liên quan

Nền tảng các kiến thức học tập

Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.

Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!

Khám phá, trải nghiệm ngay
NenTang - Hành trang tới tương lai