Ubuntu 12.04升级到Ubuntu 14.04导致Apache2从2.2升级到2.4.10版本PHP服务器的重新修正

现象


服务器已经根据Ubuntu 12.04下安装配置Worker工作模式的Apache 支持PHP设置成了Worker模式,但是当系统从Ubuntu 12.04升级到Ubuntu 14.04导致Apache22.2升级到2.4.10版本后,而MPM模块被还原为prefork模式,导致大量的Apache2进程被创建出来,时间稍微一长,系统出现大量的OOM记录,直到系统最后宕机。

Ubuntu 14.04下查看所有可用的MPM模块,命令如下:

$ 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模块,命令如下:

$ 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可以更加清晰的打印出当前使用的模块

$ apachectl -V | grep -i mpm
Server MPM:     prefork

注意,在Ubuntu 14.04下执行apache2 -l命令与Ubuntu 12.04下面的输出结果是不同的,Ubuntu 14.04下如果使用prefork模块输出的信息如下:

$ 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支持的话,会提示如下信息:

Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. 
You need to recompile PHP

修复方式


1.重新安装系统升级过程中可能会被移除的PHP-FPM模块

$ sudo apt-get install libapache2-mod-fastcgi php5-fpm

2.关闭Apache2内建的PHP支持,开启Apache2的FastCGI,PHP5-FPM支持

$ a2dismod php5

$ a2enmod actions fastcgi alias

$ a2enconf php5-fpm

3.修改PHP5-FPM的配置文件

$ sudo vim /etc/php5/fpm/pool.d/www.conf

找到

; 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

修改为:

; 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
注意,如果提示如下错误:

[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服务

$ sudo /etc/init.d/php5-fpm restart

5.切换Apache2Event-MPM模式

$ sudo a2dismod mpm_prefork

$ sudo a2dismod mpm_worker

$ sudo a2dismod mpm_itk

$ sudo a2enmod mpm_event

6.启用Apache2cache,expire,gzip模块,加强服务器性能

$ sudo a2enmod cache

$ sudo a2enmod expires

$ sudo a2enmod deflate

7.卸载libapache2-mod-php5,否则每次这个模块更新之后,都会导致apache2被自动切换到mpm_prefork模式

$ sudo apt-get remove libapache2-mod-php5

8.调整配置文件

Apache 2.4.10的配置文件如果按照Apache 2.2的配置文件的话,是没办法启用PHP-FPM的,Apache 2.4.10版本使用SetHandler的方式支持PHP-FPM是改动最少的一种方式了。

$ sudo vim /etc/apache2/sites-enabled/000-default.conf

找到

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

调整为如下:

<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服务

$ sudo service apache2 restart

参考链接


发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注