CentOS7/Apache下已有PHP7时安装PHP5.6并配置特定虚拟主机
Hey there! I’ve helped a bunch of folks set up multiple PHP versions on CentOS 7 with Apache, so let’s walk through this step by step to get your PHP 5.6-only code running on a specific virtual host while keeping PHP 7 intact.
1. Install PHP 5.6 alongside your existing PHP 7
CentOS 7’s default repos don’t include PHP 5.6, so we’ll use the Remi repository (a trusted source for updated PHP packages). Here’s how to get it set up:
- First, install the required repository files:
sudo yum install -y epel-release sudo yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm - Enable the Remi repo specifically for PHP 5.6:
sudo yum-config-manager --enable remi-php56 - Install PHP 5.6 and common modules (note the
php56-prefix to avoid conflicts with your existing PHP 7):sudo yum install -y php56 php56-php-cli php56-php-fpm php56-php-mysqlnd php56-php-gd php56-php-xml php56-php-mbstring - Start the PHP 5.6 FPM service and set it to run automatically on system boot:
sudo systemctl start php56-php-fpm sudo systemctl enable php56-php-fpm - Verify the service is running (look for "active (running)" in the output):
sudo systemctl status php56-php-fpm
2. Configure your specific virtual host to use PHP 5.6
We’ll use Apache’s mod_proxy_fcgi to route PHP requests for your target virtual host to the PHP 5.6 FPM process. Here’s the detailed setup:
- First, make sure
mod_proxy_fcgiis installed and enabled:sudo yum install -y mod_proxy_fcgi sudo systemctl restart httpd - Locate your Apache virtual host configuration directory (usually
/etc/httpd/conf.d/or/etc/httpd/sites-available/). Create or edit the config file for your target domain (e.g.,legacy-app.conf):
Important note: To confirm the correct listen path for PHP 5.6 FPM, run this command:<VirtualHost *:80> ServerName your-legacy-app-domain.com ServerAlias www.your-legacy-app-domain.com DocumentRoot /var/www/legacy-app/public_html # Disable mod_php to prevent PHP 7 from handling requests for this domain php_admin_flag engine off # Route PHP requests to PHP 5.6 FPM <FilesMatch \.php$> # Check your PHP 5.6 FPM listen path first (see note below) SetHandler "proxy:unix:/run/php56-php-fpm/www.sock|fcgi://localhost" </FilesMatch> # Logging configuration (adjust paths to your preference) ErrorLog /var/log/httpd/legacy-app_error.log CustomLog /var/log/httpd/legacy-app_access.log combined </VirtualHost>
If it returns a port likegrep 'listen =' /etc/opt/remi/php56/php-fpm.d/www.conf127.0.0.1:9000instead of a socket path, update theSetHandlerline to:SetHandler "proxy:fcgi://127.0.0.1:9000" - Create your app’s document root and set proper permissions for Apache:
sudo mkdir -p /var/www/legacy-app/public_html sudo chown -R apache:apache /var/www/legacy-app/public_html sudo chmod -R 755 /var/www/legacy-app/public_html - Test your Apache configuration for syntax errors:
If you seesudo apachectl configtestSyntax OK, restart Apache to apply the changes:sudo systemctl restart httpd - Verify the setup works: Create a
info.phpfile in your app’s document root with this content:
Visit<?php phpinfo(); ?>http://your-legacy-app-domain.com/info.phpin your browser—you should see PHP 5.6 listed as the active version.
内容的提问来源于stack exchange,提问作者user3181303
相关产品推荐
相关产品推荐

