首页 > 后端开发 > php教程 > 如何在一台服务器上运行多个版本的PHP

如何在一台服务器上运行多个版本的PHP

William Shakespeare
发布: 2025-02-19 09:02:13
原创
428 人浏览过

如何在一台服务器上运行多个版本的PHP

>在这篇特定的文章中,我们将演示一个解决方案,以安装Phalcon和PHP的多个版本,并在单个Web服务器上运行它们。 PHP 5.5.x和5.6.x将在此处使用,但是您可以用其他版本替换它们。任何支持PHP-FPM的服务器都足够,但我们建议使用NGINX。本教程中使用的环境是Fedora OS - 一个Linux系统,但对于任何其他 *NIX OS。

钥匙要点

    >利用nginx和php-fpm在单个服务器上运行多个版本的PHP,如PHP 5.5.5.x和Fedora OS上的5.6.x所示。
  • 确保安装了所有必要的开发工具和库,例如GCC,Make和libtool,以成功地从源构建PHP。
  • >克隆来自GitHub的PHP源代码访问多个PHP版本,并使用`。/configure`脚本与特定选项(例如'–Enable-fpm`为php-fpm支持)配置它们。
  • 在单独的目录(/opt/php-5.5`,`/opt/php-5.6`)中安装和配置不同的PHP版本以隔离它们并避免冲突。
  • >
  • >将NGINX配置修改为基于服务器名称将流量引向不同的PHP版本,允许同时托管多个需要不同PHP环境的应用程序。
  • >初步注释
  • 本教程将涵盖PHP 5.5.x的安装,并使用Phalcon 1.3.x和Php 5.6.x,带有Phalcon 2.0.0。我们还将构建一些其他PHP扩展,例如APC,memcache,memcached和ioncube。
  • >安装nginx
Nginx是Fedora OS中的可用软件包,我们可以按以下方式安装:

然后,我们为Nginx创建系统启动链接并开始

建立php

从PHP开始之前,我们需要安装构建php5的先决条件:

GCC或其他一些编译器套件。

sudo yum install nginx
登录后复制
登录后复制
登录后复制
登录后复制
libc-dev,提供C标准库,包括标题。

make,这是PHP使用的构建管理工具。 用于生成配置脚本的AutoConf(2.59或更高)。

> automake(1.4或更高),生成sagefile.in文件。
sudo chkconfig nginx on
  sudo service nginx start
登录后复制
登录后复制
登录后复制
>

> libtool,帮助我们管理共享库。

>

野牛(2.4或更高),用于生成PHP解析器。

(可选)RE2C,用于生成PHP Lexer。由于GIT存储库已经包含生成的lexer,因此只有在您想对其进行更改时才需要RE2C。
    >
  • >在CentOS/Fedora上,您可以使用以下命令安装所有这些:>
  • 然后,我们需要获取其源代码。有两种方法:您可以从PHP的下载页面下载存档,也可以从GitHub中克隆Git存储库。
  • >我们建议您从git中查看源代码,因为它为您提供了一种简单的方法,可以使安装保持最新状态并使用不同版本尝试代码。如果要提交补丁或拉动请求,则还需要结帐。

    克隆存储库,在您的终端中运行以下命令:

    >

    sudo yum install nginx
    登录后复制
    登录后复制
    登录后复制
    登录后复制
    默认情况下,您将在主分支上,因此,如果要移至开发版本,则需要检查稳定的分支。例如。

    sudo chkconfig nginx on
      sudo service nginx start
    登录后复制
    登录后复制
    登录后复制
    >进入单个构建步骤之前,我们必须执行一些“默认” PHP构建的命令。这仅对于git的构建才是必要的。

    >

    sudo yum install gcc libxml2-devel libXpm-devel gmp-devel libicu-devel t1lib-devel aspell-devel openssl-devel bzip2-devel libcurl-devel libjpeg-devel libvpx-devel libpng-devel freetype-devel readline-devel libtidy-devel libxslt-devel libmcrypt-devel pcre-devel curl-devel mysql-devel ncurses-devel gettext-devel net-snmp-devel libevent-devel libtool-ltdl-devel libc-client-devel postgresql-devel bison gcc make
    登录后复制
    登录后复制
    ./ buildConf生成配置脚本。这可能需要几分钟。

    我认为,将整个Web服务器存储在单个目录中是最好的,因此我在此处使用 /选择。打开终端并键入以下命令。

    >通过上述步骤生成./configure脚本后,您可以使用它来自定义PHP构建。您可以使用–HELP列出所有受支持的选项:
    sudo mkdir /opt/source && cd /opt/source
      git clone git@github.com:php/php-src.git && cd php-src
    登录后复制
    登录后复制

    >上面的命令将列出各种通用选项,所有基于AutoConf的配置脚本都支持这些通用选项。其中一个已经提到 - prefix = dir,它更改了Make Install使用的安装目录。另一个有用的选项是-c,它将缓存config.cache文件中各种测试的结果,并加快后续的./configure调用。只有一旦您已经有了工作构建,并且想快速在不同的配置之间进行更改。
    <span>  PHP 5.3:  git checkout PHP-5.3
    </span><span>  PHP 5.4:  git checkout PHP-5.4
    </span><span>  PHP 5.6:  git checkout PHP-5.6
    </span><span>  PHP HEAD: git checkout master </span>
    登录后复制
    登录后复制
    这是一些有用的设置:

    完成准备后,我们安装了PHP版本5.6。运行以下内容:

    sudo ./buildconf
    登录后复制
    登录后复制
    >最后一个开关(–enable-fpm)使该PHP版本可与PHP-FPM一起使用。如果您想将此PHP-FPM版本与Apache一起使用,请使用-with-fpm-user = apache和-with-with-fpm group = apache。另一方面,如果您想与Nginx一起使用此PHP-FPM版本,请使用-with-fpm-user = nginx和-with-with-fpm group = nginx。

    应该在终端中打印成功的消息:

    >
    sudo mkdir -p /opt/php-5.6
      sudo mkdir -p /opt/php-5.5
    登录后复制
    登录后复制

    现在,您可以使用Make来执行实际的汇编:>

    此操作的主要结果将是启用SAPI的PHP二进制文件(默认情况下,SAPI/CLI/PHP和SAPI/CGI/PHP-CGI),以及模块/目录中的共享扩展。

    现在,您可以通过使用-prefix Configuration运行“ MAKE INSTAL”将PHP安装到 /usr /local(默认)或其他目录中。在这种情况下,是/OPT/PHP-5.6
    ./configure --help
    登录后复制
    登录后复制

    请注意,MAKE INSTARM不会创建INI文件。
    [...]
    
      Usage: ./configure [OPTION]... [VAR=VALUE]...
    
      To assign environment variables (e.g., CC, CFLAGS...), specify them as
      VAR=VALUE.  See below for descriptions of some of the useful variables.
    
      Defaults for the options are specified in brackets.
    
      Configuration:
        -h, --help              display this help and exit
            --help=short        display options specific to this package
            --help=recursive    display the short help of all the included packages
        -V, --version           display version information and exit
        -q, --quiet, --silent   do not print `checking ...' messages
            --cache-file=FILE   cache test results in FILE [disabled]
        -C, --config-cache      alias for `--cache-file=config.cache'
        -n, --no-create         do not create output files
            --srcdir=DIR        find the sources in DIR [configure dir or `..']
    
      Installation directories:
        --prefix=PREFIX         install architecture-independent files in PREFIX
                                [/usr/local]
        --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                                [PREFIX]
    
      By default, `make install' will install all the files in
      `/usr/local/bin', `/usr/local/lib' etc.  You can specify
      an installation prefix other than `/usr/local' using `--prefix',
      for instance `--prefix=$HOME'.
    
      For better control, use the options below.
    
      Fine tuning of the installation directories:
        --bindir=DIR            user executables [EPREFIX/bin]
        --sbindir=DIR           system admin executables [EPREFIX/sbin]
        --libexecdir=DIR        program executables [EPREFIX/libexec]
        --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
        --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
        --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
        --libdir=DIR            object code libraries [EPREFIX/lib]
        --includedir=DIR        C header files [PREFIX/include]
        --oldincludedir=DIR     C header files for non-gcc [/usr/include]
        --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
        --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
        --infodir=DIR           info documentation [DATAROOTDIR/info]
        --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
        --mandir=DIR            man documentation [DATAROOTDIR/man]
        --docdir=DIR            documentation root [DATAROOTDIR/doc/PACKAGE]
        --htmldir=DIR           html documentation [DOCDIR]
        --dvidir=DIR            dvi documentation [DOCDIR]
        --pdfdir=DIR            pdf documentation [DOCDIR]
        --psdir=DIR             ps documentation [DOCDIR]
     
      [...]
    登录后复制
    登录后复制
    >

    >复制php.ini和php-fpm.conf到正确的目录:>

    我们再一次验证并检查PHP版本。
    ./configure \
    --prefix=/opt/php-5.6 \
    --with-pdo-pgsql \
    --with-zlib-dir \
    --with-freetype-dir \
    --enable-mbstring \
    --with-libxml-dir=/usr \
    --enable-soap \
    --enable-calendar \
    --with-curl \
    --with-mcrypt \
    --with-zlib \
    --with-gd \
    --with-pgsql \
    --disable-rpath \
    --enable-inline-optimization \
    --with-bz2 \
    --with-zlib \
    --enable-sockets \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-pcntl \
    --enable-mbregex \
    --with-mhash \
    --enable-zip \
    --with-pcre-regex \
    --with-mysql \
    --with-pdo-mysql \
    --with-mysqli \
    --with-png-dir=/usr \
    --enable-gd-native-ttf \
    --with-openssl \
    --with-fpm-user=nginx \
    --with-fpm-group=nginx \
    --with-libdir=lib64 \
    --enable-ftp \
    --with-imap \
    --with-imap-ssl \
    --with-kerberos \
    --with-gettext \
    --with-gd \
    --with-jpeg-dir=/usr/lib/
    --enable-fpm
    登录后复制
    登录后复制
    >
    sudo yum install nginx
    登录后复制
    登录后复制
    登录后复制
    登录后复制

    > open/opt/php-5.6/etc/php-fpm.conf并调整收听行中的设置。您必须更改为未使用的端口(例如9001; Fedora可能正在使用9000端口9000)

    sudo chkconfig nginx on
      sudo service nginx start
    登录后复制
    登录后复制
    登录后复制
    sudo yum install gcc libxml2-devel libXpm-devel gmp-devel libicu-devel t1lib-devel aspell-devel openssl-devel bzip2-devel libcurl-devel libjpeg-devel libvpx-devel libpng-devel freetype-devel readline-devel libtidy-devel libxslt-devel libmcrypt-devel pcre-devel curl-devel mysql-devel ncurses-devel gettext-devel net-snmp-devel libevent-devel libtool-ltdl-devel libc-client-devel postgresql-devel bison gcc make
    登录后复制
    登录后复制

    > init脚本设置

    >您可能需要为新的PHP-FPM创建一个初始脚本。幸运的是,PHP 5.3已经为您提供了它,只需将INIT脚本复制到您的目录并更改权限:

    sudo mkdir /opt/source && cd /opt/source
      git clone git@github.com:php/php-src.git && cd php-src
    登录后复制
    登录后复制

    您的初始脚本已经准备就绪。现在,您可以启动,停止和重新加载php-fpm:>

    <span>  PHP 5.3:  git checkout PHP-5.3
    </span><span>  PHP 5.4:  git checkout PHP-5.4
    </span><span>  PHP 5.6:  git checkout PHP-5.6
    </span><span>  PHP HEAD: git checkout master </span>
    登录后复制
    登录后复制
    构建第二个PHP(5.5.x)

    我们打开终端并键入以下命令。

    >

    sudo ./buildconf
    登录后复制
    登录后复制
    建筑物php phalcon扩展

    >要安装包括phalcon 2.0在内的多个版本的phalcon,我们需要安装Zephir
    sudo mkdir -p /opt/php-5.6
      sudo mkdir -p /opt/php-5.5
    登录后复制
    登录后复制
    有很多方法可以安装PHP扩展。我们将使用phpize构建。

    PHPIZE扮演与用于PHP构建的./buildconf脚本相似的角色:首先,它将通过从$ prefix/lib/php/build复制文件来将PHP构建系统导入扩展名。其中包括acinclude.m4(php的M4宏),phpize.m4(将重命名为扩展程序中的配置。 然后,Phpize将调用AutoConf生成一个./configure文件,该文件可用于自定义扩展构建。例如安装备忘录,您必须添加 - 启用 - 磁性。

    >

    记住!在构建扩展时,您必须指定 - wish-php-config选项(除非您只有单个全局安装PHP)。否则。/configure将无法正确确定PHP版本和标志。此外,php-config脚本还确保“ make install”命令将将生成的 *.SO文件移至右扩展名目录。

    构建第一个php phalcon(2.0)

    请检查它是否成功

    安装扩展程序后。您仍然需要通过将其包括在php.ini文件中来激活它。

    ./configure --help
    登录后复制
    登录后复制

    构建第二个php phalcon(1.3.x)

    [...]
    
      Usage: ./configure [OPTION]... [VAR=VALUE]...
    
      To assign environment variables (e.g., CC, CFLAGS...), specify them as
      VAR=VALUE.  See below for descriptions of some of the useful variables.
    
      Defaults for the options are specified in brackets.
    
      Configuration:
        -h, --help              display this help and exit
            --help=short        display options specific to this package
            --help=recursive    display the short help of all the included packages
        -V, --version           display version information and exit
        -q, --quiet, --silent   do not print `checking ...' messages
            --cache-file=FILE   cache test results in FILE [disabled]
        -C, --config-cache      alias for `--cache-file=config.cache'
        -n, --no-create         do not create output files
            --srcdir=DIR        find the sources in DIR [configure dir or `..']
    
      Installation directories:
        --prefix=PREFIX         install architecture-independent files in PREFIX
                                [/usr/local]
        --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                                [PREFIX]
    
      By default, `make install' will install all the files in
      `/usr/local/bin', `/usr/local/lib' etc.  You can specify
      an installation prefix other than `/usr/local' using `--prefix',
      for instance `--prefix=$HOME'.
    
      For better control, use the options below.
    
      Fine tuning of the installation directories:
        --bindir=DIR            user executables [EPREFIX/bin]
        --sbindir=DIR           system admin executables [EPREFIX/sbin]
        --libexecdir=DIR        program executables [EPREFIX/libexec]
        --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
        --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
        --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
        --libdir=DIR            object code libraries [EPREFIX/lib]
        --includedir=DIR        C header files [PREFIX/include]
        --oldincludedir=DIR     C header files for non-gcc [/usr/include]
        --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
        --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
        --infodir=DIR           info documentation [DATAROOTDIR/info]
        --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
        --mandir=DIR            man documentation [DATAROOTDIR/man]
        --docdir=DIR            documentation root [DATAROOTDIR/doc/PACKAGE]
        --htmldir=DIR           html documentation [DOCDIR]
        --dvidir=DIR            dvi documentation [DOCDIR]
        --pdfdir=DIR            pdf documentation [DOCDIR]
        --psdir=DIR             ps documentation [DOCDIR]
     
      [...]
    登录后复制
    登录后复制

    我们再次检查以确保安装成功

    ./configure \
    --prefix=/opt/php-5.6 \
    --with-pdo-pgsql \
    --with-zlib-dir \
    --with-freetype-dir \
    --enable-mbstring \
    --with-libxml-dir=/usr \
    --enable-soap \
    --enable-calendar \
    --with-curl \
    --with-mcrypt \
    --with-zlib \
    --with-gd \
    --with-pgsql \
    --disable-rpath \
    --enable-inline-optimization \
    --with-bz2 \
    --with-zlib \
    --enable-sockets \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-pcntl \
    --enable-mbregex \
    --with-mhash \
    --enable-zip \
    --with-pcre-regex \
    --with-mysql \
    --with-pdo-mysql \
    --with-mysqli \
    --with-png-dir=/usr \
    --enable-gd-native-ttf \
    --with-openssl \
    --with-fpm-user=nginx \
    --with-fpm-group=nginx \
    --with-libdir=lib64 \
    --enable-ftp \
    --with-imap \
    --with-imap-ssl \
    --with-kerberos \
    --with-gettext \
    --with-gd \
    --with-jpeg-dir=/usr/lib/
    --enable-fpm
    登录后复制
    登录后复制

    配置nginx

    [...]
    
    creating libtool
    appending configuration tag "CXX" to libtool
    
    Generating files
    configure: creating ./config.status
    creating main/internal_functions.c
    creating main/internal_functions_cli.c
    +--------------------------------------------------------------------+
    | License:                                                           |
    | This software is subject to the PHP License, available in this     |
    | distribution in the file LICENSE.  By continuing this installation |
    | process, you are bound by the terms of this license agreement.     |
    | If you do not agree with the terms of this license, you must abort |
    | the installation process at this point.                            |
    +--------------------------------------------------------------------+
    
    Thank you for using PHP.
    
    config.status: creating php5.spec
    config.status: creating main/build-defs.h
    config.status: creating scripts/phpize
    config.status: creating scripts/man1/phpize.1
    config.status: creating scripts/php-config
    config.status: creating scripts/man1/php-config.1
    config.status: creating sapi/cli/php.1
    config.status: creating sapi/fpm/php-fpm.conf
    config.status: creating sapi/fpm/init.d.php-fpm
    config.status: creating sapi/fpm/php-fpm.service
    config.status: creating sapi/fpm/php-fpm.8
    config.status: creating sapi/fpm/status.html
    config.status: creating sapi/cgi/php-cgi.1
    config.status: creating ext/phar/phar.1
    config.status: creating ext/phar/phar.phar.1
    config.status: creating main/php_config.h
    config.status: executing default commands
    登录后复制

    以下配置将创建两个服务器:phalcon-prd.localhost在php 5.5.5.x和phalcon-dev.localhost上运行,可与PHP 5.6.x一起使用。这是一个示例,您可以将其自定义为所需的任何内容,请参阅Nginx文档

    make
    登录后复制

    设置本地主机文件

    sudo make install
    登录后复制
    如果您使用的是Linux系统,则可以编辑主机文件:>

    新主机文件看起来像。
    /opt/php-5.6/bin/php --ini
    Configuration File (php.ini) Path: /opt/php-5.6/lib
    Loaded Configuration File:         (none)
    Scan for additional .ini files in: (none)
    Additional .ini files parsed:      (none)
    登录后复制

    这个拦截了phalcon-dev.localhost和phalcon-prd.localhost的所有请求。

    测试

    >用于测试,我们创建了一个名为test.php的新文件,并将其放入与上述NGINX配置相对应的文件夹中。在每个文件中,我们添加以下命令。

    sudo yum install nginx
    登录后复制
    登录后复制
    登录后复制
    登录后复制

    现在,在每个服务器中运行test.php文件,我们在http://phancon-dev.localhost http://phancon-prd.localhost/test.php上看到一个phalcon 1.3.x phpinfo() /test.php应该有Phalcon 2.0.x.

    的phpinfo()

    如何在一台服务器上运行多个版本的PHP

    总结

    在本教程中,我们了解了如何在服务器上轻松地在服务器上轻松运行多个主动运行版本的PHP版本,每个版本在必要时都有自己的一组不同的扩展名。如果您进行共享托管,或者您需要支持古老的旧应用程序,同时可以在尖端版本上开发和部署。

    >在下面的评论中留下您的反馈,一如既往,如果您喜欢的话,请分享本文!

    >经常询问有关在一台服务器上运行多个PHP版本的问题

    >如何在Ubuntu 18.04上使用Apache和PHP-FPM在一台服务器上运行多个PHP版本?安装所需的PHP版本和Apache。然后,您需要配置Apache来为每个站点使用PHP-FPM版本。您可以通过编辑每个站点的Apache配置文件并设置ProxyPassMatch指令以使用正确的PHP-FPM池来执行此操作。之后,您需要为每个PHP版本创建一个PHP-FPM池。最后,重新启动apache和php-fpm以应用更改。

    >如何在服务器上的不同php版本之间切换?

    >您可以使用A2ENMOD在服务器上的不同PHP版本之间切换和a2dismod命令。 A2ENMOD命令启用一个模块,A2Dismod命令禁用模块。要切换到其他PHP版本,您需要禁用当前的PHP版本并启用所需的PHP版本。之后,您需要重新启动apache应用更改。

    >如何运行具有不同版本的PHP的不同网站?

    以运行具有不同版本的PHP的不同网站,您需要配置每个网站以使用不同的PHP版本。您可以通过编辑每个网站的Apache配置文件来做到这一点,并设置ProxyPassMatch指令以使用正确的PHP-FPM池。之后,您需要为每个PHP版本创建一个PHP-FPM池。最后,重新启动apache和php-fpm以应用更改。

    >

    >如何使用Apache和PHP-FPM和PHP-FPM在Fedora 35上安装多个PHP版本? apache。然后,您需要配置Apache来为每个站点使用PHP-FPM版本。您可以通过编辑每个站点的Apache配置文件并设置ProxyPassMatch指令以使用正确的PHP-FPM池来执行此操作。之后,您需要为每个PHP版本创建一个PHP-FPM池。最后,重新启动Apache和php-fpm以应用更改。

    >我如何使用PHPENV管理多个PHP版本?

    您可以使用PHPENV通过安装PHPENV和PHP版本来管理多个PHP版本。必需的PHP版本。然后,您可以使用PHPENV命令在不同的PHP版本之间切换。您还可以使用PHPENV Global命令设置全局PHP版本,而PHPENV LOCAL命令为特定目录设置本地PHP版本。

    >如何检查服务器上的当前PHP版本?您可以通过在终端中运行PHP -V命令来检查服务器上的当前PHP版本。此命令将显示当前的PHP版本和有关PHP安装的其他信息。

    >

    >如何在服务器上安装特定的PHP版本?

    >您可以在服务器上安装特定的PHP版本通过使用apt-get安装命令,然后使用所需的PHP版本的软件包名称。例如,要安装PHP 7.2,您将运行命令apt-get install php7.2。通过编辑Apache配置文件并设置ProxyPassMatch指令以使用正确的PHP-FPM池来使用特定的PHP版本。之后,您需要重新启动apache应用更改。

    >如何为特定的PHP版本创建PHP-fpm池?特定的PHP版本是通过在/etc/php/7.2/fpm/pool.d/ directory中创建新的池配置文件。配置文件应包含PHP-FPM池的设置,例如收听地址和端口,用户和组以及Process Manager设置。

    >我如何在运行多个PHP版本上的问题上解决问题。一台服务器?

    您可以通过选中APACHE和PHP-FPM错误日志在一台服务器上运行多个PHP版本的问题进行故障排除。错误日志可以提供有关Apache配置或PHP-FPM池的任何问题的信息。您还可以使用php -v命令检查当前的PHP版本和A2enMod命令,以检查启用了哪些PHP模块。

    >

以上是如何在一台服务器上运行多个版本的PHP的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板