告诉 Apache 使用使用 phpbrew 安装的特定 PHP 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20584156/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:19:08  来源:igfitidea点击:

Tell Apache to use a specific PHP version installed using phpbrew

phpapachemultiple-versions

提问by Vinay Aggarwal

I had the PHP, MySQL, and Apache stack installed for development. That installation is using configuration files from:

我为开发安装了 PHP、MySQL 和 Apache 堆栈。该安装使用的配置文件来自:

/etc/apache2/
/etc/php5/

Later I installed multiple PHP version using phpbrew. All versions are accessible and switchable from CLI. But Apache always stays on the default version that was not installed using phpbrew.

后来我安装了多个 PHP 版本,使用phpbrew. 所有版本都可以从 CLI 访问和切换。但是 Apache 始终保留未使用phpbrew安装的默认版本。

Here is a list of my installed PHP versions.

这是我安装的 PHP 版本的列表。

$ phpbrew list
Installed versions:
  php-5.4.13       (/home/admin1/.phpbrew/php/php-5.4.13)
                   +default -- --with-bz2=/usr
  php-5.5.5        (/home/admin1/.phpbrew/php/php-5.5.5)

  php-5.3.27       (/home/admin1/.phpbrew/php/php-5.3.27)

I have tried changing configuration file paths so they point to phpbrew's PHP. But nothing seems to be working.

我尝试更改配置文件路径,以便它们指向 phpbrew 的 PHP。但似乎没有任何效果。

How can I tell Apache to use phpbrew's PHP version?

如何告诉 Apache 使用 phpbrew 的 PHP 版本?

回答by c9s

You need to build a PHP with apxs2:

您需要使用以下命令构建 PHP apxs2

1)Ensure your have installed sudo apt-get install apache2-dev.

1)确保您已安装sudo apt-get install apache2-dev.

2)Run phpbrew install 5.4.22 +apxs2=/usr/bin/apxs2

2)运行phpbrew install 5.4.22 +apxs2=/usr/bin/apxs2

Then you should see the built module file in your Apache configuration file.

然后您应该会在 Apache 配置文件中看到构建的模块文件。

回答by Luke Rodgers

I scripted this, because it was annoying me.

我写了这个,因为它让我很烦。

By default phpbrew switchwill change the CLI version. To update Apache, you will have to tell it to use the newly generated .sofile. On Ubuntu this file will be created like /usr/lib/apache2/modules/libphp$VERSION.so.

默认情况下phpbrew switch将更改 CLI 版本。要更新 Apache,您必须告诉它使用新生成的.so文件。在 Ubuntu 上,这个文件将像/usr/lib/apache2/modules/libphp$VERSION.so.

For this .sofile to be generated, you have to install PHP like:

.so要生成此文件,您必须像这样安装 PHP:

phpbrew install php-5.6.16 +default +apxs2

Anyway, here's the shell script I use to switch PHP versions. The switch will fail if the .sofile cannot be found, and it will request sudoprivileges to restart Apache.

无论如何,这是我用来切换 PHP 版本的 shell 脚本。如果.so找不到该文件,则切换将失败,并会请求sudo重新启动 Apache 的权限。

/home/luker/bin/phpbrewswitch

/home/luker/bin/phpbrewswitch

#!/usr/bin/env bash
VERSION=

SOFILE=/usr/lib/apache2/modules/libphp$VERSION.so
CONFFILE5=/etc/apache2/mods-available/php5.load
CONFFILE7=/etc/apache2/mods-available/php7.load

source ~/.phpbrew/bashrc

