以下实验环境为CentOS6.6,32位平台!
1、解决依赖关系
编译安装nginx需要事先需要安装开发包组”Development Tools”和 “Development Libraries”。同时,还需要专门安装pcre-devel包:
<code> [root@www ~]# yum -y install pcre-devel </code>
2、安装
首先添加用户nginx,实现以之运行nginx服务进程:
<code> [root@www ~]# groupadd -r -g 108 nginx [root@www ~]# useradd -r -g 108 -u 108 nginx </code>
接着开始编译和安装:
<code> [root@www ~]# ./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre [root@www ~]# make && make install </code>
编译安装参数详解:
–prefix= 指定安装目录
–sbin-path 指定(执行)程序文件(nginx)
–conf-path= 指定配置文件(nginx.conf)
–error-log-path= 指定错误日志文件位置
–http-log-path= 指定access log路径
–user= 指定程序运行时的非特权用户
–group= 指定程序运行时的非特权用户组
–with-http_ssi_module 启用nginx的http_ssi_module支持
–with-http_flv_module 启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
–with-http_stub_status_module 启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
–with-http_gzip_static_module 启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
–http-client-body-temp-path= 指定http客户端请求临时文件路径
–http-proxy-temp-path= 指定http代理临时文件路径
–http-fastcgi-temp-path= 指定http fastcgi临时文件路径
–http-uwsgi-temp-path= 指定http uwsgi临时文件路径
–http-scgi-temp-path= 指定http scgi临时文件路径
–with-pcre 为了支持rewrite重写功能,必须制定pcre
说明:
1、Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用此功能需要事先安装gperftools,而后在编译nginx添加–with-google_perftools_module选项即可。
2、如果想使用nginx的perl模块,可以通过为configure脚本添加–with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。
3、为nginx提供SysV init脚本:
新建文件/etc/rc.d/init.d/nginx,内容如下:
<code> #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac </code>
而后为此脚本赋予执行权限:
<code> [root@www ~]# chmod +x /etc/rc.d/init.d/nginx </code>
添加至服务管理列表,并让其开机自动启动:
<code> [root@www ~]# chkconfig --add nginx [root@www ~]# chkconfig nginx on </code>
而后就可以启动服务并测试了:
<code> [root@www ~]# service nginx start </code>
查看其监听的端口:
<code> [root@mail ~]# netstat -tlnp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27984/nginx </code>
提示:在nginx下测试php连接数据库时,会出现错误,日志中的错误信息如下:
<code> 2015/07/30 00:12:43 [error] 32065#0: *46 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.0.147, server: www.feiyu.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.0.147" </code>
此时修改nginx配置文件作如下修改:
<code> [root@localhost nginx]# vim /etc/nginx/nginx.conf location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #注释此行 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #添加此行 include fastcgi_params; } </code>
让nginx重新加载配置文件,OK!
<code> [root@localhost nginx]# service nginx reload </code>