Nginx 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服务
在 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;
}