如何在 Debian 上的 Apache 2.4 中为每个虚拟主机选择 PHP 版本 5 和 7?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45033511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select PHP version 5 and 7 per virtualhost in Apache 2.4 on Debian?
提问by Z0q
Would it be possible to run PHP 7 and PHP 5 simultaneously in Apache 2.4 on Debian 9? I would like to be able to select the PHP version I wish to use per virtualhost. I believe this would be useful considering that some of my websites still use deprecated PHP features. This allows me to perform upgrades per site. How do I achieve something like this?
是否可以在 Debian 9 上的 Apache 2.4 中同时运行 PHP 7 和 PHP 5?我希望能够为每个虚拟主机选择我希望使用的 PHP 版本。考虑到我的一些网站仍然使用不推荐使用的 PHP 功能,我相信这会很有用。这允许我执行每个站点的升级。我如何实现这样的目标?
For example
例如
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName mywebsite.com
DocumentRoot /var/www/mywebsite.com
# UsePHP 7
</virtualHost>
And
和
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName mywebsite2.com
DocumentRoot /var/www/mywebsite2.com
# UsePHP 5
</virtualHost>
回答by Elvis Plesky
Let's start from beginning. I assume that you would prefer to use php-fpm instead of Apache module.
让我们从头开始。我假设您更喜欢使用 php-fpm 而不是 Apache 模块。
First install apache:
首先安装apache:
sudo apt-get update
sudo apt-get install apache2
Next install multiple PHP:
接下来安装多个PHP:
Debian 9:
Install PHP 7:
Debian 9:
安装 PHP 7:
sudo apt-get install php7.0-cli php7.0-fpm php-pear libapache2-mod-fastcgi
Configure repositories:
配置存储库:
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
Install PHP 5:
安装 PHP 5:
sudo apt-get install php5.6-cli php5.6-fpm
Debian 8:
Install PHP 5:
Debian 8:
安装 PHP 5:
sudo apt-get install php5 php5-fpm php-pear libapache2-mod-fastcgi
Configure repositories:
Edit /etc/apt/sources.list
and add the following lines to the end of file:
配置存储库:
编辑/etc/apt/sources.list
以下行并将其添加到文件末尾:
deb http://packages.dotdeb.org jessie all
deb-src http://packages.dotdeb.org jessie all
Install GPG key:
安装 GPG 密钥:
wget https://www.dotdeb.org/dotdeb.gpg
sudo apt-key add dotdeb.gpg
sudo apt-get update
Install PHP 7:
安装 PHP 7:
sudo apt-get install php7.0 php7.0-fpm
Next switch from prefork and enable necessary modules:
For Debian 8:
接下来从 prefork 切换并启用必要的模块:
对于 Debian 8:
a2dismod php5 mpm_prefork
For Debian 9:
对于 Debian 9:
a2dismod php7 mpm_prefork
Next for both:
接下来是:
a2enmod actions fastcgi alias proxy_fcgi mpm_worker
systemctl restart apache2
Change content of /etc/apache2/mods-enabled/fastcgi.conf
to the following one:
将 的内容更改/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>
Now create document root folders for websites:
现在为网站创建文档根文件夹:
mkdir -p /var/www/example.com/public_html
mkdir -p /var/www/test.com/public_html
Add sys users for these websites:
为这些网站添加 sys 用户:
sudo useradd example --home-dir /var/www/example.com
sudo useradd test --home-dir /var/www/test.com
Configure ownership:
配置所有权:
sudo chown -R example.example /var/www/example.com
sudo chown -R test.test /var/www/test.com
For example website example.com
will use PHP 5 and website test.com
will use PHP 7.
例如网站example.com
将使用 PHP 5,网站test.com
将使用 PHP 7。
Create configuration files for websites:
Website on PHP 5:
为网站创建配置文件:
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>
Website on PHP 7:
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>
Create pool configs (I used the following):
Website on PHP 5:
创建池配置(我使用了以下内容):
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 = /
Website on PHP 7:
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 = /
Restart apache and php-fpm services:
重启apache和php-fpm服务:
sudo systemctl restart apache2 php5-fpm php7.0-fpm
Enjoy!
享受!
回答by naitsirch
The answer by @elvis-pleskyis really detailled. But Apache suggeststhe usage of mod_proxy_fcgi
:
@elvis-plesky的回答非常详细。但 Apache建议使用mod_proxy_fcgi
:
With the release of apache httpd 2.4 upon an unsuspecting populace, we have gained some very neat functionality regarding apache and php: the ability to run PHP as a fastCGI process server, and address that fastCGI server directly from within apache, via a dedicated proxy module (mod_proxy_fcgi.)
随着 apache httpd 2.4 在毫无戒心的人群中发布,我们获得了一些关于 apache 和 php 的非常简洁的功能:能够将 PHP 作为 fastCGI 进程服务器运行,并通过专用代理模块直接从 apache 内部寻址该 fastCGI 服务器(mod_proxy_fcgi。)
Here are the steps to setup two vhosts with different PHP versions for Debian 10using Apache's mod_proxy_fcgi and php-fpm.
以下是使用 Apache 的 mod_proxy_fcgi 和 php-fpm为Debian 10设置两个具有不同 PHP 版本的虚拟主机的步骤。
Configure repositories:
配置存储库:
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
Install needed PHP versions:
安装所需的 PHP 版本:
sudo apt-get install php5.6-cli php5.6-fpm php7.3-cli php7.3-fpm
Configure php5.6-fpm in /etc/apache2/conf-available/php5.6-fpm.conf
:
在 php5.6-fpm 中配置/etc/apache2/conf-available/php5.6-fpm.conf
:
<IfModule !mod_php5.c>
<IfModule proxy_fcgi_module>
# Enable http authorization headers
<IfModule setenvif_module>
SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=
</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>
Configure php7.3-fpm in /etc/apache2/conf-available/php7.3-fpm.conf
:
在 php7.3-fpm 中配置/etc/apache2/conf-available/php7.3-fpm.conf
:
<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
# Enable http authorization headers
<IfModule setenvif_module>
SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=
</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>
Now add a virtual host that uses PHP 5.6:
现在添加一个使用 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>
And a virtual host that uses PHP 7.3:
还有一个使用 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>
Activate the correct Apache module and the configs:
激活正确的 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
The default configuration of the FPM pools should be correct already.
FPM 池的默认配置应该已经正确。