php多版本共存的配置(nginx+php5.2 & 5.5 & 5.6)

首先安装好不同版本的php

1
2
3
root@ubuntu:/usr/local# ls
autoconf-2.13 bin curl etc freetype games include lib man mysql nginx php php5.2 php5.6 sbin share src zend zend52
root@ubuntu:/usr/local#

配置
以防修改配置文件出错,可以先做好备份

1
vim etc/php-fpm.conf

需要注意下面几处配置

1
<value name="listen_address">127.0.0.1:9100</value>

这个表示php的fastcgi进程监听的ip地址以及端口。因为本地已经有一个5.6版本了所以这里改成9100 nobody

1
<value name="group">nobody</value>

表示php的fastcgi进程以什么用户以及用户组来运行,默认该行是注释掉的,需要打开

1
<value name="display_errors">0</value>

是否显示php错误信息

1
<value name="max_children">5</value>

最大的子进程数目
运行php-fpm: php-fpm用一个程序来控制fastcgi进程,这个文件在$PREFIX/sbin/php-fpm

php5.6版本的php-fpm.conf文件

成功运行php-fpm后
nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
server{
    listen 80;
    server_name php5.2.com;
    index index.html index.htm index.php;
    root /home/wwwroot/php5.2;

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9100;      #这里把fastcgi_pass 改到本地的9100端口
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        include fastcgi_params;
    }
}

启动

1
/usr/local/php5.2/sbin/php-fpm start

php5.3之后官方收录了php-fpm,所以之后的版本不用加start参数
再把启动命令写入/etc/rc.local文件中,开机启动。
到此实现以不同域名访问同一主机。

Nginx配置文件try_files语法及在WP Super Cache中的应用

try_files 是nginx0.6.36 后新增一个功能,用于搜索指定目录下的N个文件,如果找不到fileN,则调用fallback中指定的位置来处理请求。个人认为,作为nginx核心的内容,可以部分替代烦琐的rewrite功能,笔者把它用在wp super cache的rewrite重写中,也取得了不错的效果。

try_files
语法: try_files file1 [file2 … filen] fallback
默认值: 无
作用域: location

这个指令的作用就是可接收多个路径作为参数,当前一个路径的资源无法找到,则自动查找下一个路径,如当请求的静态资源不存在,就将请求fallback指定位置到后台服务器上进行动态处理。
简单的例子:

1
2
3
4
5
6
7
8
location / {
    try_files index.html index.htm @fallback;
}

