/ 中存储网

Debian系统配置LANMP(Debian+Apache+Nginx+MySQL+PHP)过程记录

2014-03-13 11:38:01 来源:IT技术网
Web服务器的LAMP、LLMP、LNMP几种结构应用都已经比较广泛了。特别是LLMP、LNMP在VPS上很受欢迎,原因无非是Lighttpd、Nginx处理静态文件非常给力,用FastCGI方式解析PHP效率也很高。前几天在讨论LANMP与LLLMP相比的优势时,得知Apache的mod_php可以让Apache直接解析PHP(即Apache Handler),与FastCGI方式相同的是,Apache进程也是常驻内存,而Apache在高并发时对队列的处理比FastCGI更成熟,Apache的mod_php效率比php-cgi更高且更稳定。比如下面的代码由php-cgi解析时会产生内存泄漏,而mod_php则没有这个问题。

<?php

function whatthefuck($cons) {

  $i = 0;

  $object = new StdClass;

  for (;$i < $cons; $i++) {

$object->{"your_" . $i} = array("sorry" => 12345);

$object->{"yourdaddy_" . $i} = new StdClass;

$object->{"onlyyour"} = array("sorry" => 12345);

  }

  unset($object);

}

whatthefuck(10000);

print "done";

?>

所以我考虑从LNMP出发,然后让Apache代替php-cgi做后端解析PHP,形成LANMP结构。额外的好处是,Apache跑Ruby on Rails比Lighttpd或Nginx都简单许多。这篇笔记记录在Debian squeeze环境下配置LANMP的要点。

安装Apache

sudo apt-get install apache2 libapache2-mod-php5 libapache2-mod-rpaf

mod_rpaf是为了让Apache在被前端Nginx代理的情况下也能获取访客的真实IP。apache2和libapache2-mod-php5这两个包建议一起安装。apache2默认安装apache2-mpm-worker,这是Apache的Multi-Processing Module之一,此种方式效率更高,但配合mod_php时会有安全隐患,所以Debian的libapache2-mod-php5强制依赖apache2-mpm-prefork,会替换掉apache2-mpm-worker。自己编译的话没有这个限制。

安装nginx

Debian squeeze的nginx是0.7.67版本的,这个版本有点老,从测试结果来看效率确实不如sid的0.8.54版本(现在sid为1.0.4版本)。所以首先增加一个sid的源。为了不让sid大规模污染squeeze环境,在/etc/apt/preferences.d/新建priority文件并且写入以下Pin-Priority:

Package: *

Pin: release v=6.*

Pin-Priority: 900

Package: *

Pin: release o=Debian

Pin-Priority: -1

这段Pin-Priority的意义是默认只选用squeeze的包,当且仅当用apt-get -t sid指定release时才使用sid的包,且dist-upgrade时sid包不升级,避免升级时引入新的sid包。

然后,

sudo apt-get -t sid install nginx-light

sid中有两个nginx包,nginx-full和nginx-light,nginx-full包含的模块多一些,对比后我觉得nginx-light可以满足需求。如果要使用Limit Requests或Memcached的话,应该安装nginx-full。

安装MySQL和PHP

跟LAMP、LLMP和LNMP的区别是,不需要php5-cgi,php的配置文件在/etc/php5/apache2/。

配置Apache

Apache在LANMP里的意义相当于LNMP里的php-fam、LLMP里的spawn-fcgi,以及php-cgi,首先要调整下Apache进程的数量,在/etc/apache/apache2.conf找到下面一段:

# prefork MPM

# StartServers: number of server processes to start

# MinSpareServers: minimum number of server processes which are kept spare

# MaxSpareServers: maximum number of server processes which are kept spare

# MaxClients: maximum number of server processes allowed to start

# MaxRequestsPerChild: maximum number of requests a server process serves

<IfModule mpm_prefork_module>

StartServers         14

MinSpareServers      14

MaxSpareServers      14

MaxClients           14

MaxRequestsPerChild  10000

</IfModule>

几个参数的意义在注释里都有了,改成上面这样会保证Apache固定开14个进程。Apache解析PHP时占用的内存跟php-cgi差不多,所以原来开多少php-cgi,现在开多少Apache就行了。

Timeout值也可以改一下,比如60,后面配置Nginx时会用到这个值。

再给Apache指定一个非80端口,比如81,在ports.conf里面改成:

NameVirtualHost *:81

Listen 127.0.0.1:81

监听127.0.0.1就够了,因为Apache是要由Nginx代理的,不对外服务。

虚拟主机需要在Apache上配好,与没有Nginx前端时一样,SSL不需要在Apache上配置。

最后把Apache精简一下,只当作后端用,没必要加载很多模块,保留

deflate dir mime php5 reqtimeout rewrite rpaf setenvif

足够了。

配置Nginx

Nginx采用反向代理的方式接收Apache的处理结果,需要先写一些代理的参数,可以将下面的配置保存为proxy_params,以后每个用到proxy_pass的地方都include一下。

proxy_redirect          http:// $scheme://;

proxy_set_header        Host $host;

proxy_set_header        X-Real-IP $remote_addr;

proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header        X-Forwarded-Proto $scheme;

proxy_connect_timeout   30;

proxy_send_timeout      30;

proxy_read_timeout      60;

proxy_buffers           8 128k;

其中proxy_read_timeout应保持与Apache的TimeOut一致,proxy_buffers里的128k可以改为PHP生成的网页的大小的最大值。

然后可以配置虚拟主机了,以前的location ~ .*.(php|php5)?$里的fastcgi_pass之类,现在应该改写成proxy_pass:

# proxy the PHP scripts to Apache listening on 127.0.0.1:81

location ~ .php$ {

proxy_pass http://127.0.0.1:81;

include proxy_params;

}

跟LNMP相比,rewrite规则更麻烦了,因为Nginx的rewrite和Apache的rewrite是串行处理的,即Nginx先rewrite一下,如果处理后的请求被交给Apache,那么Apache又会再根据自己的rewrite rule再处理;相反Apache的rewrite结果不会再交回Nginx处理。所以,避免麻烦的方法是,只让Nginx做rewrite,因为Nginx的rewrite效率比Apache的高,而且rewrite的结果有可能是静态文件。特殊的情况是,比如WordPress的伪静态,在Nginx里怎么写都不成,因为index.php不接受query string。又因为proxy_pass和fastcgi_pass对URI的处理不同,所以LNMP一键安装包的WordPress rewrite规则:

if (!-f $request_filename){

rewrite (.*) /index.php;

}

以及所有的类似规则,是不行的(不得不说他写的这个规则很丑),这样rewrite,Apache收到的URI只有"/index.php",永远返回首页。解决方法是,把rewrite交给Apache(嫌Nginx的rewrite麻烦的也可以这么做):

try_files $uri $uri/ @apache;

location @apache {

proxy_pass http://127.0.0.1:81;

include proxy_params;

}

女神工作室给出过一条rewrite规则是:

if (!-e $request_filename) {

proxy_pass http://127.0.0.1:81;

}

我实验这样写不能通过,因为Nginx默认不允许在if里面写proxy_pass,if是rewrite语句之一,不能与其它的语句混用(但是编译时可以加某个参数,if就变成真的“条件语句”了),所以我用了try_files来变通。

SSL需要在Nginx上配置。

Tips

配置Apache的mod_deflate时别忘了把text/javascript也加进去,有些PHP生成的js指定的mime type是text/javascript而不是application/x-javascript和application/javascript。Nginx不需要改,静态的js一定会是application/x-javascript。

Apache跑Ruby on Rails需要mod_passenger,配置只要

DocumentRoot xxx

RailsEnv production

RailsBaseURI /

就行了。

nginx的worker process跟CPU核数一样多比较好。

ipv6only=on只在listen [::]:80 default_server的地方写。

Nginx上用的SSL证书需要把certificate、chain和CA都装在一个pem文件里,private key也可以装进去,然后ssl_certificate和ssl_certificate_key指定同一个文件就行了。