如何在开发 LAMP 服务器上运行多个版本的 PHP 5.x?

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

How can one run multiple versions of PHP 5.x on a development LAMP server?

phplamp

提问by James Mishra

I need to test my PHP applications with multiple versions of PHP 5.x, such as PHP 5.0.0 and PHP 5.2.8.

我需要使用多个版本的 PHP 5.x(例如 PHP 5.0.0 和 PHP 5.2.8)来测试我的 PHP 应用程序。

Is there a way that I can configure a development LAMP server so I can quickly test applications with multiple versions of PHP5?

有没有一种方法可以配置开发 LAMP 服务器,以便我可以使用多个版本的 PHP5 快速测试应用程序?

采纳答案by Dana the Sane

One way to do this is to have your main version of php set up with mod_php and run all of the others through fast cgi on different ports (i.e. 81, 82, 83 etc). This won't guarantee totally consistent behavior though.

一种方法是使用 mod_php 设置您的主要 php 版本,并通过不同端口(即 81、82、83 等)上的快速 cgi 运行所有其他版本。但这并不能保证完全一致的行为。

回答by transilvlad

With CentOS, you can do it using a combination of fastcgi for one version of PHP, and php-fpm for the other, as described here:

使用 CentOS,您可以将 fastcgi 用于一个版本的 PHP,将 php-fpm 用于另一个版本,如下所述:

https://web.archive.org/web/20130707085630/http://linuxplayer.org/2011/05/intall-multiple-version-of-php-on-one-server

https://web.archive.org/web/20130707085630/http://linuxplayer.org/2011/05/intall-multiple-version-of-php-on-one-server

Based on CentOS 5.6, for Apache only

基于 CentOS 5.6,仅适用于 Apache

1. Enable rpmforge and epel yum repository

1.启用rpmforge和epel yum存储库

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
wget http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
sudo rpm -ivh rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
sudo rpm -ivh epel-release-5-4.noarch.rpm

2. Install php-5.1

2.安装php-5.1

CentOS/RHEL 5.x series have php-5.1 in box, simply install it with yum, eg:

CentOS/RHEL 5.x 系列自带 php-5.1,直接用 yum 安装即可,例如:

sudo yum install php php-mysql php-mbstring php-mcrypt

3. Compile and install php 5.2 and 5.3 from source

3.从源码编译安装php 5.2和5.3

For php 5.2 and 5.3, we can find many rpm packages on the Internet. However, they all conflict with the php which comes with CentOS, so, we'd better build and install them from soure, this is not difficult, the point is to install php at different location.

对于php 5.2和5.3,我们可以在网上找到很多rpm包。但是都和CentOS自带的php有冲突,所以最好从soure编译安装,这个不难,重点是php安装在不同的位置。

However, when install php as an apache module, we can only use one version of php at the same time. If we need to run different version of php on the same server, at the same time, for example, different virtual host may need different version of php. Fortunately, the cool FastCGI and PHP-FPM can help.

但是,当将php安装为apache模块时,我们只能同时使用一个版本的php。如果我们需要在同一台服务器上同时运行不同版本的php,比如不同的虚拟主机可能需要不同版本的php。幸运的是,很酷的 FastCGI 和 PHP-FPM 可以提供帮助。

Build and install php-5.2 with fastcgi enabled

在启用 fastcgi 的情况下构建和安装 php-5.2

1) Install required dev packages

1)安装所需的开发包

yum install gcc libxml2-devel bzip2-devel zlib-devel \
    curl-devel libmcrypt-devel libjpeg-devel \
    libpng-devel gd-devel mysql-devel

2) Compile and install

2)编译安装

wget http://cn.php.net/get/php-5.2.17.tar.bz2/from/this/mirror
tar -xjf php-5.2.17.tar.bz2
cd php-5.2.17
./configure --prefix=/usr/local/php52 \
    --with-config-file-path=/etc/php52 \
    --with-config-file-scan-dir=/etc/php52/php.d \
    --with-libdir=lib64 \
    --with-mysql \
    --with-mysqli \
    --enable-fastcgi \
    --enable-force-cgi-redirect \
    --enable-mbstring \
    --disable-debug \
    --disable-rpath \
    --with-bz2 \
    --with-curl \
    --with-gettext \
    --with-iconv \
    --with-openssl \
    --with-gd \
    --with-mcrypt \
    --with-pcre-regex \
    --with-zlib
make -j4 > /dev/null
sudo make install
sudo mkdir /etc/php52
sudo cp php.ini-recommended /etc/php52/php.ini

3) create a fastcgi wrapper script

3)创建一个fastcgi包装脚本

create file /usr/local/php52/bin/fcgiwrapper.sh

创建文件 /usr/local/php52/bin/fcgiwrapper.sh

#!/bin/bash
PHP_FCGI_MAX_REQUESTS=10000
export PHP_FCGI_MAX_REQUESTS
exec /usr/local/php52/bin/php-cgi
chmod a+x /usr/local/php52/bin/fcgiwrapper.sh
Build and install php-5.3 with fpm enabled

wget http://cn.php.net/get/php-5.3.6.tar.bz2/from/this/mirror
tar -xjf php-5.3.6.tar.bz2 
cd php-5.3.6
./configure --prefix=/usr/local/php53 \
    --with-config-file-path=/etc/php53 \
    --with-config-file-scan-dir=/etc/php53/php.d \
    --enable-fpm \
    --with-fpm-user=apache \
    --with-fpm-group=apache \
    --with-libdir=lib64 \
    --with-mysql \
    --with-mysqli \
    --enable-mbstring \
    --disable-debug \
    --disable-rpath \
    --with-bz2 \
    --with-curl \
    --with-gettext \
    --with-iconv \
    --with-openssl \
    --with-gd \
    --with-mcrypt \
    --with-pcre-regex \
    --with-zlib 

make -j4 && sudo make install
sudo mkdir /etc/php53
sudo cp php.ini-production /etc/php53/php.ini

sed -i -e 's#php_fpm_CONF=${prefix}/etc/php-fpm.conf#php_fpm_CONF=/etc/php53/php-fpm.conf#' \
    sapi/fpm/init.d.php-fpm
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod a+x /etc/init.d/php-fpm
sudo /sbin/chkconfig --add php-fpm
sudo /sbin/chkconfig php-fpm on

sudo cp sapi/fpm/php-fpm.conf /etc/php53/

Configue php-fpm

配置 php-fpm

Edit /etc/php53/php-fpm.conf, change some settings. This step is mainly to uncomment some settings, you can adjust the value if you like.

编辑/etc/php53/php-fpm.conf,更改一些设置。这一步主要是取消一些设置的注释,你可以根据需要调整值。

pid = run/php-fpm.pid
listen = 127.0.0.1:9000
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20

Then, start fpm

然后,启动 fpm

sudo /etc/init.d/php-fpm start

Install and setup mod_fastcgi, mod_fcgid

安装和设置 mod_fastcgi、mod_fcgid

sudo yum install libtool httpd-devel apr-devel
wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz
tar -xzf mod_fastcgi-current.tar.gz
cd mod_fastcgi-2.4.6
cp Makefile.AP2 Makefile
sudo make top_dir=/usr/lib64/httpd/ install
sudo sh -c "echo 'LoadModule fastcgi_module modules/mod_fastcgi.so' > /etc/httpd/conf.d/mod_fastcgi.conf"
yum install mod_fcgid

Setup and test virtual hosts

设置和测试虚拟主机

1) Add the following line to /etc/hosts

1) 将以下行添加到 /etc/hosts

127.0.0.1 web1.example.com web2.example.com web3.example.com

2) Create web document root and drop an index.php under it to show phpinfo switch to user root, run

2)创建web文档root,在其下放一个index.php,显示phpinfo切换到root用户,运行

