如何在Debian上的Apache 2.4中为每个虚拟主机选择PHP版本5和7?

时间:2017-07-11 11:39:05

标签: php apache debian virtualhost php-7

是否可以在Debian 9上的Apache 2.4中同时运行PHP 7和PHP 5?我希望能够选择我希望每个虚拟主机使用的PHP版本。我相信考虑到我的一些网站仍然使用弃用的PHP功能,这将是有用的。这允许我对每个站点执行升级。我如何实现这样的目标?

例如

<VirtualHost *:80>
   ServerAdmin webmaster@localhost
   ServerName mywebsite.com
   DocumentRoot /var/www/mywebsite.com

   # UsePHP 7
</virtualHost>

<VirtualHost *:80>
   ServerAdmin webmaster@localhost
   ServerName mywebsite2.com
   DocumentRoot /var/www/mywebsite2.com

   # UsePHP 5
</virtualHost>

3 个答案:

答案 0 :(得分:31)

让我们从头开始。 我假设您更喜欢使用php-fpm而不是Apache模块。

首先安装apache:

sudo apt-get update
sudo apt-get install apache2

接下来安装多个PHP:

Debian 9:
安装PHP 7:

sudo apt-get install php7.0-cli php7.0-fpm php-pear libapache2-mod-fastcgi

配置存储库:

sudo apt-get install apt-transport-https
sudo curl https://packages.sury.org/php/apt.gpg | apt-key add -
sudo echo 'deb https://packages.sury.org/php/ stretch main' > /etc/apt/sources.list.d/deb.sury.org.list
sudo apt-get update

安装PHP 5:

sudo apt-get install php5.6-cli php5.6-fpm

Debian 8:
安装PHP 5:

sudo apt-get install php5 php5-fpm php-pear libapache2-mod-fastcgi

配置存储库:
编辑/etc/apt/sources.list并将以下行添加到文件末尾:

deb http://packages.dotdeb.org jessie all
deb-src http://packages.dotdeb.org jessie all

安装GPG密钥:

wget https://www.dotdeb.org/dotdeb.gpg
sudo apt-key add dotdeb.gpg
sudo apt-get update

安装PHP 7:

sudo apt-get install php7.0 php7.0-fpm

接下来从prefork切换并启用必要的模块:
对于Debian 8:

a2dismod php5 mpm_prefork

对于Debian 9:

a2dismod php7 mpm_prefork

两者的下一步:

a2enmod actions fastcgi alias proxy_fcgi mpm_worker
systemctl restart apache2

/etc/apache2/mods-enabled/fastcgi.conf的内容更改为以下内容:

<IfModule !mod_fastcgi.c>
    AddHandler fcgid-script fcg fcgi fpl
</IfModule>
<IfModule mod_fastcgi.c>
    <Directory /usr/lib/cgi-bin>
        Require all granted
    </Directory>
</IfModule>

现在为网站创建文档根文件夹:

mkdir -p /var/www/example.com/public_html
mkdir -p /var/www/test.com/public_html

为这些网站添加sys用户:

sudo useradd example --home-dir /var/www/example.com
sudo useradd test --home-dir /var/www/test.com

配置所有权:

sudo chown -R example.example /var/www/example.com
sudo chown -R test.test /var/www/test.com

例如,网站example.com将使用PHP 5,网站test.com将使用PHP 7.

为网站创建配置文件:
PHP 5网站:

touch /etc/apache2/sites-available/example.com.conf
ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/example.com.conf
cat /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <IfModule mod_fastcgi.c>
            AddType application/x-httpd-fastphp5 .php
            Action application/x-httpd-fastphp5 /php5-fcgi
            Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi-example.com
            FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi-example.com -socket /var/run/php5-fpm-example.com.sock -pass-header Authorization
        </IfModule>

</VirtualHost>

PHP 7网站:

touch /etc/apache2/sites-available/test.com.conf
ln -s /etc/apache2/sites-available/test.com.conf /etc/apache2/sites-enabled/test.com.conf
cat /etc/apache2/sites-available/test.com.conf
<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        ServerName test.com
        ServerAlias www.test.com
        DocumentRoot /var/www/test.com/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <IfModule mod_fastcgi.c>
                AddHandler php7-fcgi .php
                Action php7-fcgi /php7-fcgi virtual
                Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi-test.com
                FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi-test.com -socket /var/run/php/php7.0-fpm-test.com.sock -pass-header Authorization
        </IfModule>

</VirtualHost>

创建池配置(我使用以下内容):
PHP 5网站:

cat /etc/php5/fpm/pool.d/example.com.conf
[example.com]
user = example
group = example
listen = /var/run/php5-fpm-example.com.sock
listen.owner = www-data
listen.group = www-data
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /

PHP 7网站:

cat /etc/php/7.0/fpm/pool.d/test.com.conf
[test.com]
user = test
group = test
listen = /var/run/php/php7.0-fpm-test.com.sock
listen.owner = www-data
listen.group = www-data
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /

重启apache和php-fpm服务:

sudo systemctl restart apache2 php5-fpm php7.0-fpm

享受!

答案 1 :(得分:2)

@ elvis-plesky 的答案非常详细。但是Apache suggests使用mod_proxy_fcgi

  

随着毫无疑问的民众发布apache httpd 2.4,我们获得了有关apache和php的一些非常整洁的功能:能够将PHP作为fastCGI进程服务器运行,并可以通过apache直接在apache中解决该fastCGI服务器的功能。 专用代理模块(mod_proxy_fcgi。)

以下是使用Apache的mod_proxy_fcgi和php-fpm为 Debian 10 设置两个具有不同PHP版本的虚拟主机的步骤。

配置存储库:

sudo apt-get install apt-transport-https
sudo curl https://packages.sury.org/php/apt.gpg | apt-key add -
sudo echo 'deb https://packages.sury.org/php/ stretch main' > /etc/apt/sources.list.d/deb.sury.org.list
sudo apt-get update

安装所需的PHP版本:

sudo apt-get install php5.6-cli php5.6-fpm php7.3-cli php7.3-fpm

/etc/apache2/conf-available/php5.6-fpm.conf中配置php5.6-fpm:

<IfModule !mod_php5.c>
<IfModule proxy_fcgi_module>
    # Enable http authorization headers
    <IfModule setenvif_module>
        SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>

    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://php56.localhost"
    </FilesMatch>
    # Deny access to files without filename (e.g. '.php')
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

/etc/apache2/conf-available/php7.3-fpm.conf中配置php7.3-fpm:

<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
    # Enable http authorization headers
    <IfModule setenvif_module>
        SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>

    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://php73.localhost"
    </FilesMatch>
    # Deny access to files without filename (e.g. '.php')
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

现在添加使用PHP 5.6的虚拟主机:

<VirtualHost *:80>
    ServerName test-php56.localhost
    DocumentRoot "/var/www/test-php56/"
    # use php 5.6
    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://php56.localhost"
    </FilesMatch>
</VirtualHost>

以及使用PHP 7.3的虚拟主机:

<VirtualHost *:80>
    ServerName test-php73.localhost
    DocumentRoot "/var/www/test-php73/"
    # use php 7.3
    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://php73.localhost"
    </FilesMatch>
</VirtualHost>

激活正确的Apache模块和配置:

sudo a2enmod proxy_fcgi
sudo a2enconf php5.6-fpm
sudo a2enconf php7.3-fpm
sudo systemctl restart apache2 php5.6-fpm php7.3-fpm

FPM池的默认配置应该已经正确。

答案 2 :(得分:-5)

根据猫王的惊人回答,这似乎是可能的。

但我更愿意使用两个虚拟服务器,每个服务器一个虚拟主机,而不是一台服务器上的两个虚拟主机。这将更容易配置。