You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

解决方案:为不同子域名指定PHP版本

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 like user-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.sock is the default for Remi's PHP 7.2 packages. If yours is different (check the listen directive 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_proxy and mod_proxy_fcgi enabled:
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 AllowOverride is set to All (or at least FileInfo) so .htaccess rules 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 .htaccess to 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 configtest before 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.owner and listen.group in /etc/opt/remi/php72/php-fpm.d/www.conf are set to apache, then restart the FPM service.

内容的提问来源于stack exchange,提问作者Papa Lopez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:52:25