if [ -f $SOFILE ]; then
    phpbrew switch $VERSION
    phpbrew list

    if [[ $VERSION == 7* ]]; then
        FILECONTENTS="LoadModule php7_module $SOFILE"
        CONFFILE=$CONFFILE7
        sudo a2enmod php7
        sudo a2dismod php5
    else
        FILECONTENTS="LoadModule php5_module $SOFILE"
        CONFFILE=$CONFFILE5
        sudo a2enmod php5
        sudo a2dismod php7
    fi;

    echo $FILECONTENTS > $CONFFILE
    echo "AddType application/x-httpd-php .php" >> $CONFFILE
    echo "Updated $CONFFILE"
    sudo service apache2 restart

else
    echo $VERSION "is not configured for apache"
    phpbrew list
fi

Usage

用法

Attempting to switch to a PHP version that wasn't built for Apache:

尝试切换到不是为 Apache 构建的 PHP 版本:

[21:02:55] luker [~]$ phpbrewswitch 5.4.45
5.4.45 is not configured for apache
  php-5.6.16
  php-5.6.10
* php-5.5.30
  php-5.4.45

Successfully changing to a PHP version that has an existing .sofile:

成功更改为具有现有.so文件的 PHP 版本:

[21:03:55] luker [~]$ phpbrewswitch 5.6.16
* php-5.6.16
  php-5.6.10
  php-5.5.30
  php-5.4.45
Updated /etc/apache2/mods-available/php5.load

回答by aWebDeveloper

Do look into Server Fault post How do I tell Apache which PHP to use?.

请查看服务器故障帖子如何告诉 Apache 使用哪个 PHP?.

You need to specify the PHP version in Apache.

您需要在 Apache 中指定 PHP 版本。

回答by thaddeusmt

The solution I found for managing multiple PHP versions with an Apache server is to run PHP-FPM and FastCGI instead of mod_php. This way I can have multiple versions of PHP running, and select which sites on my development machine I want to run which version of PHP.

我找到的使用 Apache 服务器管理多个 PHP 版本的解决方案是运行 PHP-FPM 和 FastCGI 而不是mod_php. 这样我就可以运行多个版本的 PHP,并在我的开发机器上选择我想要运行哪个版本的 PHP 的站点。

You will need to recompile your PHP versions with the +fpmphpbrew flag instead of +apxs2, start them with the phpbrew fpm startcommand, install the Apache libapache2-mod-fastcgipackage, and probably disable apache mod_php- but it's pretty slick once it's working. I can test the same site with multiple versions of PHP just by configuring a different Virtual host pointing to the same code, but different PHP-FPM sockets.

您将需要使用+fpmphpbrew 标志而不是重新编译您的 PHP 版本,+apxs2使用phpbrew fpm start命令启动它们,安装 Apachelibapache2-mod-fastcgi包,并可能禁用 apache mod_php- 但它一旦运行就非常流畅。我可以通过配置指向相同代码但不同 PHP-FPM 套接字的不同虚拟主机来使用多个版本的 PHP 测试同一个站点。

Here's an example of an Apache 2.4 /etc/apache2/mods-enables/fastcgi.confconfiguration file with 2 versions of PHP installed via phpbrew:

这是一个 Apache 2.4/etc/apache2/mods-enables/fastcgi.conf配置文件的示例,其中包含通过 phpbrew 安装的 2 个版本的 PHP:

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  FastCgiIpcDir /var/lib/apache2/fastcgi

  AddType application/x-httpd-fastphp5 .php
  Action application/x-httpd-fastphp5 /php5-fcgi

  # This is for php 5.6.28
  FastCgiExternalServer /usr/lib/cgi-bin/php56-cgi -socket /home/{USERNAME}/.phpbrew/php/php-5.6.28/var/run/php-fpm.sock -pass-header Authorization

  # This is for php 7.0.13
  FastCgiExternalServer /usr/lib/cgi-bin/php70-cgi -socket /home/{USERNAME}/.phpbrew/php/php-7.0.13/var/run/php-fpm.sock -pass-header Authorization

  # this seems to be required by Apache 2.4.10
  <Directory /usr/lib/cgi-bin>
    Require all granted
  </Directory>
</IfModule>

Then in your apache "site" Virtualhost configuration you can specify which PHP version to run with an Alias like so:

然后在您的 apache“站点”Virtualhost 配置中,您可以指定使用别名运行的 PHP 版本,如下所示:

<VirtualHost *:80>
  # ServerName, ServerAdmin, etc
  DocumentRoot /var/www/my-site-code
  # Then point the php5-fcgi handler to a specific version of PHP
  # Here is PHP7, as defined in the fastcgi.conf file
  Alias /php5-fcgi /usr/lib/cgi-bin/php70-cgi
</VirtualHost>

回答by OB7DEV

If phpbrew successfully installs php version with +apxs2 ext, you should have a new ".so" file it inside apache2's module library (usr/lib/apache2/modules not /etc/apache2/mods-available).

如果 phpbrew 使用 +apxs2 ext 成功安装了 php 版本,您应该在 apache2 的模块库中创建一个新的“.so”文件(usr/lib/apache2/modules 而不是 /etc/apache2/mods-available)。

There should be a php5.load or php7.load created inside /etc/apache2/mods-available folder that points to the mentioned .so file.

应该在 /etc/apache2/mods-available 文件夹中创建一个 php5.load 或 php7.load 指向提到的 .so 文件。

The .so file listed in those .load files is the version that get's loaded.

这些 .load 文件中列出的 .so 文件是加载的版本。

To switch between a php5 and php7 version use a2enmod/a2dismod php5 php7.

要在 php5 和 php7 版本之间切换,请使用 a2enmod/a2dismod php5 php7。

You need to reload apache2 after changing the config.

更改配置后,您需要重新加载 apache2。

If php files are rendering as plain text, you need to either add this to the php*.load file:

如果 php 文件呈现为纯文本,则需要将其添加到 php*.load 文件中:

AddType application/x-httpd-php .php

添加类型应用程序/x-httpd-php .php

OR to avoid having to edit the php load files everytime you install a new version, you can set this globally in your apache2 config file:

或者为了避免每次安装新版本时都必须编辑 php 加载文件,您可以在 apache2 配置文件中全局设置:

SetHandler application/x-httpd-php

SetHandler 应用程序/x-httpd-php

These instructions are intended for a development server.

这些说明适用于开发服务器。

My personal script for this:

我的个人脚本:

#!/bin/bash
i=1
c=1
options=()
while [ $c -gt 0 ]
do
    v=$(phpbrew list | sed -n "${i}p")
    if [ -z "$v" ]; then
        c=0
    elif [ -n "$v" ]; then
        options+=("$v")
    fi
    i=$[$i+1]
done

count=0
printf "\n"
echo 'Available versions:'
for i in "${options[@]}"
do
 echo "$i $[$count+1]"
 count=$[$count+1]
done

printf "\n"
echo 'Which version should be enabled?'
read selected

chosen="${options[$selected - 1]}"
chosen="$(echo -e "${chosen}" | tr -d '[:space:]')"
chosen="$(echo -e "${chosen}" | sed 's/\*//g')"
chosen="$(echo -e "${chosen}" | sed 's/php-//g')"
echo -e "php-${chosen} to be enabled."

source $HOME/.phpbrew/bashrc
phpbrew switch php-${chosen}

if [[ $chosen =~ ^5 ]]; then
    sudo a2dismod php7
    sudo a2enmod php5
    sudo service apache2 reload
elif [[ $chosen =~ ^7 ]]; then
    sudo a2dismod php5
    sudo a2enmod php7
    sudo service apache2 reload
else echo 'This script only works on php 5 and 7';
fi

The script runs phpbrew list on your behalf and let's you select the version using number keys. It then runs phpbrew switch and also switches the apache2 modules on or off and restarts the server.

该脚本代表您运行 phpbrew list,让您使用数字键选择版本。然后它运行 phpbrew switch 并打开或关闭 apache2 模块并重新启动服务器。