mkdir /var/www/fcgi-bin
for i in {1..3}; do
    web_root=/var/www/web$i
    mkdir $web_root
    echo "<?php phpinfo(); ?>" > $web_root/index.php
done

Note: The empty /var/www/fcgi-bin directory is required, DO NOT REMOVE IT LATER

注意:空的 /var/www/fcgi-bin 目录是必需的,以后不要删除

3) Create Apache config file(append to httpd.conf)

3)创建Apache配置文件(附加到httpd.conf)

NameVirtualHost *:80

# module settings
# mod_fcgid
<IfModule mod_fcgid.c>
        idletimeout 3600
        processlifetime 7200
        maxprocesscount 17
        maxrequestsperprocess 16
        ipcconnecttimeout 60 
        ipccommtimeout 90
</IfModule>
# mod_fastcgi with php-fpm
<IfModule mod_fastcgi.c>
        FastCgiExternalServer /var/www/fcgi-bin/php-fpm -host 127.0.0.1:9000
</IfModule>


# virtual hosts...

#################################################################
#1st virtual host, use mod_php, run php-5.1
#################################################################
<VirtualHost *:80>
        ServerName web1.example.com
        DocumentRoot "/var/www/web1"

        <ifmodule mod_php5.c>
                <FilesMatch \.php$>
                        AddHandler php5-script .php
                </FilesMatch>
        </IfModule>

        <Directory "/var/www/web1">
                DirectoryIndex index.php index.html index.htm
                Options -Indexes FollowSymLinks
                Order allow,deny
                Allow from all
        </Directory>

</VirtualHost>
#################################################################
#2nd virtual host, use mod_fcgid, run php-5.2
#################################################################
<VirtualHost *:80>
        ServerName web2.example.com
        DocumentRoot "/var/www/web2"

        <IfModule mod_fcgid.c>
                AddHandler fcgid-script .php
                FCGIWrapper /usr/local/php52/bin/fcgiwrapper.sh
        </IfModule>

        <Directory "/var/www/web2">
                DirectoryIndex index.php index.html index.htm
                Options -Indexes FollowSymLinks +ExecCGI
                Order allow,deny
                Allow from all
        </Directory>

</VirtualHost>
#################################################################
#3rd virtual host, use mod_fastcgi + php-fpm, run php-5.3
#################################################################
<VirtualHost *:80>
        ServerName web3.example.com
        DocumentRoot "/var/www/web3"


        <IfModule mod_fastcgi.c>
                ScriptAlias /fcgi-bin/ /var/www/fcgi-bin/
                AddHandler php5-fastcgi .php
                Action php5-fastcgi /fcgi-bin/php-fpm
        </IfModule>

        <Directory "/var/www/web3">
                DirectoryIndex index.php index.html index.htm
                Options -Indexes FollowSymLinks +ExecCGI
                Order allow,deny
                Allow from all
        </Directory>

</VirtualHost>

4) restart apache. visit the 3 sites respectly to view phpinfo and validate the result. ie:

4)重启apache。分别访问3个站点查看phpinfo并验证结果。IE:

http://web1.example.com
http://web2.example.com
http://web3.example.com

If all OK, you can use one of the 3 virtual host as template to create new virtual host, with the desired php version.

如果一切正常,您可以使用 3 个虚拟主机之一作为模板创建新的虚拟主机,具有所需的 php 版本。

回答by Leprechaun

Having multiple instances of apache + php never really tickled my fancy, but it probably the easiest way to do it. If you don't feel like KISS ... here's an idea.

拥有多个 apache + php 实例从来没有真正引起我的兴趣,但这可能是最简单的方法。如果你不喜欢 KISS ......这里有一个想法。

Get your apache up and running, and try do configure it like debian and ubuntu do it, eg, have directories for loaded modules. Your apache conf can use lines like this:

启动并运行您的 apache,并尝试像 debian 和 ubuntu 那样配置它,例如,为加载的模块提供目录。你的 apache conf 可以使用这样的行:

Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf

Then build your first version of php, and give it a prefix that has the version number explicitly contained, eg, /usr/local/php/5.2.8, /usr/local/php/5.2.6 ...

然后构建你的第一个 php 版本,并给它一个明确包含版本号的前缀,例如 /usr/local/php/5.2.8, /usr/local/php/5.2.6 ...

The conf/load would look something like this:

conf/load 看起来像这样:

php5.2.6.load

php5.2.6.load

LoadModule php5_module /usr/local/php/5.2.6/libphp5.so

php5.2.8.load

php5.2.8.load

LoadModule php5_module /usr/local/php/5.2.8/libphp5.so

To switch versions, all you have to do is change the load and conf files from the directory apache does the include on for the ones for another version. You can automate that with a simple bash script (delete the actual file, copy the alternate versions file in place, and restart apache.

要切换版本,您所要做的就是更改 apache 目录中的 load 和 conf 文件,为另一个版本的文件包含在内。您可以使用简单的 bash 脚本自动执行此操作(删除实际文件,将备用版本文件复制到位,然后重新启动 apache。

One advantage of this setup is the everything is consitent, so long you keep the php.ini's the same in terms of options and modules (which you would have to do with CGI anyway). They're all going through SAPI. Your applications won't need any changes whatsoever, nor need to use relative URLs.

这种设置的一个优点是一切都是一致的,只要您在选项和模块方面保持 php.ini 相同(无论如何您都必须使用 CGI)。他们都在通过 SAPI。您的应用程序不需要任何更改,也不需要使用相对 URL。

I thinkthis should work, but then again, i haven't tried it, nor am i likely to do so as i don't have the same requirements as you. Do comment if you ever do try though.

认为这应该可行,但话说回来,我还没有尝试过,也不太可能这样做,因为我的要求与您不同。如果您尝试过,请发表评论。

回答by wmac

Note: The following method will work on windows.

注意:以下方法适用于 Windows。

An alternative method (if it is ok to run a single version of PHP at a time) is to define multiple Apache services, each of which will use a different PHP version.

另一种方法(如果可以一次运行单个版本的 PHP)是定义多个 Apache 服务,每个服务将使用不同的 PHP 版本。

First of all use conditions in the Apache configuration file:

首先在Apache配置文件中使用条件:

 <ifdefine php54>
    SetEnv PHPRC C:/apache/php54/
    ScriptAlias /php/ "C:/apache/php54/"
    AddType application/x-httpd-php .php
    Action application/x-httpd-php "/php/php-cgi.exe"
</ifdefine>

<ifdefine php55>
    SetEnv PHPRC C:/apache/php55/
    ScriptAlias /php/ "C:/apache/php55/"
    AddType application/x-httpd-php .php
    Action application/x-httpd-php "/php/php-cgi.exe"
</ifdefine>

Now using the httpd.exe create two separate services from command line (elevated to administrator):

现在使用 httpd.exe 从命令行创建两个单独的服务(提升为管理员):

httpd.exe -k install -n Apache224_php54 -D php54

httpd.exe -k install -n Apache224_php55 -D php55

Now you can start one of the above services at a time (should shutdown one before starting the other).

现在您可以一次启动上述服务之一(应该在启动另一个之前关闭一个)。

If you have previously installed Apache as service you can remove that using below command (replace the service name with the one you have used):

如果您之前已将 Apache 作为服务安装,则可以使用以下命令将其删除(将服务名称替换为您使用过的名称):

apache -k uninstall -n Apache224

One further note is that I personally use a "notification area icon program" called "Seobiseu" to start and stop services as needed. I have added the two above services to it.

还有一点要注意的是,我个人使用一个名为“Seobiseu”的“通知区域图标程序”来根据需要启动和停止服务。我已经添加了上述两项服务。

回答by Jason

Understanding that you're probably talking about a local/desktop machine and would probably like to continuetalking about a local/desktop machine, I'll throw an alternative out there for you just in case it might help you or someone else:

了解您可能在谈论本地/台式机并且可能想继续谈论本地/台式机,我会为您提供替代方案,以防万一它可能对您或其他人有所帮助:

Set up multiple virtual server instances in the cloud, and share your code between them as a git repository (or mercurial, I suppose, though I have no personal experience all you really need is something decentralized). This has the benefit of giving you as close to a production experience as possible, and if you have experience setting up servers then it's not that complicated (or expensive, if you just want to spin a server up, do what you need to do, then spin it down again, then you're talking about a few cents up to say 50 cents, up to a few bucks if you just leave it running).

在云中设置多个虚拟服务器实例,并在它们之间共享您的代码作为 git 存储库(或 mercurial,我想,虽然我没有个人经验,但您真正需要的是去中心化的东西)。这样做的好处是可以为您提供尽可能接近生产的体验,如果您有设置服务器的经验,那么它并不那么复杂(或昂贵,如果您只想启动服务器,请做您需要做的事情,然后再次旋转它,然后你在谈论几美分到 50 美分,如果你让它继续运行,最多几美元)。

I do all of my project development in the cloud these days and I've found it much simpler to manage the infrastructure than I ever did when using local/non-virtualized installs, and it makes this sort of side-by-side scenario fairly straight forward. I just wanted to throw the idea out there if you hadn't considered it.

这些天我在云中进行我所有的项目开发,我发现管理基础设施比使用本地/非虚拟化安装时更简单,并且它使这种并排场景变得相当直接向前。如果你没有考虑过,我只是想把这个想法扔掉。

回答by Tisch

I have just successfully downgraded from PHP5.3 on Ubuntu 10.

我刚刚在 Ubuntu 10 上成功从 PHP5.3 降级。

To do this I used the following script:

为此,我使用了以下脚本:

#! /bin/sh
php_packages=`dpkg -l | grep php | awk '{print }'`

sudo apt-get remove $php_packages

sed s/lucid/karmic/g /etc/apt/sources.list | sudo tee /etc/apt/sources.list.d/karmic.list

sudo mkdir -p /etc/apt/preferences.d/

for package in $php_packages;
do echo "Package: $package
Pin: release a=karmic
Pin-Priority: 991
" | sudo tee -a /etc/apt/preferences.d/php
done

sudo apt-get update

sudo apt-get install $php_packages

For anyone that doesn't know how to run scripts from the command line, here is a brief tutorial:

对于不知道如何从命令行运行脚本的人,这里有一个简短的教程:

1. cd ~/
2. mkdir bin
3. sudo nano ~/bin/myscriptname.sh
4. paste in the script code I have posted above this
5. ctrl+x (this exits and prompts for you to save)
6. chmod u+x myscriptname.sh

These 6 steps create a script in a folder called "bin" in your home directory. You can then run this script by calling the following command:

这 6 个步骤在您的主目录中名为“bin”的文件夹中创建一个脚本。然后,您可以通过调用以下命令来运行此脚本:

~/bin/myscriptname.sh

Oulia!

欧利亚!

Hope this helps some of you!

希望这对你们中的一些人有所帮助!

For reference, here is where I got the script: PHP5.2.10 for Ubuntu 10

作为参考,这里是我得到脚本的地方: PHP5.2.10 for Ubuntu 10

There are several people on there all confirming that this works, and it worked a treat for me.

那里有几个人都确认这有效,对我来说是一种享受。

回答by Figidon

Rasmus Lerdorf, who created PHP, is maintaining an active Vagrant solution that seems to solve your needs. It allows for quickly switching between PHP versions, currently supporting more than 20 different versions. It comes out of the box with an nginx server, but can easily be switched to apache2 with a preconfigured setting. It also supports MySQL out of the box.

创建 PHP 的 Rasmus Lerdorf 正在维护一个活跃的 Vagrant 解决方案,它似乎可以解决您的需求。它允许在 PHP 版本之间快速切换,目前支持 20 多个不同的版本。它带有一个 nginx 服务器,开箱即用,但可以通过预先配置的设置轻松切换到 apache2。它还支持开箱即用的 MySQL。

This way you will have access to all versions of PHP, deployable on two of the main web servers, in a nice vagrant box, maintained by the big man behind PHP.

通过这种方式,您将可以访问所有版本的 PHP,可部署在两个主要的 Web 服务器上,在一个漂亮的流浪盒子中,由 PHP 背后的大人物维护。

For more information I would like to refer to the talk given by mr. Lerdorf at https://youtu.be/6XnysJAyThs?t=2864

有关更多信息,我想参考先生的演讲。勒多夫https://youtu.be/6XnysJAyThs?t=2864

The github repository containing the Vagrant solution is found at https://github.com/rlerdorf/php7dev

包含 Vagrant 解决方案的 github 存储库位于https://github.com/rlerdorf/php7dev

回答by Danial

For testing I just run multiple instances of httpd on different IP addresses, so I have php7 running on 192.168.0.70 and php5.6 running on 192.168.0.56. In production I have a site running an old oscommerce running php5.3 and I just have a different conf file for the site

为了测试,我只是在不同的 IP 地址上运行多个 httpd 实例,所以我在 192.168.0.70 上运行 php7,在 192.168.0.56 上运行 php5.6。在生产中,我有一个运行 php5.3 的旧 oscommerce 的站点,我只有一个不同的站点配置文件

httpd -f /etc/apache2/php70.conf
httpd -f /etc/apache2/php53.conf

It's also a clean way to have different php.inifiles for different sites. If you just have a couple of sites if a nice way to keep things organized and you don't have to worry about more then 1 site at a time when you upgrade something

这也是php.ini为不同站点使用不同文件的一种干净方式。如果您只有几个站点,这是一种使事情井井有条的好方法,而且您在升级某些内容时不必一次担心超过 1 个站点

回答by Growling Flea

I have several projects running on my box. If you have already installed more than one version, this bash script should help you easily switch. At the moment I have php5, php5.6, and php7.0 which I often swtich back and forth depending on the project I am working on. Here is my code.

我有几个项目在我的盒子上运行。如果您已经安装了多个版本,这个 bash 脚本应该可以帮助您轻松切换。目前我有 php5、php5.6 和 php7.0,我经常根据我正在处理的项目来回切换。这是我的代码。

Feel free to copy. Make sure you understand how the code works. This is for the webhostin. my local box my mods are stored at /etc/apache2/mods-enabled/

随意复制。确保您了解代码的工作原理。这是针对虚拟主机的。我的本地盒子我的模组存储在 /etc/apache2/mods-enabled/

    #!/bin/bash
# This file is for switching php versions.  
# To run this file you must use bash, not sh
# 
    # OS: Ubuntu 14.04 but should work on any linux
# Example: bash phpswitch.sh 7.0
# Written by Daniel Pflieger
# growlingflea at g mail dot com

NEWVERSION=  #this is the git directory target

#get the active php enabled mod by getting the array of files and store
#it to a variable
VAR=$(ls /etc/apache2/mods-enabled/php*)

#parse the returned variables and get the version of php that is active.
IFS=' ' read -r -a array <<< "$VAR"
array[0]=${array[0]#*php}
array[0]=${array[0]%.conf}


#confirm that the newversion veriable isn't empty.. if it is tell user 
#current version and exit
if [ "$NEWVERSION" = "" ]; then
echo current version is ${array[0]}.  To change version please use argument
exit 1
fi 

OLDVERSION=${array[0]}
#confirm to the user this is what they want to do
echo "Update php"  ${OLDVERSION} to ${NEWVERSION}


#give the user the opportunity to use CTRL-C to exit ot just hit return
read x

#call a2dismod function: this deactivate the current php version
sudo a2dismod php${OLDVERSION}

#call the a2enmod version.  This enables the new mode
sudo a2enmod php${NEWVERSION} 

echo "Restart service??"
read x

#restart apache
sudo service apache2 restart