在同一台服务器上运行两个 PHP 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42696856/
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
Running two PHP versions on the same server
提问by Makubex
I have two projects on the local server, one project is running PHP5.6 and the other one is running PHP7.0. Now would it be possible to enable this two versions based on the projects? I already tried adding AddHandler application/x-httpd-php7 .php
in one of the project htaccess but its not working. Currently, PHP7.0 and PHP5.6-fpm already installed on the server. Below is the screenshot of the phpinfo.
我在本地服务器上有两个项目,一个项目运行 PHP5.6,另一个项目运行 PHP7.0。现在可以根据项目启用这两个版本吗?我已经尝试添加AddHandler application/x-httpd-php7 .php
项目 htaccess 之一,但它不起作用。目前,服务器上已经安装了 PHP7.0 和 PHP5.6-fpm。下面是phpinfo的截图。
回答by Makubex
So after searching on Google for the whole day. I managed to run my two projects in FastCgi on different php versions. Thanks to the guys from this forum. I uninstalled everything including Apache and start over again. Below are the steps I used to enable two versions of PHP on my local server. Btw, my computer is running on Linux Mint 18.
所以在谷歌搜索了一整天之后。我设法在不同 php 版本的 FastCgi 中运行我的两个项目。感谢这个论坛的人。我卸载了包括 Apache 在内的所有东西,然后重新开始。下面是我用来在本地服务器上启用两个版本的 PHP 的步骤。顺便说一句,我的电脑在 Linux Mint 18 上运行。
Assuming you already installed Apache, created virtual host for the two projects and added the necessary php PPAs. Let's call the projects
site56.local
for PHP 5.6 andsite70.local
for PHP 7.0. Installphp5.6-fpm
andphp7.0-fpm
by running:sudo apt-get install php5.6-fpm sudo apt-get install php7.0-fpm
Create two files under
/usr/lib/cgi-bin/
(honestly I don't know if this step is still necessary), and save:sudo nano /usr/lib/cgi-bin/php56-fcgi sudo nano /usr/lib/cgi-bin/php70-fcgi
Open php56 conf file
/etc/apache2/conf-available/php5.6-fpm.conf
, add this config and save:<IfModule mod_fastcgi.c> AddHandler php56-fcgi .php Action php56-fcgi /php56-fcgi Alias /php56-fcgi /usr/lib/cgi-bin/php56-fcgi -socket /var/run/php/php5.6-fpm.sock -pass-header Authorization Action php70-fcgi /php70-fcgi Alias /php70-fcgi /usr/lib/cgi-bin/php70-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization </IfModule> <Directory /usr/lib/cgi-bin> Require all granted </Directory>
Now enable the new apache config:
sudo a2enconf php5.6-fpm
If you installed php5.6 and php5.7, make sure you disable this two and restart apache:
sudo a2dismod php5.6 php7.0 sudo systemctl restart apache2
Create a
.htacces
file on the project that should run on php7.0 and add this handler:AddHandler php70-fcgi .php
Now create a phpinfo file on the two projects and if you see something like this, then congratulations!
假设您已经安装了 Apache,为两个项目创建了虚拟主机并添加了必要的 php PPA。让我们调用
site56.local
PHP 5.6 和site70.local
PHP 7.0 的项目。安装php5.6-fpm
并php7.0-fpm
运行:sudo apt-get install php5.6-fpm sudo apt-get install php7.0-fpm
在下创建两个文件
/usr/lib/cgi-bin/
(老实说我不知道这一步是否还需要),并保存:sudo nano /usr/lib/cgi-bin/php56-fcgi sudo nano /usr/lib/cgi-bin/php70-fcgi
打开 php56 conf 文件
/etc/apache2/conf-available/php5.6-fpm.conf
,添加此配置并保存:<IfModule mod_fastcgi.c> AddHandler php56-fcgi .php Action php56-fcgi /php56-fcgi Alias /php56-fcgi /usr/lib/cgi-bin/php56-fcgi -socket /var/run/php/php5.6-fpm.sock -pass-header Authorization Action php70-fcgi /php70-fcgi Alias /php70-fcgi /usr/lib/cgi-bin/php70-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization </IfModule> <Directory /usr/lib/cgi-bin> Require all granted </Directory>
现在启用新的 apache 配置:
sudo a2enconf php5.6-fpm
如果安装了 php5.6 和 php5.7,请确保禁用这两个并重新启动 apache:
sudo a2dismod php5.6 php7.0 sudo systemctl restart apache2
.htacces
在应该在 php7.0 上运行的项目上创建一个文件并添加此处理程序:AddHandler php70-fcgi .php
现在在这两个项目上创建一个 phpinfo 文件,如果你看到这样的东西,那么恭喜!
PS: Make sure you enable htaccess in your apache2.conf or httpd.conf
PS:确保在 apache2.conf 或 httpd.conf 中启用 htaccess
回答by Jun Xie
First of all, ensure all the PHP related configraution are disabled by run the following commands:
首先,通过运行以下命令确保禁用所有与 PHP 相关的配置:
# ls -la /etc/apache2/conf-enabled | grep php
# ls -la /etc/apache2/conf-enabled | grep php
# ls -la /etc/apache2/mods-enabled | grep php
# ls -la /etc/apache2/mods-enabled | grep php
Set up a different version of PHP-FPM for a specific site:
为特定站点设置不同版本的 PHP-FPM:
Add the following line in your existing VirtualHost file.
在现有的 VirtualHost 文件中添加以下行。
Include "conf-available/php7.2-fpm.conf"
For example,
例如,
# vim /etc/apache2/sites-enabled/symfony.local.conf
# vim /etc/apache2/sites-enabled/symfony.local.conf
<VirtualHost *:80>
ServerName symfony.local
Include "conf-available/php7.2-fpm.conf"
ServerAdmin webmaster@localhost
DocumentRoot /var/www/symfony.local/curr/public
<Directory /var/www/symfony.local/curr/web>
AllowOverride All
</Directory>
</VirtualHost>
回答by JGL
It doesn't work for debian 9 Stretch. It took me a while to firgure out what to do but at the end I found a solution which seems even easier :
它不适用于 debian 9 Stretch。我花了一段时间才弄清楚该怎么做,但最后我找到了一个似乎更容易的解决方案:
sudo apt-get install php5.6-fpm
sudo apt-get install php7.0-fpm
sudo a2enconf php5.6-fpm
If you installed php5.6 and php5.7, make sure you disable this two and restart apache.
如果您安装了php5.6和php5.7,请确保禁用这两个并重新启动apache。
sudo a2dismod php5.6 php7.0
sudo systemctl restart apache2
At this point all of your sites must work on php 5.6.
此时,您的所有站点都必须在 php 5.6 上运行。
For the sites who need php 7, add this line in the vhost :
对于需要 php 7 的站点,请在 vhost 中添加以下行:
ProxyPassMatch "^/(.*\.php(/.*)?)$" "unix:/var/run/php/php7.0-fpm.sock|fcgi://localhost/path/to/my/main/file"
It should do the trick :)
它应该可以解决问题:)
回答by aminakbari
I tried to implement Makubex's solution and it worked. There is just one problem I had with a newly installed LAMP and that was the fact that fastcgi was not enabled. I just installed php again using the following script and enabled fastcgi afterwards.
我尝试实施 Makubex 的解决方案,并且奏效了。我新安装的 LAMP 只有一个问题,那就是没有启用 fastcgi。我刚刚使用以下脚本再次安装了 php,然后启用了 fastcgi。
sudo apt-get install libapache2-mod-fastcgi php5.6-fpm php5.6 php5.6-dev php5.6-mcrypt php5.6-mbstring php5.6-mysql php5.6-zip php5.6-gd php5.6-xml php7.1-fpm libapache2-mod-fastcgi php7.1-fpm php7.1 php7.1-dev php7.1-mbstring php7.1-mysql php7.1-zip php7.1-gd php7.1-xml php7.1-curl php7.1-intl php7.1-json php7.1-mcrypt
And then:
进而:
a2enmod actions
a2enmod fastcgi
After this, the solution worked perfectly. I got the two above from Run multiple PHP version on the same server using php-fpm and xdebug on Ubuntu 16.04
在此之后,该解决方案完美运行。我在 Ubuntu 16.04 上使用 php-fpm 和 xdebug 在同一台服务器上运行多个 PHP 版本,得到了上面的两个
回答by Hiren Makwana
You can do this using .htaccess files
您可以使用 .htaccess 文件执行此操作
First You need to install requred FPM using
首先,您需要使用安装所需的 FPM
sudo apt install php7.2-fpm
Then run below command
然后运行下面的命令
sudo a2enmod actions alias proxy_fcgi fcgid
Then Restart Your server
然后重启你的服务器
sudo systemctl restart apache
Then do below changes in htacess file
然后在 htacess 文件中做以下更改
<FilesMatch \.php$>
# Apache 2.4.10+ can proxy to unix socket
SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
</FilesMatch>
Here is example of running php 7.2 when php 5.6 is enabled
这是启用 php 5.6 时运行 php 7.2 的示例
回答by MagePsycho
We have got this covered in case of PHP on Ubuntu + Nginx:
https://blog.magepsycho.com/how-to-install-multiple-versions-of-php-on-ubuntu/
如果在 Ubuntu + Nginx 上使用 PHP,我们已经涵盖了这一点:https:
//blog.magepsycho.com/how-to-install-multiple-versions-of-php-on-ubuntu/
To change CLI PHP version
更改 CLI PHP 版本
sudo update-alternatives --set php /usr/bin/php7.2
To change Web PHP version
更改 Web PHP 版本
fastcgi_pass unix:/run/php/php7.2-fpm.sock
Where php7.2 can be replaced by the PHP version of your choice.
其中 php7.2 可以替换为您选择的 PHP 版本。
回答by Danial
This is made a lot harder by all of the linux package stuff muddying the process. The easy manual, system independent way:
所有 linux 包的东西都混淆了这个过程,这使得这个过程变得更加困难。简单的手动、系统独立方式:
Build your 2 versions of PHP from source. You'll have say libphp7.so and libphp5.so in your modules directory. Just run a separate httpd.conf file for each. I ran 5 and 7 for years:
从源代码构建您的 2 个版本的 PHP。你会在你的模块目录中说 libphp7.so 和 libphp5.so 。只需为每个运行一个单独的 httpd.conf 文件。我跑了 5 和 7 多年:
httpd-php7.conf
httpd-php5.conf
then. just put your virtual hosts that can't run 7 in the php5 config and either start them manually at boot or create the rc.d files or whatever your system uses for startup.
然后。只需将无法运行 7 的虚拟主机放在 php5 配置中,然后在启动时手动启动它们或创建 rc.d 文件或系统用于启动的任何文件。
回答by saschadd
After trying to follow your directions I found a possibly easier way for the same result.
在尝试按照您的指示进行操作后,我找到了一种可能更简单的方法来获得相同的结果。
By adding the following line
通过添加以下行
ProxyPassMatch "^/php70project/.*\.php(/.*)?$" "fcgi://localhost:9070/var/www/" enablereuse=on
in the apache vhost configuration, you could change the php version inside the directory php70project in your vhost root /var/www to version 7 running as fastcgi.
在 apache vhost 配置中,您可以将 vhost 根目录 /var/www 中的 php70project 目录中的 php 版本更改为以 fastcgi 运行的版本 7。
The following approach for use with unix socket
以下方法与 unix socket 一起使用
ProxyPassMatch "^/php70project/.*\.php(/.*)?$" "unix:/var/run/php70fpm.sock|fcgi://localhost/var/www"
is not working as intended. Unfortunately i couldnt find the correct code to get this working but as there is no example for unix socket use with subfolder on that page https://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.htmlit might just not be possible.
没有按预期工作。不幸的是,我找不到正确的代码来让它工作,但是因为在该页面https://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html上没有使用子文件夹的 unix socket 示例,它可能不是可能的。