location @fallback {
    root /var/www/error;
    index index.html;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
server {
    set $cache /wp-content/cache/supercache/$host;
        #wp-super-cache的路径
    listen 80;
    server_name _;
    location / {
        root /home/html/s0001/domains/$host;
        index index.php index.html;
                #直接调用gzip压缩后的html静态文件
        add_header Content-Type "text/html; charset=UTF-8";
        add_header Content-Encoding "gzip";
        try_files $cache/$uri/index.html.gz @proxy;
 
    }
    #所有静态文件都由nginx处理,并用gzip压缩输出
    location ~* \.(jpg|jpeg|png|gif|css|js|swf|mp3|avi|flv|xml|zip|rar)$ {
        expires 30d;
        gzip on;
        gzip_types  text/plain application/x-javascript text/css application/xml;
        root /home/html/s0001/domains/$host;
    }
    #找不到的文件都交给后端的apache处理了
    location @proxy {
        index  index.php index.htm index.html;
        root   /home/html/$user/domains/$host;
            proxy_pass   http://127.0.0.1:8080;
            proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Nginx常用的超时配置说明

1
client_header_timeout

语法 client_header_timeout time
默认值 60s
上下文 http server
说明 指定等待client发送一个请求头的超时时间(例如:GET / HTTP/1.1).仅当在一次read中,没有收到请求头,才会算成超时。如果在超时时间内,client没发送任何东西,nginx返回HTTP状态码408(“Request timed out”)

1
client_body_timeout

语法 client_body_timeout time
默认值 60s
上下文 http server location
说明 该指令设置请求体(request body)的读超时时间。仅当在一次readstep中,没有得到请求体,就会设为超时。超时后,nginx返回HTTP状态码408(“Request timed out”)

1
keepalive_timeout

语法 keepalive_timeout timeout [ header_timeout ]
默认值 75s
上下文 http server location
说明 第一个参数指定了与client的keep-alive连接超时时间。服务器将会在这个时间后关闭连接。可选的第二个参数指定了在响应头Keep-Alive: timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必要去关闭连接了。没有这个参数,nginx不会发送Keep-Alive响应头(尽管并不是由这个头来决定连接是否“keep-alive”)
两个参数的值可并不相同
注意不同浏览器怎么处理“keep-alive”头
MSIE和Opera忽略掉”Keep-Alive: timeout=” header.
MSIE保持连接大约60-65秒,然后发送TCP RST
Opera永久保持长连接
Mozilla keeps the connection alive for N plus about 1-10 seconds.
Konqueror保持长连接N秒

1
lingering_timeout

语法 lingering_timeout time
默认值 5s
上下文 http server location
说明 lingering_close生效后,在关闭连接前,会检测是否有用户发送的数据到达服务器,如果超过lingering_timeout时间后还没有数据可读,就直接关闭连接;否则,必须在读取完连接缓冲区上的数据并丢弃掉后才会关闭连接。

1
resolver_timeout

语法 resolver_timeout time
默认值 30s
上下文 http server location
说明 该指令设置DNS解析超时时间

1
proxy_connect_timeout

语法 proxy_connect_timeout time
默认值 60s
上下文 http server location
说明 该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒。
这个不是等待后端返回页面的时间,那是由proxy_read_timeout声明的。如果你的upstream服务器起来了,但是hanging住了(例如,没有足够的线程处理请求,所以把你的请求放到请求池里稍后处理),那么这个声明是没有用的,由于与upstream服务器的连接已经建立了。

1
proxy_read_timeout

语法 proxy_read_timeout time
默认值 60s
上下文 http server location
说明 该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。

1
proxy_send_timeout

语法 proxy_send_timeout time
默认值 60s
上下文 http server location
说明 这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接

1
proxy_upstream_fail_timeout(fail_timeout)

语法 server address [fail_timeout=30s]
默认值 10s
上下文 upstream
说明 Upstream模块下 server指令的参数,设置了某一个upstream后端失败了指定次数(max_fails)后,该后端不可操作的时间,默认为10秒

Ubuntu/Debian安装Nginx和upstream-fair

在ubuntu或debian上安装nginx,可以直接采用使用指令安装

1
apt-get install nginx

假定工作操作目录为用户根目录即~或者/home/uname(当前你是uname用户),如果你是root用户,那么你的根目录应该是/root

获取nginx及安装依赖包
使用官方源安装可能不能满足选择版本的需求,手动安装nginx首先安装依赖包
apt-get指令需要root权限,确保你使用root用户或者获得root权限

1
2
3
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libpcre3 libpcre3-dev libssl-dev libxslt-dev libgd2-xpm-dev libgeoip-dev

nginx源码可以访问nginx官方网站的下载页面:http://nginx.org/en/download.html
比如选择 Stable version nginx-1.2.7
复制下载地址,然后使用命令下载tar包

1
wget http://nginx.org/download/nginx-1.2.7.tar.gz

解压缩

1
tar zxvf nginx-1.2.7.tar.gz

upstream-fair模块
upstream-fair是比内建的负载均衡更加智能的负载均衡模块,一般google关键词nginx-upstream-fair可以找到相关资源和文章,这里不详述了。它采用的不是内建负载均衡使用的轮换的均衡算法,而是可以根据页面大小、加载时间长短智能的进行负载均衡。
可以通过github获取,地址是https://github.com/gnosek/nginx-upstream-fair
googlecc上也有http://wcoserver.googlecc.com/files/gnosek-nginx-upstream-fair-2131c73.tar.gz
注意:这个版本可能已经过时,推荐使用github上的master zip包
获取到nginx-upstream-fair-master.zip或者gnosek-nginx-upstream-fair-2131c73.tar.gz后解压缩

1
2
unzip nginx-upstream-fair-master.zip
tar zxvf gnosek-nginx-upstream-fair-2131c73.tar.gz

为了方便起见,把加压缩后得到文件夹更改一下名字,比如upstream

1
2
mv nginx-upstream-fair-master upstream
mv gnosek-nginx-upstream-fair-2131c73 upstream

如果你喜欢用rename也行,具体可以自己操作。

编译安装nginx
接下来进入解压缩之后的目录

1
cd nginx-1.2.7/

配置参数

1
./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_dav_module --with-http_flv_module --with-http_geoip_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-ipv6 --with-mail --with-mail_ssl_module --add-module=/home/uname/upstream/

注意最后一个参数–add-module=/home/uname/upstream/的路径,之前假定的工作目录为/home/uname(uname为你的用户根目录),我们下载的gnosek-nginx-upstream-fair压缩包在这里解压并且更名未upstream,此处填写绝对路径。
配置结束应该不会报错,我在ubuntu12.04和debian6上都做过安装,并且作为开发和生产环境一直在运行,如果有问题可以联系我。
接下来编译安装

1
2
sudo make
sudo make install

等待编译结束,安装完成
执行文件被安装在/etc/nginx/sbin/nginx,如果你想安装到/usr下可以更改前面的配置参数,这里可以这样在/usr/sbin中建立软链接

1
2
cd /usr/sbin
sudo ln -s /etc/nginx/sbin/nginx

然后你在任意位置执行

1
nginx -v

应该能看到nginx版本信息

1
nginx version: nginx/1.2.7

查看详细配置信息

1
nginx -V

显示详细信息

1
2
3
4
nginx version: nginx/1.2.7
built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_dav_module --with-http_flv_module --with-http_geoip_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-ipv6 --with-mail --with-mail_ssl_module --add-module=/home/uname/upstream/

附一个nginx使用upstream-fair的配置文件示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
upstream your-site {
    server 127.0.0.1:5550;
    server 127.0.0.1:5551;
    fair;
}
server {
    listen 80;
    server_name yoursite.com www.yoursite.com;
    access_log /var/log/nginx/access.log;
    location / {
        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;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 60s;
        proxy_send_timeout 90s;
        proxy_read_timeout 90s;
        proxy_buffering off;
        proxy_temp_file_write_size 64k;
        proxy_pass http://your-site;
        proxy_redirect off;
    }
}