CentOS7开发服务器多PHP版本配置:按子域名指定版本
Hey there! Let's work through this PHP version setup you need for your CentOS 7 dev server. You've already got PHP 5.4 (mod_php) and PHP 7.2 (FastCGI) installed, so we just need to wire Apache to serve the right version for each subdomain. Here's how to do it reliably:
1. 先确认PHP 7.2 FPM服务正常运行
First things first—make sure your PHP 7.2 FastCGI Process Manager (FPM) is up and running, and set to start on boot:
systemctl start php72-php-fpm systemctl enable php72-php-fpm
Check its status to confirm it's active:
systemctl status php72-php-fpm
You should see active (running) in the output.
2. 配置Apache虚拟主机(推荐方式)
Since each user has their own subdomain, setting the PHP version directly in the virtual host config is the most stable approach—it avoids user-facing .htaccess issues and keeps control centralized.
Step-by-step:
- Locate the virtual host config file for the target subdomain. These are usually in
/etc/httpd/conf.d/or/etc/httpd/sites-available/, named something likeuser-subdomain.conf. - Open the file and add this block inside the
<VirtualHost>section to force the subdomain to use PHP 7.2 FPM:
<FilesMatch \.php$> SetHandler "proxy:unix:/run/php-fpm/php72-php-fpm.sock|fcgi://localhost/" </FilesMatch>
Note: The socket path
/run/php-fpm/php72-php-fpm.sockis the default for Remi's PHP 7.2 packages. If yours is different (check thelistendirective in/etc/opt/remi/php72/php-fpm.d/www.conf), replace it with your actual socket path.
- For subdomains that need to stay on PHP 5.4, leave out this block—Apache will automatically use the mod_php version you already have.
- Test your Apache config for syntax errors before restarting:
apachectl configtest
If you see Syntax OK, restart Apache to apply changes:
systemctl restart httpd
3. 修复.htaccess方法(可选)
If you want users to be able to set their own PHP version via .htaccess, you need to make sure Apache allows overrides and has the right modules loaded.
Step-by-step:
- Verify Apache has
mod_proxyandmod_proxy_fcgienabled:
apachectl -M | grep proxy
You should see both proxy_module and proxy_fcgi_module listed. If not, add these lines to /etc/httpd/conf/httpd.conf and restart Apache:
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
- In the subdomain's virtual host config, ensure
AllowOverrideis set toAll(or at leastFileInfo) so.htaccessrules are applied:
<Directory "/var/www/html/user-subdomain-root"> AllowOverride All # Keep your existing directory config here </Directory>
- Users can then add this to their site's root
.htaccessto switch to PHP 7.2:
<FilesMatch \.php$> SetHandler "proxy:unix:/run/php-fpm/php72-php-fpm.sock|fcgi://localhost/" </FilesMatch>
4. 验证配置生效
For each subdomain, create a simple info.php file in the document root:
<?php phpinfo(); ?>
Visit http://your-subdomain/info.php and check the PHP Version at the top of the page—you should see either 5.4 or 7.2 depending on your config.
重要注意事项
- Since PHP 5.4 is End of Life (EOL), prioritize migrating users to PHP 7.2 as soon as possible to avoid security risks.
- Always run
apachectl configtestbefore restarting Apache—bad configs can take your server offline. - If you get 502 Bad Gateway errors, check the permissions on the PHP 7.2 FPM socket. Make sure the
listen.ownerandlisten.groupin/etc/opt/remi/php72/php-fpm.d/www.confare set toapache, then restart the FPM service.
内容的提问来源于stack exchange,提问作者Papa Lopez

