nginx+php-fpm在Linux下的安装和配置

一直想做web服务方面的事儿,昨天和哥们交流,他推荐我用一下nginx。从本周起,开始nginx之旅,呼呼~~~

      Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

最新稳定的源代码:下载地址

我下载的是最新的稳定版本nginx-1.4.2。

解压后,执行./configure 出现错误:error: the HTTP rewrite module requires the PCRE library.

需要下载PCRE库,进行编译安装,下载地址:pcre库 ,最好下载其最新的版本,解压进入目录pcre-8.21,逐步执行./configure       make       make install,就可以解决上面出现的问题。

然后再次进入nginx源代码目录,逐步执行./configure        make          make install

在配置完成后,终端会显示如下一段代码:

nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
我们可以发现,nginx安装到了/usr/local/nginx目录下。

下面就要对其进行配置,由上面代码的提示我们进入/usr/local/nginx/conf目录下,编辑nginx.conf文件。对配置文件的解释可以参照这里:nginx配置文件

启动Nginx,终端输入/usrlocal/nginx/sbin/nginx

然后在浏览器输入127.0.0.1,出现如下的情况时,则说明配置成功

Welcome to nginx!

下面安装php-fpm

nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx。

终端下分别运行如下命令:

wget http://cn2.php.net/distributions/php-5.4.7.tar.gz
tar zvxf php-5.4.7.tar.gz
cd php-5.4.7
./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt \
--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \
--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
 
make all 
make install
在安装后内容放在/usr/local/php目录下。

对php-fpm运行用户进行设置

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

修改
user = Server-nginx
group = Server-nginx

运行php-fpm

打开Nginx的配置文件,使其支持php

找到这个位置,并且修改成如下的格式:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
保存退出,使用命令 /usr/nginx/sbin/nginx -s reload 重新启动Nginx

然后可以自己写一个PHP文件测试一下

重启php-fpm可采用如下的方法:

ps aux | grep -c php-fpm找出是master的主进程ID

kill -INT  ID

再次启动php-fpm。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值