freebsd 9.1.1_如何在FreeBSD 12.0上使用MPM事件和PHP-FPM配置Apache HTTP

freebsd 9.1.1

The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

作者选择了“ 开放互联网/言论自由基金会”作为“ Write for DOnations”计划的一部分来接受捐赠。

介绍 (Introduction)

The Apache HTTP web server has evolved through the years to work in different environments and solve different needs. One important problem Apache HTTP has to solve, like any web server, is how to handle different processes to serve an http protocol request. This involves opening a socket, processing the request, keeping the connection open for a certain period, handling new events occurring through that connection, and returning the content produced by a program made in a particular language (such as PHP, Perl, or Python). These tasks are performed and controlled by a Multi-Processing Module (MPM).

多年来, Apache HTTP Web服务器已经发展为可以在不同的环境中工作并解决不同的需求。 与任何Web服务器一样,Apache HTTP必须解决的一个重要问题是如何处理不同的进程来满足http协议请求。 这涉及打开套接字,处理请求,在一定时间内保持连接打开,处理通过该连接发生的新事件以及返回由使用特定语言(例如PHP,Perl或Python)制作的程序产生的内容。 。 这些任务由多处理模块 (MPM)执行和控制。

Apache HTTP comes with three different MPM:

Apache HTTP带有三种不同的MPM:

  • Pre-fork: A new process is created for each incoming connection reaching the server. Each process is isolated from the others, so no memory is shared between them, even if they are performing identical calls at some point in their execution. This is a safe way to run applications linked to libraries that do not support threading—typically older applications or libraries.

    前叉 :为到达服务器的每个传入连接创建一个新过程。 每个进程彼此隔离,因此即使它们在执行的某个时刻执行相同的调用,也不会在它们之间共享内存。 这是运行链接到不支持线程的库的应用程序的安全方法,这些库通常是较旧的应用程序或库。

  • Worker: A parent process is responsible for launching a pool of child processes, some of which are listening for new incoming connections, and others are serving the requested content. Each process is threaded (a single thread can handle one connection) so one process can handle several requests concurrently. This method of treating connections encourages better resource utilization, while still maintaining stability. This is a result of the pool of available processes, which often has free available threads ready to immediately serve new connections.

    工作者 :父进程负责启动子进程池,其中一些进程正在侦听新的传入连接,而其他进程则在服务所请求的内容。 每个进程都有线程(一个线程可以处理一个连接),因此一个进程可以同时处理多个请求。 这种处理连接的方法鼓励更好地利用资源,同时仍保持稳定性。 这是可用进程池的结果,该进程通常具有准备就绪的可用空闲线程以立即为新连接提供服务。

  • Event: Based on worker, this MPM goes one step further by optimizing how the parent process schedules tasks to the child processes and the threads associated to those. A connection stays open for 5 seconds by default and closes if no new event happens; this is the keep-alive directive default value, which retains the thread associated to it. The Event MPM enables the process to manage threads so that some threads are free to handle new incoming connections while others are kept bound to the live connections. Allowing re-distribution of assigned tasks to threads will make for better resource utilization and performance.

    事件 :基于工作人员,此MPM通过优化父进程如何调度任务到子进程以及与这些子进程关联的线程,又向前迈进了一步。 默认情况下,连接保持打开状态5秒钟,如果没有新事件发生,则关闭连接。 这是keep-alive指令的默认值,它保留与之关联的线程。 使用事件MPM,进程可以管理线程,以便某些线程可以自由处理新的传入连接,而另一些线​​程则保持绑定到实时连接。 允许将分配的任务重新分配给线程将有助于提高资源利用率和性能。

The MPM Event module is a fast multi-processing module available on the Apache HTTP web server.

MPM事件模块是Apache HTTP Web服务器上可用的快速多处理模块。

PHP-FPM is the FastCGI Process Manager for PHP. The FastCGI protocol is based on the Common Gateway Interface (CGI), a protocol that sits between applications and web servers like Apache HTTP. This allows developers to write applications separately from the behavior of web servers. Programs run their processes independently and pass their product to the web server through this protocol. Each new connection in need of processing by an application will create a new process.

