CentOS 7中基于单Apache实例部署多PHP版本的安装配置及测试方法咨询
Hey there! Setting up multiple PHP versions on a single Apache instance in CentOS 7 is totally doable—perfect when you need to run older apps that rely on PHP 7.x alongside newer projects using PHP 8.x. Let's break this down into simple, actionable steps.
First, make sure your system is up to date and has the right repositories to get multiple PHP versions (since CentOS 7's default repo only has an old PHP version).
- Update your packages:
sudo yum update -y - Install EPEL and Remi repositories (Remi is key for getting modern PHP versions):
sudo yum install -y epel-release yum-utilssudo yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm - Enable the Remi repos for the PHP versions you want. For example, if you need PHP 7.4 and 8.2:
sudo yum-config-manager --enable remi-php74sudo yum-config-manager --enable remi-php82
Remi packages use a phpXX- prefix so different versions don't conflict. Let's install PHP 7.4 and 8.2 along with common extensions and FPM (since we'll use FPM to connect Apache to each PHP version):
- Install PHP 7.4 and FPM:
sudo yum install -y php74 php74-php-fpm php74-php-cli php74-php-mysqlnd php74-php-gd - Install PHP 8.2 and FPM:
sudo yum install -y php82 php82-php-fpm php82-php-cli php82-php-mysqlnd php82-php-gd
PHP-FPM is what handles PHP processing for Apache. We'll set each version to listen on a unique port so Apache can tell them apart.
- Start and enable the FPM services so they run on boot:
sudo systemctl start php74-php-fpmsudo systemctl enable php74-php-fpmsudo systemctl start php82-php-fpmsudo systemctl enable php82-php-fpm - Edit the FPM config for PHP 7.4 to set its listening port:
sudo vi /etc/opt/remi/php74/php-fpm.d/www.conf
Find thelistenline and change it to:listen = 127.0.0.1:9074 - Do the same for PHP 8.2:
sudo vi /etc/opt/remi/php82/php-fpm.d/www.conf
Set:listen = 127.0.0.1:9082 - Restart both FPM services to apply changes:
sudo systemctl restart php74-php-fpmsudo systemctl restart php82-php-fpm
Now we need to tell Apache which PHP version to use for each website. We'll use virtual hosts for this.
- First, make sure Apache's
proxy_fcgiandsetenvifmodules are enabled (these let Apache communicate with PHP-FPM):
Check if they're loaded withsudo httpd -M | grep -E "proxy_fcgi|setenvif". If not, enable them by ensuring the lines in/etc/httpd/conf.modules.d/00-proxy.confare uncommented, then restart Apache withsudo systemctl restart httpd. - Create virtual host configs for each site. Let's make one for PHP 7.4 and one for PHP 8.2:
Create/etc/httpd/conf.d/php74-site.confwith this content:
Create<VirtualHost *:80> ServerName php74.local DocumentRoot /var/www/php74-site <Directory /var/www/php74-site> AllowOverride All Require all granted </Directory> # Send PHP requests to PHP 7.4 FPM <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9074" </FilesMatch> ErrorLog /var/log/httpd/php74-site-error.log CustomLog /var/log/httpd/php74-site-access.log combined </VirtualHost>/etc/httpd/conf.d/php82-site.confwith this:<VirtualHost *:80> ServerName php82.local DocumentRoot /var/www/php82-site <Directory /var/www/php82-site> AllowOverride All Require all granted </Directory> # Send PHP requests to PHP 8.2 FPM <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9082" </FilesMatch> ErrorLog /var/log/httpd/php82-site-error.log CustomLog /var/log/httpd/php82-site-access.log combined </VirtualHost> - Create the website directories and set correct permissions:
sudo mkdir -p /var/www/php74-site /var/www/php82-sitesudo chown -R apache:apache /var/www/php74-site /var/www/php82-site
Let's verify that each site uses the correct PHP version.
- Restart Apache to apply all config changes:
sudo systemctl restart httpd - Create a
phpinfo()file for each site to check the PHP version:
For PHP 7.4:echo "<?php phpinfo(); ?>" | sudo tee /var/www/php74-site/info.php
For PHP 8.2:echo "<?php phpinfo(); ?>" | sudo tee /var/www/php82-site/info.php - If you're testing locally, add these entries to your
/etc/hostsfile to map the domain names to your server:127.0.0.1 php74.local127.0.0.1 php82.local - Now visit
http://php74.local/info.phpin your browser—you should see PHP 7.4 listed at the top. Do the same forhttp://php82.local/info.phpand confirm it shows PHP 8.2. - You can also test from the command line:
php74 -vshould output PHP 7.4.xphp82 -vshould output PHP 8.2.x
- If Apache won't start, check the error log:
sudo tail -f /var/log/httpd/error_log - If PHP pages show raw code instead of executing, make sure
proxy_fcgiis enabled and the FPM service is running. - Verify FPM is listening on the correct port:
sudo netstat -tulpn | grep php-fpm
内容的提问来源于stack exchange,提问作者Tabish

