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

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.3. The second, site2.your_domain, will deploy PHP 7.4.

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

先决条件 (Prerequisites)

  • One CentOS 8 server with at least 1GB of RAM set up by following the Initial Server Setup with CentOS 8, including a sudo non-root user and a firewall.

    通过使用CentOS 8进行初始服务器设置来设置一台至少具有1GB RAM的CentOS 8服务器,包括sudo非root用户和防火墙。

  • An Apache web server set up and configured by following How to Install the Apache Web Server on CentOS 8.

    通过遵循如何在CentOS 8上安装 Apache Web服务器来设置和配置Apache Web服务器 。

  • A domain name configured to point to your CentOS 8 server. You can learn how to point domains to DigitalOcean Droplets by following How To Point to DigitalOcean Nameservers From Common Domain Registrars. For the purposes of this tutorial, you will use two subdomains, each specified with an A record in your DNS settings: site1.your_domain and site2.your_domain.

    配置为指向您的CentOS 8服务器的域名。 通过遵循如何从通用域注册商指向DigitalOcean域名服务器,您可以学习如何将域指向DigitalOcean Droplet。 就本教程而言,您将使用两个子域,每个子域在DNS设置中均指定为A记录: site1.your_domainsite2.your_domain

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

With the prerequisites completed, you will now install PHP versions 7.3 and 7.4, as well as PHP-FPM and several additional extensions. In order to install multiple versions of PHP, you will need to install and enable the Remi repository to your system. Which also offers the latest versions of the PHP stack on CentOS 8 system.

完成前提条件后,您现在将安装PHP版本7.3和7.4,以及PHP-FPM和一些其他扩展。 为了安装PHP的多个版本,您将需要安装Remi存储库并将其启用到系统中。 它也提供了CentOS 8系统上最新版本PHP堆栈。

You can add the both repository to your system using the below commands:

您可以使用以下命令将这两个存储库都添加到系统中:

  • sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm

    sudo dnf安装http://rpms.remirepo.net/enterprise/remi-release-8.rpm

The command above will also enable the EPEL repository.

上面的命令还将启用EPEL存储库。

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

首先,让我们发现Remi提供了哪些版本PHP 7:

  • sudo dnf module list php

    sudo dnf模块列表php

You’ll see an output like this:

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


   
   
     
     
     
     
Output
Remi's Modular repository for Enterprise Linux 8 - x86_64 Name Stream Profiles Summary php remi-7.2 common [d], devel, minimal PHP scripting language php remi-7.3 common [d], devel, minimal PHP scripting language php remi-7.4 common [d], devel, minimal PHP scripting language

Next, disable the default PHP module and enable Remi’s PHP7.3 module using the below command:

接下来,禁用默认PHP模块,并使用以下命令启用RemiPHP7.3模块:

  • sudo dnf module reset php

    sudo dnf模块重置PHP
  • sudo dnf module enable php:remi-7.3

    sudo dnf模块启用php:remi-7.3

Lets start to installing php73 and php73-php-fpm:

让我们开始安装php73php73-php-fpm

  • sudo dnf install php73 php73-php-fpm -y

    须藤dnf安装php73 php73-php-fpm -y
  • php73 is a metapackage that can be used to run PHP application.

    php73是可用于运行PHP应用程序的元包。

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

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

Now repeat the process for PHP version 7.4. Install php74 and php74-php-fpm.

现在,为PHP 7.4重复该过程。 安装php74php74-php-fpm

  • sudo dnf module reset php

    sudo dnf模块重置PHP
  • sudo dnf module enable php:remi-7.4

    sudo dnf模块启用php:remi-7.4
  • sudo dnf install php74 php74-php-fpm -y

    须藤dnf安装php74 php74-php-fpm -y

After installing both PHP versions, start the php73-php-fpm service and enable it to start at boot with the following commands:

在安装两个PHP版本之后,启动php73-php-fpm服务,并使用以下命令使其在启动时启动:

  • sudo systemctl start php73-php-fpm

    须藤systemctl启动php73-php-fpm
  • sudo systemctl enable php73-php-fpm

    sudo systemctl启用php73-php-fpm

