当前位置: 首页 > 工具软件 > Moduel Proxy > 使用案例 >

Apache2.4.x下proxy_module、proxy_fcgi_module结合PHP-FPM解决内存不足问题

於英朗
2023-12-01

目录

一、背景

二、问题

三、解决方案


一、背景

最近对贫僧的个人页https://stackoperator.top进行升级,将Apache升级到2.4.46,PHP升级到7.4.15。升级后没啥问题,但是第二天却宕机了。经过查证是内存不足,导致mysql服务宕机了。那么是谁占用了这么多内存呢?

利用free、top等工具发现在页面刷新时,httpd进程的内存消耗会增加,而且是每点击、刷新一次页面就增加10+M的内存。如此严重的内存泄漏必须修理!!!谨以此文记录一下不太严谨的处理方式。

二、问题

因为是升级apache修改过httpd.conf文件,怀疑是apache配置改坏了。那么httpd.conf恢复到原始状态,再逐条修改配置,测试会不会消耗内存。最终确定是因为有一条配置,是要有最后一条,那么就会出现上面说的问题。

LoadModule php7_module lib64/httpd/modules/libphp7.so

已经周折(失败的解决方案),最终找到了Apache的FAQ页面 https://cwiki.apache.org/confluence/display/HTTPD/PHP-FPM,这个点进去看看全文基本就明白了。

问题是这样的——这个版本的apache httpd (2.4.x)引入了两个值得注意的特性:一个新的专门为fastCGI (mod_proxy_fcgi)设计的代理模块,以及使用MPM作为默认的apache进程管理器。与之前版本的worker MPM一样,当mod_php与非线程安全的第三方PHP扩展一起使用时,这个MPM的线程模型会导致问题。自apache 2.2发布以来,这一直是mod_php用户的祸根,实际上迫使他们拼凑fastcgi解决方案,或者使用更慢、内存消耗更大的prefork MPM。

This release of apache httpd has introduced two noteworthy features: a new proxy module specifically for fastCGI (mod_proxy_fcgi), and the move to the event MPM as the default apache process manager.

As with the worker MPM of the previous version, the threaded model of this MPM causes issues when mod_php is used with non-thread-safe third-party PHP extensions.

This has been a bane of mod_php users ever since apache 2.2 was released, practically forcing them to cobble together fastcgi solutions, or use the much slower and memory-hungry prefork MPM.

大概的意思是,在apache2.4.x版本不要直接LoadModule php7_module lib64/httpd/modules/libphp7.so。应该用 proxy_fcgi 和 php-fpm结合使用。 

三、解决方案

对于php部分,编译的时候使用 --enable-fpm 选项,启用php-fpm服务。这个服务默认监听127.0.0.1:9000。

php-fpm

Prerequisites:

  • installing software packages
  • editing configuration files
  • controlling service daemons.

Your distribution or OS will either include it in the stock PHP package, or make it available as an add-on package; you can build it from source by adding --enable-fpm to your ./configure options.

The default pool, [www], has this configured as listen 127.0.0.1:9000: it will only respond to requests on the local loopback network interface (localhost), on TCP port 9000.

对于apache2.4.x部分,启用 proxy_module proxy_fcgi_module 模块。注意这个/path/to/your/documentroot要改成你自己vhost的documentroot的绝对路径。

To work the magic with the PHP fastCGI process manager, we will be using a new module, mod_proxy_fcgi, which is intended specifically for communicating with (possibly external) fastCGI servers.

Make sure you include the proxy_fcgi module in your httpd.conf so we can use its features; since this requires the base proxy module, ensure both are loaded (uncommented):

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

TCP socket (IP and port) approach

Edit the configuration for a vhost of your choice, and add the following line to it:

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/path/to/your/documentroot/$1

DirectoryIndex /index.php index.php

然后重启apache服务,再刷新页面,内存在增长到一定程度后趋于稳定。不会再有严重的内存泄漏了。

 类似资料: