3、PHP网站搭建
PHP作为目前最受欢迎的Web服务器脚本语言,已经被全球80%的网站使用。Nginx的PHP网站搭建是Nginx与PHP-FPM组合实现的,由于Nginx不支持对PHP动态脚本程序的直接调用或解析,所有的动态脚本程序解析都是通过调用FastCGI接口服务器实现的。FastCGI是Web服务器和动态脚本程序间的一个高速、可伸缩的接口,它采用C/S架构,将Web服务器和动态脚本解析器分离,同时启动一个或多个脚本解析器守护进程接收Web服务器的动态脚本解析请求。当用户向Nginx服务器发起动态脚本请求时,Nginx服务器将动态脚本解析任务交由FastCGI进程来执行,并将FastCGI解析的结果返回给用户。PHP-FPM是一个被编译到PHP内核中的FastCGI应用,因为它是作为PHP的补丁开发的,所以它在PHP脚本的解析上更加高效。本节选择在CentOS 7环境下使用PHP 5.6版本来搭建PHP网站。
3.1、FastCGI模块指令
Nginx的FastCGI模块默认编译在Nginx的二进制文件中,无须单独编译。该模块配置指令如表所示。



- 除fastcgi_cache_path指令外,FastCGI模块指令均可编写在http、server、location指令域中。
- fastcgi_cache_purge指令仅商业版Nginx才支持。开源版可通过第三方模块或自己写脚本实现。
- fastcgi_cache_path指令只能编写在http指令域中。
- fastcgi_cache与fastcgi_store指令不能在同一指令域中同时使用。
- non_idempotent是指POST、LOCK、PATCH请求方法的处理。
fastcgi_cache_path指令值参数如表所示。

