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

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.

1. Prep Your System

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-utils
    sudo 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-php74
    sudo yum-config-manager --enable remi-php82
2. Install Multiple PHP Versions

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
3. Configure PHP-FPM for Each Version

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-fpm
    sudo systemctl enable php74-php-fpm
    sudo systemctl start php82-php-fpm
    sudo 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 the listen line 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-fpm
    sudo systemctl restart php82-php-fpm
4. Configure Apache to Route to the Right PHP Version

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_fcgi and setenvif modules are enabled (these let Apache communicate with PHP-FPM):
    Check if they're loaded with sudo httpd -M | grep -E "proxy_fcgi|setenvif". If not, enable them by ensuring the lines in /etc/httpd/conf.modules.d/00-proxy.conf are uncommented, then restart Apache with sudo 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.conf with this content:
    <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>
    
    Create /etc/httpd/conf.d/php82-site.conf with 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-site
    sudo chown -R apache:apache /var/www/php74-site /var/www/php82-site
5. Test Everything Works

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/hosts file to map the domain names to your server:
    127.0.0.1 php74.local
    127.0.0.1 php82.local
  • Now visit http://php74.local/info.php in your browser—you should see PHP 7.4 listed at the top. Do the same for http://php82.local/info.php and confirm it shows PHP 8.2.
  • You can also test from the command line:
    php74 -v should output PHP 7.4.x
    php82 -v should output PHP 8.2.x
Quick Troubleshooting Tips
  • 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_fcgi is enabled and the FPM service is running.
  • Verify FPM is listening on the correct port: sudo netstat -tulpn | grep php-fpm

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:34:03