CentOS 安装 Nginx 与 PHP(FastCGI)

 

安装 Nginx

 

Nginx 安装比较简单,可以到这里查看:

 

http://nginx.org/en/docs/install.html

https://www.nginx.com/resources/wiki/start/topics/tutorials/install/

 

 

安装 PHP

 

1.下载 PHP

 

到官方网站下载 PHP。网址 http://php-fpm.org/downloads/

 

2.解压 PHP

 

tar zxf php-x.x.x

 

 

 

3.编译 PHP (这里只简单设置支持 PHP-FPM 和 MySQL ),高级命令查看后文

 

 

cd ../php-x.x.x

./configure --enable-fpm --with-mysql 

make 

make install


4.复制配置文件到正确位置

cp php.ini-development /usr/local/php/php.ini 

cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf 

cp sapi/fpm/php-fpm /usr/local/bin

 

5. 如果访问 Nginx 的文件不存在时,要阻止请求传递到 PHP-FPM 后端,防止任意脚本注入。

 

我们可以通过设置我们的php.ini文件中的 cgi.fix_pathinfo 语句为 0 来解决这个问题。

 

查找 php.ini:

vim /usr/local/php/php.ini

找到 cgi.fix_pathinfo 语句:

cgi.fix_pathinfo=0

在启动服务之前,php-fpm.conf 必须修改指定 PHP-FPM 服务必须作为用户 www-data 和 组 www-data 运行 :

vim /usr/local/etc/php-fpm.conf

找到下面的代码,修改成下面这样:

; Unix user/group of processes

; Note: The user is mandatory. If the group is not set, the default user's group ; will be used.

user = www-data 

group = www-data

现在就能够启动 php-fpm 服务了:

/usr/local/bin/php-fpm

Nginx 配置支持PHP应用程序:

vim /usr/local/nginx/conf/nginx.conf

找到类似以下代码,修改如下,配置支持 .php 文件:

location / {

 root html;

 index index.php index.html index.htm;

}

下一步骤是确保 PHP 文件传递到 PHP-FPM 后端. 找到关于 PHP location 指令,输入以下内容:

location ~* \.php$ { 

 fastcgi_index index.php; 

 fastcgi_pass 127.0.0.1:9000; 

 include fastcgi_params; 

 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 

 fastcgi_param SCRIPT_NAME $fastcgi_script_name; 

}

重启 Nginx.

sudo /usr/local/nginx/sbin/nginx -s stop 

sudo /usr/local/nginx/sbin/nginx

创建测试文件

rm /usr/local/nginx/html/index.html

echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php

在浏览器中打开 http://localhost 。 phpinfo() 就能够显示了。

编译 PHP 高级命令

./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt --enable-mbstring --with-curl --enable-inline-optimization --with-bz2  --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli --with-gd --with-jpeg-dir --with-openssl --enable-xml --with-png-dir --disable-rpath --with-freetype-di

上一篇:Nginx 与 PHP-FPM 运行原理

原文链接:http://woqilin.blogspot.com/2016/07/centos-nginx-phpfastcgi.html

猜你喜欢

转载自wangzq-phper.iteye.com/blog/2314035