3.2、PHP环境安装
CentOS 7默认的PHP版本是5.3,可以使用Remi扩展源安装PHP 5.6和PHP-FPM。
yum install -y epel-release # 安装EPEL扩展源
# 安装Remi扩展源
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install -y --nogpgcheck --enablerepo=remi --enablerepo=remi-php56 \
php php-opcache php-devel php-mbstring php-mcrypt \
php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug \
php-pecl-xhprof php-gd php-ldap php-xml php-fpm \
php-pecl-imagick # 安装基于Remi扩展源的PHP 5.6
systemctl start php-fpm # 启动PHP-FPM服务
3.3、PHP网站配置样例
在Nginx的conf文件夹中创建文件fscgi.conf,用于编辑FastCGI的全局配置,配置内容如下:
# 缓冲区配置
fastcgi_buffering on; # 默认启用缓冲区
fastcgi_buffers 8 64k; # 若响应数据大小小于512KB,则会分配8个64KB缓冲区为其缓
# 冲;若大于512KB,则超出的部分会存储到临时文件中
fastcgi_buffer_size 64k; # 读取FastCGI服务器响应数据第一部分的缓冲区大小为64KB,
# 通常包含响应头信息
fastcgi_busy_buffers_size 128K; # 繁忙时向客户端发送响应的缓冲区大小为128KB
fastcgi_limit_rate 0; # 默认不做限制
fastcgi_max_temp_file_size 1024M; # 临时文件中大小为1024MB
fastcgi_temp_file_write_size 64k; # 每次写入临时文件的数据大小为64KB
# fastcgi_temp_path使用默认配置
# 请求处理
fastcgi_request_buffering on; # 默认启用读取整个请求体到缓冲区后再向FastCGI服务器发送请求
fastcgi_pass_request_body on; # 默认将客户端请求体传递给FastCGI服务器
fastcgi_pass_request_headers on; # 默认将客户端请求头传递给FastCGI服务器
# FastCGI连接配置
fastcgi_connect_timeout 60s; # 默认Nginx与FastCGI服务器建立连接的超时时间为60s
fastcgi_keep_conn on; # 启用保持连接
fastcgi_ignore_client_abort on; # 当客户端关闭连接时,同时关闭与FastCGI服务器的连接
fastcgi_read_timeout 60s; # 默认连续两个从FastCGI服务器接收数据的读操作之间的间隔
# 时间为60s
fastcgi_send_timeout 60s; # 默认连续两个发送到FastCGI服务器的写操作之间的间隔时间
# 为60s
fastcgi_socket_keepalive on; # FastCGI的连接启用so-keepalive socket选项
# 响应处理
fastcgi_force_ranges on ; # 强制启用byte-range请求支持
fastcgi_hide_header X-Powered-By; # 隐藏PHP版本字段
# fastcgi_pass_header无必须传递的特殊头字段属性
fastcgi_ignore_headers X-Accel-Redirect X-Accel-Expires \
X-Accel-Limit-Rate X-Accel-Buffering \
X-Accel-Charset Expires \
Cache-Control Set-Cookie Vary;
# 禁止Nginx处理从FastCGI获取响应的头属性字段
# 异常处理
fastcgi_intercept_errors on; # 在FastCGI响应数据中响应码大于等于300时重定向给Nginx
fastcgi_next_upstream error timeout invalid_header \
http_500 http_503 http_403 \
http_404 http_429; # 当出现指定的条件时,将用户请求传递给upstream
# 中的下一个服务器
fastcgi_next_upstream_timeout 0; # 不限制将用户请求传递给upstream中的下一个
# 服务器的超时时间
fastcgi_next_upstream_tries 0; # 不限制将用户请求传递给upstream中的下一个
# 服务器的尝试次数
Nginx PHP网站配置如下:
server {
listen 8080;
root /opt/nginx-web/phpweb;
index index.php; # 默认首页index.php
include fscgi.conf; # 引入FastCGI配置
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000; # FastCGI服务器地址及端口
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$; # 获取$fastcgi_path_info变量值
fastcgi_param PATH_INFO $fastcgi_path_info; # 赋值给参数PATH_INFO
include fastcgi.conf; # 引入默认参数文件
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
3.4、FastCGI集群负载及缓存
Nginx支持后端多个FastCGI服务器的负载均衡,负载均衡有两种方式:
- 一种是通过域名解析多个FastCGI服务器,该方式通过所有域名地址轮询(round-robin)的方式实现负载;
- 另一种是通过配置Nginx的upstream模块实现负载。
本节通过后一种方式实现负载均衡场景的搭建。Nginx的FastCGI模块支持对后端PHP解析数据的缓存,对于动态数据的缓存可以在实际应用场景中提升动态网站的访问速度。
安装PHP-FPM后,如果把PHP代码部署在与Nginx不同的服务器上,需要修改PHP-FPM服务器中的/etc/php-fpm.d/www.conf配置。
# PHP-FPM绑定本机所有IP
sed -i "s/^listen =.*/listen = 0.0.0.0:9000/g" /etc/php-fpm.d/www.conf
# 允许任何主机访问PHP-FPM服务
sed -i "s/^listen.allowed_clients/;listen.allowed_clients/g" /etc/php-fpm.d/www.conf
Nginx配置样例如下:
upstream fscgi_server {
ip_hash; # session会话保持
server 192.168.2.145:9000; # PHP-FPM服务器IP
server 192.168.2.159:9000; # PHP-FPM服务器IP
}
fastcgi_cache_path /usr/local/nginx/nginx-cache1
levels=1:2
keys_zone=fscgi_hdd1:100m
max_size=10g
use_temp_path=off
inactive=60m; # 设置缓存存储路径1,缓存的共享内存名称和大小
# 100MB,无效缓存的判断时间为1小时
fastcgi_cache_path /usr/local/nginx/nginx-cache2
levels=1:2
keys_zone=fscgi_hdd2:100m
max_size=10g
use_temp_path=off
inactive=60m; # 设置缓存存储路径2,缓存的共享内存名称和大小
# 100MB,无效缓存的判断时间为1小时
split_clients $request_uri $fscgi_cache {
50% "fscgi_hdd1"; # 50%请求的缓存存储在第一个磁盘上
50% "fscgi_hdd2"; # 50%请求的缓存存储在第二个磁盘上
}
server {
listen 8080;
root /opt/nginx-web/phpweb;
index index.php;
include fscgi.conf; # 引入默认配置文件
location ~ \.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) { # 静态资源文件过期时间
# 为12小时
expires 12h;
}
set $no_cache 0;
if ($query_string != "") { # URI无参数的数据不进行缓存
set $no_cache 1;
}
location ~ \.php(.*)$ {
root /opt/nginx-web/phpweb;
fastcgi_cache $fscgi_cache; # 启用fastcgi_cache_path设置的$fscgi_cache
# 的共享内存区域做缓存
fastcgi_cache_key ${request_method}://$host$request_uri; # 设置缓存的关键字
fastcgi_cache_lock on; # 启用缓存锁
fastcgi_cache_lock_age 5s; # 启用缓存锁时,添加缓存请求的处理时间为5s
fastcgi_cache_lock_timeout 5s; # 等待缓存锁超时时间为5s
fastcgi_cache_methods GET HEAD; # 默认对GET及HEAD方法的请求进行缓存
fastcgi_cache_min_uses 1; # 响应数据被请求一次就将被缓存
fastcgi_no_cache $no_cache; # $no_cache时对当前请求不进行缓存
fastcgi_cache_bypass $no_cache; # $no_cache时对当前请求不进行缓存
fastcgi_cache_use_stale error timeout invalid_header
updating http_500 http_503
http_403 http_404 http_429; # 当出现指定的条件时,使用
# 已经过期的缓存响应数据
fastcgi_cache_background_update on; # 允许使用过期的响应数据时,启用后台子请求用于
# 更新过期缓存,并将过期的缓存响应数据返回给客户端
fastcgi_cache_revalidate on; # 当缓存过期时,向后端服务器发起包含If-
# Modified-Since和If-None-Match HTTP消息
# 头字段的服务端校验
fastcgi_cache_valid 200 301 302 10h; # 200 301 302状态码的响应缓存10小时
fastcgi_cache_valid any 1m; # 其他状态码的响应缓存1分钟
add_header X-Cache-Status $upstream_cache_status; # 查看缓存命中状态
fastcgi_pass fscgi_server;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$; # 获取$fastcgi_path_info变量值
fastcgi_param PATH_INFO $fastcgi_path_info; # 赋值给参数PATH_INFO
include fastcgi.conf; # 引入默认参数文件
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
1万+



被折叠的 条评论
为什么被折叠?