Next, verify the status of php73-php-fpm service with the following commands:

接下来,使用以下命令验证php73-php-fpm服务的状态:

  • sudo systemctl status php73-php-fpm

    sudo systemctl状态php73-php-fpm

You’ll see the following output:

您将看到以下输出:

  • ● php73-php-fpm.service - The PHP FastCGI Process Manager

    ●php73-php-fpm.service-PHP FastCGI流程管理器
  • Loaded: loaded (/usr/lib/systemd/system/php73-php-fpm.service; enabled; vendor preset: disabled)

    已加载:已加载(/usr/lib/systemd/system/php73-php-fpm.service;已启用;供应商预设:已禁用)
  • Active: active (running) since Wed 2020-04-22 05:14:46 UTC; 52s ago

    活动:自UTC 2020-04-22 05:14:46 UTC起活动 (运行); 52s前

  • Main PID: 14206 (php-fpm)

    主PID:14206(php-fpm)
  • Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"

    状态:“进程处于活动状态:0,空闲:5,请求:0,慢速:0,流量:0req / sec”
  • Tasks: 6 (limit: 5059)

    任务:6(限制:5059)
  • Memory: 25.9M

    记忆体:25.9M
  • CGroup: /system.slice/php73-php-fpm.service

    CGroup:/system.slice/php73-php-fpm.service
  • ├─14206 php-fpm: master process (/etc/opt/remi/php73/php-fpm.conf)

    ├─14206php-fpm:主进程(/etc/opt/remi/php73/php-fpm.conf)
  • ├─14207 php-fpm: pool www

    ├─14207php-fpm:池www
  • ├─14208 php-fpm: pool www

    ├─14208php-fpm:池www
  • ├─14209 php-fpm: pool www

    ├─14209php-fpm:池www
  • ├─14210 php-fpm: pool www

    ├─14210php-fpm:池www
  • └─14211 php-fpm: pool www

    └─14211php-fpm:池www
  • Apr 22 05:14:46 centos-s-1vcpu-1gb-nyc3-01 systemd[1]: Starting The PHP FastCGI Process Manager...

    4月22日05:14:46 centos-s-1vcpu-1gb-nyc3-01 systemd [1]:启动PHP FastCGI进程管理器...
  • Apr 22 05:14:46 centos-s-1vcpu-1gb-nyc3-01 systemd[1]: Started The PHP FastCGI Process Manager.

    4月22日05:14:46 centos-s-1vcpu-1gb-nyc3-01 systemd [1]:启动了PHP FastCGI进程管理器。

Repeating this process, now start the php74-php-fpm service and enable it to start at boot with the following commands:

重复此过程,现在启动php74-php-fpm服务,并使用以下命令使其在启动时启动:

  • sudo systemctl start php74-php-fpm

    须藤systemctl启动php74-php-fpm
  • sudo systemctl enable php74-php-fpm

    sudo systemctl启用php74-php-fpm

And then verify the status of php74-php-fpm service with the following commands:

然后使用以下命令验证php74-php-fpm服务的状态:

  • sudo systemctl status php74-php-fpm

    sudo systemctl状态php74-php-fpm

You’ll see the following output:

您将看到以下输出:

  • ● php74-php-fpm.service - The PHP FastCGI Process Manager

    ●php74-php-fpm.service-PHP FastCGI流程管理器
  • Loaded: loaded (/usr/lib/systemd/system/php74-php-fpm.service; enabled; vendor preset: disabled)

    已加载:已加载(/usr/lib/systemd/system/php74-php-fpm.service;已启用;供应商预设:已禁用)
  • Active: active (running) since Wed 2020-04-22 05:16:16 UTC; 23s ago

    活动:自UTC 2020-04-22 05:16:16 UTC起活动 (运行); 23s前

  • Main PID: 14244 (php-fpm)

    主PID:14244(php-fpm)
  • Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"

    状态:“进程处于活动状态:0,空闲:5,请求:0,慢速:0,流量:0req / sec”
  • Tasks: 6 (limit: 5059)

    任务:6(限制:5059)
  • Memory: 18.8M

    记忆体:18.8M
  • CGroup: /system.slice/php74-php-fpm.service

    CGroup:/system.slice/php74-php-fpm.service
  • ├─14244 php-fpm: master process (/etc/opt/remi/php74/php-fpm.conf)

    ├─14244php-fpm:主进程(/etc/opt/remi/php74/php-fpm.conf)
  • ├─14245 php-fpm: pool www

    ├─14245php-fpm:池www
  • ├─14246 php-fpm: pool www

    ├─14246php-fpm:池www
  • ├─14247 php-fpm: pool www

    ├─14247php-fpm:池www
  • ├─14248 php-fpm: pool www

    ├─14248php-fpm:池www
  • └─14249 php-fpm: pool www

    └─14249php-fpm:池www
  • Apr 22 05:16:15 centos-s-1vcpu-1gb-nyc3-01 systemd[1]: Starting The PHP FastCGI Process Manager...

    4月22日05:16:15 centos-s-1vcpu-1gb-nyc3-01 systemd [1]:启动PHP FastCGI进程管理器...
  • Apr 22 05:16:16 centos-s-1vcpu-1gb-nyc3-01 systemd[1]: Started The PHP FastCGI Process Manager.

    4月22日05:16:16 centos-s-1vcpu-1gb-nyc3-01 systemd [1]:启动了PHP FastCGI进程管理器。

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.yourdomain and site2.yourdomain:

首先,为site1.your 和site2.yourdomain创建文档根目录:

  • 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, Apache webserver runs as a apache user and apache group. To ensure that you have the correct ownership and permissions of your website root directories, execute the following commands:

默认情况下,Apache Web服务器以apache用户和apache组的身份运行。 为确保您具有网站根目录的正确所有权和权限,请执行以下命令:

  • 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:

添加以下行:

site1.your_domain/info.php’>/var/www/site1.your_domain/info.php
site1.your_domain / info.php'> / var / www / site1.your_domain /info.php

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 file 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 php7.3:

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

  • 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, and PHP version match your setup:

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

/etc/httpd/conf.d/site1.your_domain.conf
/etc/httpd/conf.d/site1.your_domain.conf

     ServerAdmin [email protected]_domain
     ServerName site1.your_domain
     DocumentRoot /var/www/site1.your_domain
     DirectoryIndex info.php
     ErrorLog /var/log/httpd/site1.your_domain-error.log
     CustomLog /var/log/httpd/site1.your_domain-access.log combined

  
    
        SetHandler "proxy:unix:/var/opt/remi/php73/run/php-fpm/www.sock|fcgi://localhost"
    
  

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 the PHP-FPM socket file for php7.3.

对于DocumentRoot您要指定网站根目录的路径。 对于ServerAdmin您正在添加一封电子邮件, your_domain站点管理员可以访问。 对于ServerName您要添加第一个子域的URL。 对于SetHandler,您要为php7.3指定PHP-FPM套接字文件。

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 php7.4:

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

  • 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, and PHP version match your unique information:

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

/etc/httpd/conf.d/site2.your_domain.conf
/etc/httpd/conf.d/site2.your_domain.conf

     ServerAdmin [email protected]_domain
     ServerName site2.your_domain
     DocumentRoot /var/www/site2.your_domain
     DirectoryIndex info.php
     ErrorLog /var/log/httpd/site2.your_domain-error.log
     CustomLog /var/log/httpd/site2.your_domain-access.log combined
  
    
        SetHandler "proxy:unix:/var/opt/remi/php74/run/php-fpm/www.sock|fcgi://localhost"
    
  

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

完成后保存并关闭文件。 然后,使用以下命令检查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.yourdomain deployed PHP version 7.3. The second indicates that site2.yourdomain deployed PHP version 7.4.

注意标题。 第一页表明site1.your 域已部署PHP 7.3版。 第二个指示site2.yourdomain部署了PHP版本7.4。

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. To remove both files, run the following commands:

现在,您已经测试了站点,请删除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 8 server handling two websites with two different PHP versions. PHP-FPM, however, is not limited to this one application.

现在,您只有一台CentOS 8服务器,可以处理具有两个不同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 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-8

你可能感兴趣的:(linux,java,运维,centos,nginx)