/ 中存储网

Nginx服务器和PHP服务详细配置笔记

2013-09-02 22:57:11 来源:kejihao
使用PHP5.5.x

使用PHP5.5.x可大幅度提高Webserver负载能力,详细看这里

Nginx配置 Nginx worker_processes

max_clients = worker_processes * worker_connections

worker_processes  ;

worker_connections  ;

worker_processes的数值一般设置为内核数

$ cat  "o">/proc "o">/cpuinfo  "o">|grep  "n">processor

processor        "o">:

processor        "o">:

processor        "o">:

processor        "o">:

processor        "o">:

processor        "o">:

processor        "o">:

processor        "o">:

根据查到的内核数,这里我们配置worker_processes为8

worker_processes 8;

隐藏 Nginx Server Tokens

server_tokens在Nginx配置文件的http/server/location部分设置。

Nginx Request / Upload Max Body Size (client_max_body_size)

client_max_body_size的默认值是1m,如果你的网站需要上传大于1m的文件,设置client_max_body_size为你需要的值。否则,在你上传超过client_max_body_size的文件时,Nginx会返回给你413错误,告诉你“Request Entity Too Large” 。

client_max_body_size在Nginx配置文件的http/server/location部分设置。

client_max_body_size m;

client_body_buffer_size k;

缓存静态文件

location  "o">~*  "p">.(jpg "o">|jpeg "o">|gif "o">|png "o">|css "o">|js "o">|ico "o">|xml "p">)$ {

access_log         "n">off;

log_not_found      "n">off;

expires           d;

}

启用gzip

gzip_proxied  "n">any;

gzip_types

text/ "n">css

text/ "n">plain

text/ "n">javascript

application "o">/javascript

application "o">/json

application "o">/x "o">-javascript

application "o">/xml

application "o">/xml "o">+rss

application "o">/xhtml "o">+xml

application "o">/x "o">-font "o">-ttf

application "o">/x "o">-font "o">-opentype

application "o">/vnd "p">.ms "o">-fontobject

image "o">/svg "o">+xml

image "o">/x "o">-icon

application "o">/rss "o">+xml

application "o">/atom_xml;

gzip_comp_level ;

gzip_http_version  "mf">1.0;

gzip_vary  "n">on;

gzip_buffers   "n">k;

gzip_min_length ;

把PHP请求交给PHP-FPM

location  "o">/ {

try_files  "err">$uri  "err">$uri "o">/ / "n">index. "n">php$ "n">uri? "err">$args;

}

location  "o">~*  "p">.php {

fastcgi_pass  "mf">127.0.0.1:;

fastcgi_index  "n">index. "n">php;

include  "n">fastcgi_params;

fastcgi_split_path_info  "o">^(. "o">+ "p">.php "p">)(/? "p">.* "p">)$;

fastcgi_param  "n">PATH_INFO $ "n">fastcgi_path_info;

fastcgi_param  "n">SCRIPT_FILENAME $ "n">document_root$ "n">fastcgi_script_name;

}

禁止访问隐藏文件

location  "o">~ / "err">. {

access_log  "n">off;

log_not_found  "n">off;

deny  "n">all;

}

配置PHP-FPM PHP-FPM 基本配置

emergency_restart_threshold

emergency_restart_interval m

process_control_timeout s

配置PHP-FPM Pool 进程

pm.max_children是PHP-FPM Pool 最大的子进程数,他数值取决于你的服务器内存。

假设你打算给1G内存给当前配置的PHP-FPM Pool,一般一个PHP请求占用内存10M-40M,我们按站点每个PHP请求占用内存25M,这样max_children = 1G/25M = 40.96。

这样,我设置pm.max_children为40. max_requests是每个子进程重生之前处理的请求数, 默认值为unlimited,设置为2000主要是为了避免内存泄露带来的问题

pm =  "n">dynamic

pm. "n">max_children =

pm. "n">start_servers =

pm. "n">min_spare_servers =

pm. "n">max_spare_servers =

pm. "n">max_requests =

PHP-FPM Pools Configuration

建议不同站点使用不同的PHP-FPM Pool,绑定在不同的端口。这样一个站点出问题,不至于影响其他的站点。同时,如果php-fpm进程占用CPU过高,也可以很快定位有问题的站点。

下边配置了两个PHP-FPM Pool,绑定了9000和9001两个http端口

/etc/php-fpm.d/site.conf

[site]

listen  "o">= 127.0.0.1:9000

user =  "s">site

group =  "s">site

request_slowlog_timeout  "o">= 5s

slowlog  "o">=  "s">/var/log/php-fpm/slowlog-site.log

listen.allowed_clients  "o">= 127.0.0.1

pm =  "s">dynamic

pm.max_children  "o">=

pm.start_servers  "o">=

pm.min_spare_servers  "o">=

pm.max_spare_servers  "o">=

pm.max_requests  "o">=

listen.backlog  "o">= -1

pm.status_path  "o">= /status

request_terminate_timeout  "o">= 120s

rlimit_files  "o">=

rlimit_core  "o">= unlimited

catch_workers_output  "o">= yes

env[HOSTNAME]  "o">= $HOSTNAME

env[TMP]  "o">= /tmp

env[TMPDIR]  "o">= /tmp

env[TEMP]  "o">= /tmp

/etc/php-fpm.d/blog.conf

[blog]

listen  "o">= 127.0.0.1:9001

user =  "s">blog

group =  "s">blog

request_slowlog_timeout  "o">= 5s

slowlog  "o">=  "s">/var/log/php-fpm/slowlog-blog.log

listen.allowed_clients  "o">= 127.0.0.1

pm =  "s">dynamic

pm.max_children  "o">=

pm.start_servers  "o">=

pm.min_spare_servers  "o">=

pm.max_spare_servers  "o">=

pm.max_requests  "o">=

listen.backlog  "o">= -1

pm.status_path  "o">= /status

request_terminate_timeout  "o">= 120s

rlimit_files  "o">=

rlimit_core  "o">= unlimited

catch_workers_output  "o">= yes

env[HOSTNAME]  "o">= $HOSTNAME

env[TMP]  "o">= /tmp

env[TMPDIR]  "o">= /tmp

env[TEMP]  "o">= /tmp

这段代码放在 /etc/php-fpm.conf 中,加载多个php-fpm pool

include=/ "n">etc/ "n">php- "n">fpm. "n">d/* "p">.conf

开启PHP-FPM状态信息页

php-fpm.conf配置文件要启用以下选项:

在Nginx配置的server部分增加以下配置:

location  "o">~ ^/ "p">(status "p">)$ {

access_log  "n">off;

include  "n">fastcgi_params;

fastcgi_pass  "mf">127.0.0.1:;

fastcgi_param  "n">SCRIPT_FILENAME $ "n">document_root$ "n">fastcgi_script_name;

}

访问http://localhost/status 查看php-fpm状态