一直想做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。