阅读(3759)

Nginx + PHP(FastCGI)安装配置详解

最后一次修改 2018年02月01日

本文主要和大家分享Nginx + PHP(FastCGI)安装配置笔记,希望能帮助到大家。

软件环境:CentOS 5.7 + PHP 5.2.17 + Nginx 0.8.55

1. 编译安装 PHP 和 PHP-FPM

提示:

安装PHP前应先安装MySQL,具体方法请自行baidu或google;
PHP所需支持库的安装这里不做过多介绍,可上网搜索参照相关教程进行安装。

从http://php-fpm.org/downloads/下载与当前PHP版本对应的PHP-FPM源码包,例如:php-5.2.17-fpm-0.5.14.diff.gz
tar zxvf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17
./configure –prefix=/usr/local/php/ –with-config-file-path=/usr/local/php/etc –with-mysql=/usr/local/mysql/ –with-pdo-mysql=/usr/local/mysql/ –with-gd –with-zlib –with-curl –with-jpeg-dir –with-png-dir –with-libxml-dir –with-freetype-dir –with-iconv=/usr/local –with-mcrypt –enable-fastcgi –enable-fpm –enable-xml –enable-mbstring
make
make install
cp php.ini-dist /usr/local/php/etc/php.ini

注意:上面配置PHP编译参数中的–enable-fastcgi是必须开启的。

 

2. 创建 Nginx 使用的用户和用户组

groupadd www
useradd -g www www

 

3. 创建站点目录并设置权限

mkdir -p /htdocs/www
chmod +w /htdocs/www
chown -R www:www /htdocs/www

 

4. 安装 Nginx 所需的 PCRE 库

tar zxvf pcre-8.13.tar.gz
cd pcre-8.13
./configure
make
make install

 

5. 安装 Nginx

tar zxvf nginx-0.8.55.tar.gz
cd nginx-0.8.55
./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –user=www –group=www
make
make install

 

6. 配置 Nginx,创建虚拟主机

在 nginx.conf 文件的 http{…} 段中,增加一个 server{…} 段,例如下面的代码:

server {
listen 80;
server_name www.youdomain.com; #你的域名
root /htdocs/www;
access_log logs/www.access.log;
location / {
index index.html index.php;
}
error_page 500 502 503 504  /50x.html;
location = /50x.html {
root html;
}
location ~ .*\.php?$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
include        fastcgi.conf;
}
location ~ /nginx_info {
access_log off;
stub_status on;
}
}

注意:上面的配置中,增加了location ~ /nginx_info {…},这样就可以通过http://www.youdomain.com/nginx_info/来查询Nginx的状态信息了。

 

启动 PHP-FPM

/usr/local/php/sbin/php-fpm start

 

启动 Nginx

/usr/local/nginx/sbin/nginx