PHP-FPM是用于PHP的FastCGI流程管理器。 FastCGI协议基于通用网关接口(CGI),该协议位于应用程序和Web服务器(如Apache HTTP)之间。 这使开发人员可以独立于Web服务器的行为编写应用程序。 程序独立运行其进程,并通过此协议将其产品传递到Web服务器。 每个需要应用程序处理的新连接都将创建一个新进程。

By combining the MPM Event in Apache HTTP with the PHP FastCGI Process Manager (PHP-FPM) a website can load faster and handle more concurrent connections while using fewer resources.

通过将Apache HTTP中的MPM事件与PHP FastCGI流程管理器(PHP-FPM)相结合,网站可以更快地加载并处理更多的并发连接,同时使用更少的资源。

In this tutorial you will improve the performance of the FAMP stack by changing the default multi-processing module from pre-fork to event and by using the PHP-FPM process manager to handle PHP code instead of the classic mod_php in Apache HTTP.

在本教程中,您将通过将默认的多处理模块从前叉更改为事件,并使用PHP-FPM流程管理器来处理PHP代码而不是Apache HTTP中的经典mod_php来提高FAMP堆栈的性能。

先决条件 (Prerequisites)

Before you begin this guide you’ll need the following:

在开始本指南之前,您需要满足以下条件:

  • A FreeBSD 12.0 server set up following this guide.

    按照本指南设置FreeBSD 12.0服务器。

  • The FAMP stack installed on your server following this tutorial.

    按照本教程在您的服务器上安装的FAMP堆栈。

  • Access to a user with root privileges (or allowed by using sudo) in order to make configuration changes.

    具有root特权(或使用sudo允许)访问用户,以进行配置更改。

步骤1 —更改多处理模块 (Step 1 — Changing the Multi-Processing Module)

You’ll begin by looking for the pre-fork directive in the httpd.conf file. This is the main configuration file for Apache HTTP in which you can enable and disable modules. You can edit and set directives such as the listening port where Apache HTTP will serve content or the location of the content to display in this file.

首先,请在httpd.conf文件中查找前叉指令。 这是Apache HTTP的主要配置文件,您可以在其中启用和禁用模块。 您可以编辑和设置指令,例如Apache HTTP将在其中提供内容的侦听端口或要在此文件中显示的内容的位置。

To make these changes, you’ll use the nl, number line, program, with the -ba flag to count and number lines so that nothing is mismatched at a later stage. Combined with grep this command will first count all the lines in the file specified in the path, and once finished, it will search for the string of characters you’re looking for.

要进行这些更改,您将使用带有-ba标志的nl ,数字行,程序来对数字行进行计数和编号,以便在以后的任何时间都不会出现不匹配的情况。 与grep结合使用时,此命令将首先计算路径中指定的文件中的所有行,完成后,它将搜索您要查找的字符串。

Run the following command so that the nl program will process and number the lines in httpd.conf. Then, grep will process the output by searching for the given string of characters 'mod_mpm_prefork':

运行以下命令,以便nl程序将对httpd.conf的行进行处理和编号。 然后, grep将通过搜索给定的字符串'mod_mpm_prefork'来处理输出:

  • nl -ba /usr/local/etc/apache24/httpd.conf | grep 'mod_mpm_prefork'

    nl -ba /usr/local/etc/apache24/httpd.conf | grep'mod_mpm_prefork'

As output you’ll see something similar to:

作为输出,您将看到类似以下内容:


   
   
Output
67 LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so

Let’s edit line 67 with your text editor. In this tutorial, you’ll use vi, which is the default editor on FreeBSD:

让我们用文本编辑器编辑第67行。 在本教程中,您将使用vi ,它是FreeBSD上的默认编辑器:

  • sudo vi +67 /usr/local/etc/apache24/httpd.conf

    须藤vi + 67 /usr/local/etc/apache24/httpd.conf

Append a # symbol at the beginning of the line so this line is commented out, like so:

在该行的开头添加一个#符号,以便将该行注释掉,如下所示:

/usr/local/etc/apache24/httpd.conf
/usr/local/etc/apache24/httpd.conf
...
# LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so
...

By appending the # symbol you’ve disabled the pre-fork MPM module.

通过添加#符号,您已禁用了前叉MPM模块。

Now you’ll find the event directive in the same httpd.conf file.

现在,您将在同一httpd.conf文件中找到事件指令。

  • nl -ba /usr/local/etc/apache24/httpd.conf | grep mpm_event

    nl -ba /usr/local/etc/apache24/httpd.conf | grep mpm_event

You’ll see output similar to the following:

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


   
   
Output
... 66 #LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so ...

Now you’ll remove the # symbol in line 66 to enable the Event MPM:

现在,您将在第66行中删除#符号以启用事件MPM:

  • sudo vi +66 /usr/local/etc/apache24/httpd.conf

    须藤vi + 66 /usr/local/etc/apache24/httpd.conf

The directive will now read as follows:

该指令现在将如下所示:

/usr/local/etc/apache24/httpd.conf
/usr/local/etc/apache24/httpd.conf
...
LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so
...

Now that you’ve switched the configuration from the MPM pre-fork to event, you can remove the mod_php73 package connecting the PHP processor to Apache HTTP, since it is no longer necessary and will interfere if it remains on the system:

现在,您已经将配置从MPM前叉切换到了事件,您可以删除将PHP处理器连接到Apache HTTP的mod_php73软件包,因为它不再是必需的,并且如果仍然存在于系统中,将会造成干扰:

  • sudo pkg remove -y mod_php73

    sudo pkg删除-y mod_php73

Make sure the configuration is correct by running the following command to test:

通过运行以下命令进行测试,以确保配置正确:

  • sudo apachectl configtest

    须藤apachectl configtest

If you see Syntax OK in your output, you can restart the Apache HTTP server:

如果在输出中看到“ Syntax OK ,则可以重新启动Apache HTTP服务器:

  • sudo apachectl restart

    sudo apachectl重新启动

Note: If there are other running HTTP connections on your server a graceful restart is recommended instead of a regular restart. This will ensure that users are not pushed out, losing their connection:

注意:如果服务器上还有其他正在运行的HTTP连接,则建议正常重启而不是常规重启。 这将确保用户不会被推出,从而失去连接:

  • sudo apachectl graceful

    sudo apachectl优美

You’ve switched the MPM from pre-fork to event and removed the mod_php73 module connection PHP to Apache HTTP. In the next step you’ll install the PHP-FPM module and configure Apache HTTP so that it can communicate with PHP more quickly.

您已将MPM从前叉切换为事件,并删除了与Apache HTTP的mod_php73模块连接PHP。 在下一步中,您将安装PHP-FPM模块并配置Apache HTTP,以便它可以更快地与PHP通信。

第2步-配置Apache HTTP以使用FastCGI进程管理器 (Step 2 — Configuring Apache HTTP to Use the FastCGI Process Manager)

FreeBSD has several supported versions of PHP that you can install via the package manager. On FreeBSD different binaries of the various available versions are compiled instead of using just one like most GNU/Linux distributions offer in their default repositories. To follow best practice you’ll use the supported version, which you can check on at PHP’s supported versions page.

FreeBSD有几个受支持PHP版本,您可以通过软件包管理器进行安装。 在FreeBSD上,将编译各种可用版本的不同二进制文件,而不是像大多数GNU / Linux发行版在其默认存储库中提供的那样使用一种二进制文件。 为了遵循最佳实践,您将使用受支持的版本,可以在PHP的受支持版本页面上进行检查。

In this step you’ll add PHP-FPM as a running service to start at boot. You’ll also configure Apache HTTP to work with PHP by adding a dedicated configuration for the module as well as enabling some further modules in httpd.conf.

在此步骤中,您将添加PHP-FPM作为正在运行的服务,以在启动时启动。 您还将通过为模块添加专用配置并在httpd.conf启用其他一些模块,来配置Apache HTTP与PHP一起使用。

First you’ll append 'php_fpm_enable=YES' to the /etc/rc.conf file so the PHP-FPM service can start. You’ll do that by using the sysrc command:

首先,您将'php_fpm_enable=YES'附加到/etc/rc.conf文件中,以便可以启动PHP-FPM服务。 您将使用sysrc命令执行此操作:

  • sudo sysrc php_fpm_enable="YES"

    须藤sysrc php_fpm_enable =“是”

Now you’ll add the php-fpm module into the Apache module’s directory, so it is configured to be used by Apache HTTP. Create the following file to do so:

现在,将php-fpm模块添加到Apache模块的目录中,以便将其配置为由Apache HTTP使用。 为此,请创建以下文件:

  • sudo vi /usr/local/etc/apache24/modules.d/030_php-fpm.conf

    须藤vi /usr/local/etc/apache24/modules.d/030_php-fpm.conf

Add the following into 030_php-fpm.conf:

将以下内容添加到030_php-fpm.conf

/usr/local/etc/apache24/modules.d/030_php-fpm.conf
/usr/local/etc/apache24/modules.d/030_php-fpm.conf
<IfModule proxy_fcgi_module>
    <IfModule dir_module>
        DirectoryIndex index.php
    </IfModule>
    <FilesMatch "\.(php|phtml|inc)$">
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</IfModule>

This states that if the module 'proxy_fcgi' is enabled as well as the 'dir_module' then any processed files matching the extensions in parentheses should be handled by the FastCGI process manager running on the local machine through port 9000—as if the local machine were a proxy server. This is where the PHP-FPM module and Apache HTTP interconnect. To achieve this, you’ll activate further modules during this step.

这说明,如果同时启用了模块'proxy_fcgi''dir_module'则任何与括号中的扩展名匹配的已处理文件都应由在本地计算机上通过端口9000运行的FastCGI进程管理器处理-就像本地计算机代理服务器。 这是PHP-FPM模块和Apache HTTP互连的地方。 为此,您将在此步骤中激活其他模块。

To enable the proxy module, you’ll first search for it in the httpd.conf file:

要启用代理模块,您首先需要在httpd.conf文件中进行搜索:

  • nl -ba /usr/local/etc/apache24/httpd.conf | grep mod_proxy.so

    nl -ba /usr/local/etc/apache24/httpd.conf | grep mod_proxy.so

You’ll see output similar to the following:

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


   
   
Output
... 129 #LoadModule proxy_module libexec/apache24/mod_proxy.so ...

You’ll uncomment the line by removing the # symbol:

您将通过删除#符号来取消注释该行:

  • sudo vi +129 /usr/local/etc/apache24/httpd.conf

    须藤vi + 129 /usr/local/etc/apache24/httpd.conf

The line will look as follows once edited:

编辑后,该行将如下所示:

/usr/local/etc/apache24/httpd.conf
/usr/local/etc/apache24/httpd.conf
...
LoadModule proxy_module libexec/apache24/mod_proxy.so
...

Now you can activate the FastCGI module. Find the module with the following command:

现在您可以激活FastCGI模块。 使用以下命令查找模块:

  • nl -ba /usr/local/etc/apache24/httpd.conf | grep mod_proxy_fcgi.so

    nl -ba /usr/local/etc/apache24/httpd.conf | grep mod_proxy_fcgi.so

You’ll see something similar to the following:

您将看到类似于以下内容:


   
   
Output
... 133 #LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so ...

Now uncomment the line 133 as you’ve already done with the other modules:

现在,取消注释第133行,就像使用其他模块一样:

  • sudo vi +133 /usr/local/etc/apache24/httpd.conf

    须藤vi + 133 /usr/local/etc/apache24/httpd.conf

You’ll leave the line as follows:

您将按如下所示离开该行:

/usr/local/etc/apache24/httpd.conf
/usr/local/etc/apache24/httpd.conf
...
LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so
...

Once this is done you’ll start the PHP-FPM service:

完成此操作后,您将启动PHP-FPM服务:

  • sudo service php-fpm start

    sudo服务php-fpm启动

And you’ll restart Apache so it loads the latest configuration changes incorporating the PHP module:

然后您将重新启动Apache,以便它加载包含PHP模块的最新配置更改:

  • sudo apachectl restart

    sudo apachectl重新启动

You’ve installed the PHP-FPM module, configured Apache HTTP to work with it, enabled the necessary modules for the FastCGI protocol to work, and started the corresponding services.

您已经安装了PHP-FPM模块,将Apache HTTP配置为可以使用它,为FastCGI协议启用了必要的模块,并启动了相应的服务。

Now that Apache has the Event MPM module enabled and PHP-FPM is present and running, it is time to check everything is working as intended.

现在,Apache已启用了事件MPM模块,并且PHP-FPM已存在并正在运行,现在该检查所有功能是否按预期工作。

步骤3 —检查您的配置 (Step 3 — Checking Your Configuration)

In order to check that the configuration changes have been applied you’ll run some tests. The first one will check what multi-processing module Apache HTTP is using. The second will verify that PHP is using the FPM manager.

为了检查配置更改是否已应用,您将运行一些测试。 第一个将检查Apache HTTP使用的多处理模块。 第二个将验证PHP是否正在使用FPM管理器。

Check the Apache HTTP server by running the following command:

通过运行以下命令来检查Apache HTTP服务器:

  • sudo apachectl -M | grep 'mpm'

    须藤apachectl -M | grep'mpm'

Your output will be as follows:

您的输出将如下所示:


   
   
Output
mpm_event_module (shared)

You can repeat the same for the proxy module and FastCGI:

您可以对代理模块和FastCGI重复相同的操作:

  • sudo apachectl -M | grep 'proxy'

    须藤apachectl -M | grep'代理'

The output will show:

输出将显示:


   
   
Output
proxy_module (shared) proxy_fcgi_module (shared)

If you would like to see the entire list of the modules, you can remove the the second part of the command after -M.

如果要查看模块的整个列表,可以删除-M之后的命令的第二部分。

It is now time to check if PHP is using the FastCGI Process Manager. To do so you’ll write a very small PHP script that will show you all the information related to PHP.

现在是时候检查PHP是否正在使用FastCGI Process Manager。 为此,您将编写一个非常小PHP脚本,该脚本将向您显示与PHP相关的所有信息。

Run the following command to write a file named as follows:

运行以下命令以编写一个名为如下的文件:

  • sudo vi /usr/local/www/apache24/data/info.php

    须藤vi /usr/local/www/apache24/data/info.php

Add the following content into the info.php file:

将以下内容添加到info.php文件中:

info.php
info.php
<?php phpinfo(); ?>

Now visit your server’s URL and append info.php at the end like so: http://your_server_IP_address/info.php.

现在,访问服务器的URL并在末尾附加info.php ,如下所示: http:// your_server_IP_address /info.php

The Server API entry will be FPM/FastCGI.

服务器API条目将为FPM / FastCGI

Remember to delete the info.php file after this check so no information about the server is publicly disclosed.

请记住在检查之后删除info.php文件,这样就不会公开公开有关服务器的信息。

  • sudo rm /usr/local/www/apache24/data/info.php

    须藤rm /usr/local/www/apache24/data/info.php

You’ve checked the working status of the MPM module, the modules handling the FastCGI, and the handling of PHP code.

您已经检查了MPM模块的工作状态,处理FastCGI的模块以及PHP代码的处理。

结论 (Conclusion)

You’ve optimized your original FAMP stack, so the number of connections to create new Apache HTTP processes has increased, PHP-FPM will handle PHP code more efficiently, and overall resource utilization has improved.

您已经优化了原始FAMP堆栈,因此创建新的Apache HTTP进程的连接数量增加了,PHP-FPM将更有效地处理PHP代码,并且总体资源利用率得到了提高。

See the Apache HTTP server project documentation for more information on the different modules and related projects.

有关不同模块和相关项目的更多信息,请参见Apache HTTP服务器项目文档

翻译自: https://www.digitalocean.com/community/tutorials/how-to-configure-apache-http-with-mpm-event-and-php-fpm-on-freebsd-12-0

freebsd 9.1.1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值