导航
导航
文章目录
  1. 一、FastCGI
  2. 二、原理
  3. 三、配置

Configure PHP behind Nginx with FastCGI

一、FastCGI

FastCGI (Common Gateway Interface), 是 Web Server 与 动态脚本 script 之间的通信接口。更多有关 FastCGI 的信息 PHP manual.

二、原理

Web Server, 如 Nginx、Apache,本身不会对外部 script 直接调用或者解析,所有的 script(如 PHP)必须通过 FastCGI 接口来调用。FastCGI 接口通过 socket 或者 daemon(绑定一个特定的端口 9000)与 Web Server 交互。


三、配置

1.下载最新版的 php
http://php.net/downloads.php

1
wget -c http://am1.php.net/distributions/php-7.0.6.tar.gz

2.编译

由于 php7,已经彻底放弃了–with-myql 选项,改用 mysqlnd,如果直接用此选择,而不选择 mysqli 和 pdo,会导致连接mysql出错,因为 php script 找不到连接mysql 的驱动。添加 –enable-fpm 选项,使能 FastCGI.

1
2
3
4
5
6
7
8
./configure --prefix=/opt/php7 \
--enable-fpm \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysql-sock=/var/run/mysqld/mysqld.sock \
--enable-mbstring \
--enable-sockets

3. 更改配置文件

采用 socket 方式连接 webserver

1
vim /opt/php7/etc/php-fpm.d/www.conf

修改以下参数

1
2
3
4
5
6
user = coolstar-pc //与nginx配置的user相同
group = coolstar-pc
;listen = 127.0.0.1:9000
listen = /opt/php7/var/run/php7-fpm.sock
listen.owner = coolstar-pc
listen.group = coolstar-pc

copy 启动脚本到 /etc/init.d

1
2
3
cp ~/php-7.0.6/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 777 /etc/init.d/php-fpm
service php-fpm restart

4.编辑 nginx 站点配置文件,添加对 Fast CGI 的支持

add 下面一行数据到站点 配置文件

1
fastcgi_pass unix:/opt/php7/var/run/php7-fpm.sock;

 

支持一下
坚持原创技术分享,您的支持将鼓励我继续创作!