现象
服务器已经根据Ubuntu 12.04下安装配置Worker工作模式的Apache 支持PHP设置成了Worker模式,但是当系统从Ubuntu 12.04升级到Ubuntu 14.04导致Apache2从2.2升级到2.4.10版本后,而MPM模块被还原为prefork模式,导致大量的Apache2进程被创建出来,时间稍微一长,系统出现大量的OOM记录,直到系统最后宕机。
Ubuntu 14.04下查看所有可用的MPM模块,命令如下:
|
1 2 3 4 5 6 7 |
$ ls /etc/apache2/mods-available/mpm* /etc/apache2/mods-available/mpm_event.conf /etc/apache2/mods-available/mpm_event.load /etc/apache2/mods-available/mpm_prefork.conf /etc/apache2/mods-available/mpm_prefork.load /etc/apache2/mods-available/mpm_worker.conf /etc/apache2/mods-available/mpm_worker.load |
查看当前正在使用的MPM模块,命令如下:
|
1 2 3 |
$ ls /etc/apache2/mods-enabled/mpm* /etc/apache2/mods-enabled/mpm_prefork.conf /etc/apache2/mods-enabled/mpm_prefork.load |
可以看到,目前正在使用的模块就是prefork模块。
使用apachectl -V | grep -i mpm可以更加清晰的打印出当前使用的模块
|
1 2 |
$ apachectl -V | grep -i mpm Server MPM: prefork |
注意,在Ubuntu 14.04下执行apache2 -l命令与Ubuntu 12.04下面的输出结果是不同的,Ubuntu 14.04下如果使用prefork模块输出的信息如下:
|
1 2 3 4 5 6 7 8 9 10 |
$ apache2 -l Compiled in modules: core.c mod_so.c mod_watchdog.c http_core.c mod_log_config.c mod_logio.c mod_version.c mod_unixd.c |
默认情况下Ubuntu 14.04中的PHP默认是没有线程安全支持,如果使用mpm_event支持的话,会提示如下信息:
|
1 2 |
Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP |
修复方式
1.重新安装系统升级过程中可能会被移除的PHP-FPM模块
|
1 |
$ sudo apt-get install libapache2-mod-fastcgi php5-fpm |
2.关闭Apache2内建的PHP支持,开启Apache2的FastCGI,PHP5-FPM支持
|
1 2 3 4 5 |
$ a2dismod php5 $ a2enmod actions fastcgi alias $ a2enconf php5-fpm |
3.修改PHP5-FPM的配置文件
|
1 |
$ sudo vim /etc/php5/fpm/pool.d/www.conf |
找到
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses on a ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. listen = 127.0.0.1:9000 ; Set listen(2) backlog. A value of '-1' means unlimited. ; Default Value: 128 (-1 on FreeBSD and OpenBSD) ;listen.backlog = -1 ; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. Many ; BSD-derived systems allow connections regardless of permissions. ; Default Values: user and group are set as the running user ; mode is set to 0666 ;listen.owner = www-data ;listen.group = www-data ;listen.mode = 0666 |
修改为:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses on a ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. ;listen = 127.0.0.1:9000 listen = /var/run/php5-fpm.sock ; Set listen(2) backlog. A value of '-1' means unlimited. ; Default Value: 128 (-1 on FreeBSD and OpenBSD) ;listen.backlog = -1 ; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. Many ; BSD-derived systems allow connections regardless of permissions. ; Default Values: user and group are set as the running user ; mode is set to 0666 listen.owner = www-data listen.group = www-data listen.mode = 0666 |
注意,上面修改了四处地方,listen,listen.owner,listen.group,listen.mode。
注意,如果提示如下错误:
|
1 2 |
[Fri May 09 09:13:06.149292 2014] [fastcgi:error] [pid 18537:tid 139690281211648] (13)Permission denied: [client 192.168.33.101:35036] FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed [Fri May 09 09:13:06.255308 2014] [fastcgi:error] [pid 18537:tid 139690281211648] [client 192.168.33.101:35036] FastCGI: incomplete headers (0 bytes) received from server "/usr/lib/cgi-bin/php5-fcgi" |
则说明listen.owner,listen.group,listen.mode这三行没有打开。
4.重启PHP5-FPM服务
|
1 |
$ sudo /etc/init.d/php5-fpm restart |
5.切换Apache2到Event-MPM模式
|
1 2 3 4 5 6 7 |
$ sudo a2dismod mpm_prefork $ sudo a2dismod mpm_worker $ sudo a2dismod mpm_itk $ sudo a2enmod mpm_event |
6.启用Apache2的cache,expire,gzip模块,加强服务器性能
|
1 2 3 4 5 |
$ sudo a2enmod cache $ sudo a2enmod expires $ sudo a2enmod deflate |
7.卸载libapache2-mod-php5,否则每次这个模块更新之后,都会导致apache2被自动切换到mpm_prefork模式
|
1 |
$ sudo apt-get remove libapache2-mod-php5 |
8.调整配置文件
Apache 2.4.10的配置文件如果按照Apache 2.2的配置文件的话,是没办法启用PHP-FPM的,Apache 2.4.10版本使用SetHandler的方式支持PHP-FPM是改动最少的一种方式了。
|
1 |
$ sudo vim /etc/apache2/sites-enabled/000-default.conf |
找到
|
1 2 3 4 5 6 7 8 9 10 |
<Directory /var/www/wordpress> #Options Indexes FollowSymLinks MultiViews Options FollowSymLinks MultiViews AllowOverride All FCGIWrapper /usr/bin/php5-cgi .php AddHandler fcgid-script .php Options ExecCGI SymLinksIfOwnerMatch Order allow,deny allow from all </Directory> |
调整为如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Directory /var/www/wordpress> #Options Indexes FollowSymLinks MultiViews Options FollowSymLinks MultiViews AllowOverride All #Apache 2.2/2.4.9 #FCGIWrapper /usr/bin/php5-cgi .php #AddHandler fcgid-script .php #Options ExecCGI SymLinksIfOwnerMatch #Apache 2.4.10 <FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost" </FilesMatch> Order allow,deny allow from all </Directory> |
同理,调整HTTPS的配置文件。
9.重启Apache2服务
|
1 |
$ sudo service apache2 restart |













