如何在CentOS 7上使用Apache和PHP-FPM在一台服务器上运行多个PHP版本

本教程详细介绍了如何在CentOS 7服务器上利用Apache和PHP-FPM同时运行PHP 7.0和7.2版本,以支持多个使用不同PHP版本的网站。通过安装SCL仓库、配置PHP-FPM监听不同端口、调整SELinux设置以及创建虚拟主机,可以实现这一目标。此外,文章强调了这样做的优势,如资源效率和对不同应用需求的支持。
摘要由CSDN通过智能技术生成

The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program.

作者选择了COVID-19救济基金来接受捐赠,这是Write for DOnations计划的一部分。

介绍 (Introduction)

The Apache web server uses virtual hosts to manage multiple domains on a single instance. Similarly, PHP-FPM uses a daemon to manage multiple PHP versions on a single instance. Together, you can use Apache and PHP-FPM to host multiple PHP web-applications, each using a different version of PHP, all on the same server, and all at the same time. This is useful because different applications may require different versions of PHP, but some server stacks, like a regularly configured LAMP stack, can only manage one. Combining Apache with PHP-FPM is also a more cost-efficient solution than hosting each application on its own instance.

Apache Web服务器使用虚拟主机来管理单个实例上的多个域。 同样, PHP-FPM使用守护程序在单个实例上管理多个PHP版本。 您可以一起使用Apache和PHP-FPM托管多个PHP Web应用程序,每个Web应用程序都使用不同版本PHP,并且全部都在同一服务器上,并且同时全部。 这很有用,因为不同的应用程序可能需要不同版本PHP,但是某些服务器堆栈( 如定期配置的LAMP堆栈 )只能管理一个。 与将每个应用程序托管在自己的实例上相比,将Apache与PHP-FPM结合使用也是一种更具成本效益的解决方案。

PHP-FPM also offers configuration options for stderr and stdout logging, emergency restarts, and adaptive process spawning, which is useful for heavy-loaded sites. In fact, using Apache with PHP-FPM is one of the best stacks for hosting PHP applications, especially when it comes to performance.

PHP-FPM还提供了用于stderrstdout日志记录,紧急重启和自适应进程生成的配置选项,这对于负载较重的站点非常有用。 实际上,将Apache与PHP-FPM结合使用是托管PHP应用程序的最佳堆栈之一,尤其是在性能方面。

In this tutorial, you will set up two PHP sites on a single instance. Each site will use its own domain, and each domain will deploy its own version of PHP. The first, site1.your_domain, will deploy PHP 7.0. The second, site2.your_domain, will deploy PHP 7.2.

在本教程中,您将在单个实例上设置两个PHP网站。 每个站点都将使用其自己的域,并且每个域都将部署其自己PHP版本。 第一个site1.your_domain将部署PHP 7.0。 第二个site2.your_domain将部署PHP 7.2。

先决条件 (Prerequisites)

第1步-使用PHP-FPM安装PHP 7.0和7.2版 (Step 1 — Installing PHP Versions 7.0 and 7.2 with PHP-FPM)

With the prerequisites completed, you will now install PHP versions 7.0 and 7.2. The SCL (Software Collections) repository maintains numerous versions of the PHP stack for the CentOS 7 system. If you require the absolute newest version of PHP and it is not available on SCL, check the remi PPA (personal package archive) instead.

完成前提条件后,您现在将安装PHP版本7.0和7.2。 SCL(软件集合)存储库为CentOS 7系统维护了许多版本PHP堆栈。 如果您需要绝对最新PHP版本,而SCL上没有此版本, 改用remi PPA(个人软件包存档)

Begin by installing the SCL repository to your system:

首先将SCL存储库安装到系统中:

  • sudo yum install centos-release-scl -y

    须藤yum安装centos-release-scl -y

First let’s discover what versions of PHP 7 are available on SCL:

首先,让我们了解一下SCL上可用PHP 7版本:

  • sudo yum list rh-php7[0-9].x86_64

    须藤百胜清单rh-php7 [0-9] .x86_64

You’ll see an output like this:

您将看到类似以下的输出:


   
   
Output
Available Packages rh-php70.x86_64 2.3-1.el7 centos-sclo-rh rh-php71.x86_64 1-1.el7 centos-sclo-rh rh-php72.x86_64 1-2.el7 centos-sclo-rh rh-php73.x86_64 1-1.el7 centos-sclo-rh

You will note that the newest version, PHP 7.3, is also available. For our examples, however, we will install versions 7.0 and 7.2.

您会注意到,也提供了最新版本PHP 7.3。 但是,对于我们的示例,我们将安装版本7.0和7.2。

Lets begin with the older version. Install rh-php70 and rh-php70-php-fpm:

让我们从旧版本开始。 安装rh-php70rh-php70-php-fpm

  • sudo yum install rh-php70 rh-php70-php-fpm -y

    须藤百胜安装rh-php70 rh-php70-php-fpm -y
  • rh-php70 is a metapackage that runs PHP applications.

    rh-php70是运行PHP应用程序的元rh-php70

  • rh-php70-php-fpm provides the Fast Process Manager interpreter that runs as a daemon and receives Fast/CGI requests.

    rh-php70-php-fpm提供了作为守护程序运行的Fast Process Manager解释器,并接收Fast / CGI请求。

Now repeat the process for PHP version 7.2. Install rh-php72 and rh-php72-php-fpm.

现在,对PHP 7.2版重复该过程。 安装rh-php72rh-php72-php-fpm

  • sudo yum install rh-php72 rh-php72-php-fpm -y

    须藤百胜安装rh-php72 rh-php72-php-fpm -y

Next, run the following commands to start using both software collections:

接下来,运行以下命令以开始使用两个软件集合:

  • sudo scl enable rh-php70 bash

    sudo scl启用rh-php70 bash
  • sudo scl enable rh-php72 bash

    sudo scl启用rh-php72 bash

By default, both PHP versions are listening on port 9000. But in this tutorial, we want to run two versions simultaneously. Therefore, let’s designate two new ports:

默认情况下,两个PHP版本都在端口9000上侦听。 但是在本教程中,我们希望同时运行两个版本。 因此,我们指定两个新端口:

To accomplish this, you can open /etc/opt/rh/rh-php70/php-fpm.d/www.conf in your favorite text editor and change every appearance of 9000 to 9002. Then save and close the file and repeat the process for /etc/opt/rh/rh-php72/php-fpm.d/www.conf, only now substitute 9000 with 9003. Alternately, you can use these two sed commands to make the replacements:

为此,可以在您喜欢的文本编辑器中打开/etc/opt/rh/rh-php70/php-fpm.d/www.conf ,并将每个9000外观更改为9002 。 然后保存并关闭文件,并对/etc/opt/rh/rh-php72/php-fpm.d/www.conf重复此过程,仅现在将9000替换为9003 。 或者,您可以使用以下两个sed命令进行替换:

  • sudo sed -i 's/:9000/:9002/' /etc/opt/rh/rh-php70/php-fpm.d/www.conf

    sudo sed -i's /:9000 /:9002 /'/etc/opt/rh/rh-php70/php-fpm.d/www.conf
  • sudo sed -i 's/:9000/:9003/' /etc/opt/rh/rh-php72/php-fpm.d/www.conf

    sudo sed -i's /:9000 /:9003 /'/etc/opt/rh/rh-php72/php-fpm.d/www.conf

You have now designated a dedicated port for each of your PHP services. Before these modifications will work, however, you must add the ports to your SELinux configuration.

现在,您已经为每个PHP服务指定了专用端口。 但是,在进行这些修改之前,必须将端口添加到SELinux配置中。

SELinux is short for Security Enhanced Linux, and it is enabled by default on CentOS 7. You must add your new ports of 9002 and 9003 to your SELinux database and assign them to your httpd services, or your applications will not run. Use the semanage command to perform this task:

SELinux是Security Enhanced Linux的缩写,默认情况下已在CentOS 7上启用。SELinux必须将90029003新端口添加到SELinux数据库并将它们分配给httpd服务,否则应用程序将无法运行。 使用semanage命令执行此任务:

  • sudo semanage port -a -t http_port_t -p tcp 9002

    sudo semanage端口-a -t http_port_t -p tcp 9002
  • sudo semanage port -a -t http_port_t -p tcp 9003

    sudo semanage端口-a -t http_port_t -p tcp 9003

The -a flag specifies that you are adding an object to the database. The -t flag specifies the type of object, which in this case is http_port_t. And the -p flag designates the tcp protocol. You can learn more about SELinux and the semanage command in this tutorial, or by visiting the official SELinux documentation.

-a标志指定要向数据库添加对象。 -t标志指定对象的类型,在这种情况下为http_port_t-p标志指定tcp协议。 您可以在本教程中或通过访问SELinux官方文档来了解有关SELinux和semanage命令的更多信息。

Now you are ready to start and enable your PHP services. Begin with your rh-php70-php-fpm service and enable it to start at boot:

现在您可以启动并启用PHP服务了。 从您的rh-php70-php-fpm服务开始,并使其在启动时启动:

  • sudo systemctl start rh-php70-php-fpm

    sudo systemctl启动rh-php70-php-fpm
  • sudo systemctl enable rh-php70-php-fpm

    sudo systemctl启用rh-php70-php-fpm

Next, verify the status of your rh-php70-php-fpm service:

接下来,验证您的rh-php70-php-fpm服务的状态:

  • sudo systemctl status rh-php70-php-fpm

    sudo systemctl状态rh-php70-php-fpm

You’ll see an output like this:

您将看到类似以下的输出:


   
   
Output
● rh-php70-php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/rh-php70-php-fpm.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2020-04-18 04:49:59 UTC; 1min 6s ago Main PID: 1852 (php-fpm) Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec" CGroup: /system.slice/rh-php70-php-fpm.service ├─1852 php-fpm: master process (/etc/opt/rh/rh-php70/php-fpm.conf) ├─1853 php-fpm: pool www ├─1854 php-fpm: pool www ├─1855 php-fpm: pool www ├─1856 php-fpm: pool www └─1857 php-fpm: pool www Apr 18 04:49:59 centos-s-1vcpu-1gb-blr1-01 systemd[1]: Starting The PHP FastCGI Process Manager... Apr 18 04:49:59 centos-s-1vcpu-1gb-blr1-01 systemd[1]: Started The PHP FastCGI Process Manager.

Repeating this process, start the rh-php72-php-fpm service and enable it to start at boot:

重复此过程,启动rh-php72-php-fpm服务,并使其在启动时启动:

  • sudo systemctl start rh-php72-php-fpm

    sudo systemctl启动rh-php72-php-fpm
  • sudo systemctl enable rh-php72-php-fpm

    sudo systemctl启用rh-php72-php-fpm

Next, verify the status of your rh-php72-php-fpm service:

接下来,验证您的rh-php72-php-fpm服务的状态:

  • sudo systemctl status rh-php72-php-fpm

    sudo systemctl状态rh-php72-php-fpm

You’ll see another output like this:

您将看到另一个输出,如下所示:


   
   
Output
● rh-php72-php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/rh-php72-php-fpm.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2020-04-18 04:50:04 UTC; 1min 59s ago Main PID: 1876 (php-fpm) Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec" CGroup: /system.slice/rh-php72-php-fpm.service ├─1876 php-fpm: master process (/etc/opt/rh/rh-php72/php-fpm.conf) ├─1877 php-fpm: pool www ├─1878 php-fpm: pool www ├─1879 php-fpm: pool www ├─1880 php-fpm: pool www └─1881 php-fpm: pool www Apr 18 04:50:04 centos-s-1vcpu-1gb-blr1-01 systemd[1]: Starting The PHP FastCGI Process Manager... Apr 18 04:50:04 centos-s-1vcpu-1gb-blr1-01 systemd[1]: Started The PHP FastCGI Process Manager.

At this point you have installed two PHP versions on your server. Next, you will create a directory structure for each website you want to deploy.

至此,您已经在服务器上安装了两个PHP版本。 接下来,您将为要部署的每个网站创建目录结构。

第2步-为两个网站创建目录结构 (Step 2 — Creating Directory Structures for Both Websites)

In this section, you will create a document root directory and an index page for each of your two websites.

在本部分中,您将为两个网站中的每个网站创建一个文档根目录和一个索引页。

First, create document root directories for both site1.your_domain and site2.your_domain:

首先,为site1.your_domainsite2.your_domain创建文档根目录:

  • sudo mkdir /var/www/site1.your_domain

    须藤mkdir / var / www / site1.your_domain

  • sudo mkdir /var/www/site2.your_domain

    须藤mkdir / var / www / site2.your_domain

By default, the Apache webserver runs as an apache user and an apache group. So /var/www/ and all of its files and subdirectories should also be owned by them. Run the following commands to verify the correct ownership and permissions of your website root directories:

默认情况下,Apache Web服务器以apache用户和apache组的身份运行。 因此, /var/www/及其所有文件和子目录也应归它们所有。 运行以下命令来验证您的网站根目录的正确所有权和权限:

  • sudo chown -R apache:apache /var/www/site1.your_domain

    须藤chown -R apache:apache / var / www / site1.your_domain

  • sudo chown -R apache:apache /var/www/site2.your_domain

    须藤chown -R apache:apache / var / www / site2.your_domain

  • sudo chmod -R 755 /var/www/site1.your_domain

    须藤chmod -R 755 / var / www / site1.your_domain

  • sudo chmod -R 755 /var/www/site2.your_domain

    须藤chmod -R 755 / var / www / site2.your_domain

The chown command changes the ownership of your two website directories to the apache user and the apache group. The chmod command changes the permissions associated with that user and group, as well as others.

chown命令将两个网站目录的所有权更改为apache用户和apache组。 chmod命令更改与该用户和组以及其他用户关联的权限。

Next you will create an info.php file inside each website root directory. This will display each website’s PHP version information. Begin with site1:

接下来,您将在每个网站根目录中创建一个info.php文件。 这将显示每个网站PHP版本信息。 从site1开始:

  • sudo vi /var/www/site1.your_domain/info.php

    须藤vi / var / www / site1.your_domain /info.php

Add the following line:

添加以下行:

/var/www/site1.your_domain/info.php
/var/www/site1.your_domain/info.php
<?php phpinfo(); ?>

Save and close the file. Now copy the info.php file you created to site2:

保存并关闭文件。 现在,将您创建的info.php文件复制到site2

  • sudo cp /var/www/site1.your_domain/info.php /var/www/site2.your_domain/info.php

    须藤cp / var / www / site1.your_domain /info.php / var / www / site2.your_domain /info.php

Your web server now has the document root directories that each site requires to serve data to visitors. Next, you will configure your Apache web server to work with two different PHP versions.

现在,您的Web服务器具有每个站点向访问者提供数据所需的文档根目录。 接下来,您将配置Apache Web服务器以使用两个不同PHP版本。

步骤3 —为两个网站配置Apache (Step 3 — Configuring Apache for Both Websites)

In this section, you will create two virtual host configuration files. This will enable your two websites to work simultaneously with two different PHP versions.

在本节中,您将创建两个虚拟主机配置文件。 这将使您的两个网站可以同时使用两个不同PHP版本。

In order for Apache to serve this content, it is necessary to create a virtual host file with the correct directives. You’ll create two new virtual host configuration files inside the directory /etc/httpd/conf.d/.

为了使Apache能够提供此内容,必须使用正确的指令创建虚拟主机文件。 您将在目录/etc/httpd/conf.d/创建两个新的虚拟主机配置文件。

First create a new virtual host configuration file for the website site1.your_domain. Here you will direct Apache to render content using PHP 7.0:

首先为网站site1.your_domain创建一个新的虚拟主机配置文件。 在这里,您将指导Apache使用PHP 7.0呈现内容:

  • sudo vi /etc/httpd/conf.d/site1.your_domain.conf

    须藤vi /etc/httpd/conf.d/ site1.your_domain .conf

Add the following content. Make sure the website directory path, server name, port, and PHP version match your setup:

添加以下内容。 确保网站目录路径,服务器名称,端口和PHP版本与您的设置匹配:

/etc/httpd/conf.d/site1.your_domain.conf
/etc/httpd/conf.d/site1.your_domain.conf
<VirtualHost *:80>
     ServerAdmin admin@site1.your_domain
     ServerName site1.your_domain
     DocumentRoot /var/www/site1.your_domain
     DirectoryIndex info.php
     SetHandler "proxy:fcgi://127.0.0.1:9002
     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
     AddHandler php70-fcgi .php
     Action php70-fcgi /cgi-bin/php70.fcgi
     ErrorLog /var/log/httpd/site1.your_domain_error.log
     CustomLog /var/log/httpd/site1.your_domain_access.log combined
</VirtualHost>

For DocumentRoot you are specifying the path of your website root directory. For ServerAdmin you are adding an email that the your_domain site administrator can access. For ServerName you are adding the url for your first subdomain. For SetHandler you are specifying port 9002. The remaining directives also configure your service to deploy PHP 7.0.

对于DocumentRoot您要指定网站根目录的路径。 对于ServerAdmin您正在添加一封电子邮件, your_domain站点管理员可以访问。 对于ServerName您要添加第一个子域的URL。 对于SetHandler您指定端口9002 。 其余指令也将您的服务配置为部署PHP 7.0。

Save and close the file.

保存并关闭文件。

Next, create a new virtual host configuration file for the website site2.your_domain. You will specify this subdomain to deploy PHP 7.2:

接下来,为网站site2.your_domain创建一个新的虚拟主机配置文件。 您将指定此子域来部署PHP 7.2:

  • sudo vi /etc/httpd/conf.d/site2.your_domain.conf

    须藤vi /etc/httpd/conf.d/ site2.your_domain .conf

Add the following content. Again, make sure the website directory path, server name, port, and PHP version match your unique information:

添加以下内容。 再次,确保网站目录路径,服务器名称,端口和PHP版本与您的唯一信息匹配:

/etc/httpd/conf.d/site2.your_domain.conf
/etc/httpd/conf.d/site2.your_domain.conf
<VirtualHost *:80>
     ServerAdmin admin@site2.your_domain
     ServerName site2.your_domain
     DocumentRoot /var/www/site2.your_domain
     DirectoryIndex info.php
     SetHandler "proxy:fcgi://127.0.0.1:9003
     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
     AddHandler php72-fcgi .php
     Action php72-fcgi /cgi-bin/php72.fcgi
     ErrorLog /var/log/httpd/site2.your_domain_error.log
     CustomLog /var/log/httpd/site2.your_domain_access.log combined
</VirtualHost>

Save and close the file when you are finished. Then check the Apache configuration file for any syntax errors:

完成后保存并关闭文件。 然后检查Apache配置文件是否存在语法错误:

  • sudo apachectl configtest

    须藤apachectl configtest

You’ll see an output printing Syntax OK:

您将看到输出打印Syntax OK

Output
输出量
  • Syntax OK

    语法确定

Finally, restart the Apache service to implement your changes:

最后,重新启动Apache服务以实现您的更改:

  • sudo systemctl restart httpd

    sudo systemctl重新启动httpd

Now that you have configured Apache to serve each site, you will test them to make sure the proper PHP versions are running.

既然已经配置了Apache为每个站点提供服务,您将对其进行测试以确保正在运行正确PHP版本。

第4步-测试两个网站 (Step 4 — Testing Both Websites)

At this point, you have configured two websites to run two different versions of PHP. Now test the results.

至此,您已经配置了两个网站来运行两个不同版本PHP。 现在测试结果。

Open your web browser and visit both sites http://site1.your_domain and http://site2.your_domain. You will see two pages that look like this:

打开您的Web浏览器,并访问两个站点http:// site1.your_domainhttp:// site2.your_domain 。 您将看到两个页面,如下所示:

Note the titles. The first page indicates that site1.your_domain deployed PHP version 7.0. The second indicates that site2.your_domain deployed PHP version 7.2.

注意标题。 第一页表明site1.your_domain部署了PHP 7.0版。 第二个指示site2.your_domain部署了PHP版本7.2。

Now that you’ve tested your sites, remove the info.php files. Because they contain sensitive information about your server and are accessible to unauthorized users, they pose a security vulnerability. Remove the files:

现在,您已经测试了站点,请删除info.php文件。 因为它们包含有关服务器的敏感信息,并且未经授权的用户可以访问,所以它们构成了安全漏洞。 删除文件:

  • sudo rm -rf /var/www/site1.your_domain/info.php

    须藤rm -rf / var / www / site1.your_domain /info.php

  • sudo rm -rf /var/www/site2.your_domain/info.php

    须藤rm -rf / var / www / site2.your_domain /info.php

You now have a single CentOS 7 server handling two websites with two different PHP versions. PHP-FPM, however, is not limited to this one application.

现在,您只有一台CentOS 7服务器,可以处理具有两个不同PHP版本的两个网站。 但是,PHP-FPM不限于此一个应用程序。

结论 (Conclusion)

You have now combined virtual hosts and PHP-FPM to serve multiple websites and multiple versions of PHP on a single server. The only practical limit on the number of PHP sites and PHP versions that your Apache service can handle is the processing power of your instance.

现在,您已经将虚拟主机和PHP-FPM组合在一起,以在单个服务器上为多个网站和多个版本PHP提供服务。 Apache服务可以处理PHP站点和PHP版本数量的唯一实际限制是实例的处理能力。

From here you might consider exploring PHP-FPM’s more advanced features, like its adaptive spawning process or how it can log sdtout and stderr Alternatively, you could now secure your websites. To accomplish this, you can follow our tutorial on how to secure your sites with free TLS/SSL certificates from Let’s Encrypt.

从这里开始,您可能会考虑探索PHP-FPM的更高级功能 ,例如其自适应产卵过程或如何记录sdtoutstderr或者,您现在可以保护您的网站。 为此,您可以按照我们的教程进行, 教程介绍了如何使用来自Let's Encrypt的免费TLS / SSL证书保护网站的安全。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-run-multiple-php-versions-on-one-server-using-apache-and-php-fpm-on-centos-7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值