1、安装Nginx及相关组件
1.1 openssl安装
# 问题
md5-x86_64.s: Assembler messages: 
md5-x86_64.s:41: Error: 0xd76aa478 out range of signed 32bit displacement 
md5-x86_64.s:50: Error: 0xe8c7b756 out range of signed 32bit displacement 
md5-x86_64.s:68: Error: 0xc1bdceee out range of signed 32bit displacement 
md5-x86_64.s:77: Error: 0xf57c0faf out range of signed 32bit displacement 
md5-x86_64.s:95: Error: 0xa8304613 out range of signed 32bit displacement 
md5-x86_64.s:104: Error: 0xfd469501 out range of signed 32bit displacement 
md5-x86_64.s:122: Error: 0x8b44f7af out range of signed 32bit displacement 
md5-x86_64.s:131: Error: 0xffff5bb1 out range of signed 32bit displacement 
md5-x86_64.s:140: Error: 0x895cd7be out range of signed 32bit displacement 
md5-x86_64.s:158: Error: 0xfd987193 out range of signed 32bit displacement 
md5-x86_64.s:167: Error: 0xa679438e out range of signed 32bit displacement 
md5-x86_64.s:187: Error: 0xf61e2562 out range of signed 32bit displacement 
md5-x86_64.s:196: Error: 0xc040b340 out range of signed 32bit displacement 
md5-x86_64.s:214: Error: 0xe9b6c7aa out range of signed 32bit displacement 
md5-x86_64.s:223: Error: 0xd62f105d out range of signed 32bit displacement 
md5-x86_64.s:241: Error: 0xd8a1e681 out range of signed 32bit displacement 
md5-x86_64.s:250: Error: 0xe7d3fbc8 out range of signed 32bit displacement 
md5-x86_64.s:268: Error: 0xc33707d6 out range of signed 32bit displacement 
md5-x86_64.s:277: Error: 0xf4d50d87 out range of signed 32bit displacement 
md5-x86_64.s:295: Error: 0xa9e3e905 out range of signed 32bit displacement 
md5-x86_64.s:304: Error: 0xfcefa3f8 out range of signed 32bit displacement 
md5-x86_64.s:322: Error: 0x8d2a4c8a out range of signed 32bit displacement 
md5-x86_64.s:332: Error: 0xfffa3942 out range of signed 32bit displacement 
md5-x86_64.s:340: Error: 0x8771f681 out range of signed 32bit displacement 
md5-x86_64.s:356: Error: 0xfde5380c out range of signed 32bit displacement 
md5-x86_64.s:364: Error: 0xa4beea44 out range of signed 32bit displacement 
md5-x86_64.s:380: Error: 0xf6bb4b60 out range of signed 32bit displacement 
md5-x86_64.s:388: Error: 0xbebfbc70 out range of signed 32bit displacement 
md5-x86_64.s:404: Error: 0xeaa127fa out range of signed 32bit displacement 
md5-x86_64.s:412: Error: 0xd4ef3085 out range of signed 32bit displacement 
md5-x86_64.s:428: Error: 0xd9d4d039 out range of signed 32bit displacement 
md5-x86_64.s:436: Error: 0xe6db99e5 out range of signed 32bit displacement 
md5-x86_64.s:452: Error: 0xc4ac5665 out range of signed 32bit displacement 
md5-x86_64.s:463: Error: 0xf4292244 out range of signed 32bit displacement 
md5-x86_64.s:481: Error: 0xab9423a7 out range of signed 32bit displacement 
md5-x86_64.s:490: Error: 0xfc93a039 out range of signed 32bit displacement 
md5-x86_64.s:508: Error: 0x8f0ccc92 out range of signed 32bit displacement 
md5-x86_64.s:517: Error: 0xffeff47d out range of signed 32bit displacement 
md5-x86_64.s:526: Error: 0x85845dd1 out range of signed 32bit displacement 
md5-x86_64.s:544: Error: 0xfe2ce6e0 out range of signed 32bit displacement 
md5-x86_64.s:553: Error: 0xa3014314 out range of signed 32bit displacement 
md5-x86_64.s:571: Error: 0xf7537e82 out range of signed 32bit displacement 
md5-x86_64.s:580: Error: 0xbd3af235 out range of signed 32bit displacement 
md5-x86_64.s:598: Error: 0xeb86d391 out range of signed 32bit displacement 
make[2]: *** [md5-x86_64.o] 错误 1 
make[2]: Leaving directory `/home/ncoadmin/openssl-0.9.8j/crypto/md5' 
make[1]: *** [subdirs] 错误 1 
make[1]: Leaving directory `/home/ncoadmin/openssl-0.9.8j/crypto' 
make: *** [build_crypto] 错误 1 

# 下载更高的版本可以解决以上问题,openssl-1.0.1a.tar.gz

1.2 pcre安装
# 问题:make[1]: *** [/usr/local/pcre//Makefile] Error 127

configure 配置通过
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre/  --with-openssl=/usr/local/openssl/ --with-http_ssl_module --with-http_flv_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/ --user=nginx  --group=nginx
make 错误
[root@nginx nginx-1.0.5]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx/nginx-1.0.5'
cd /usr/local/pcre/ \
    && if [ -f Makefile ]; then make distclean; fi \
    && CC="gcc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
    ./configure --disable-shared
/bin/sh: line 2: ./configure: No such file or directory
make[1]: *** [/usr/local/pcre//Makefile] Error 127
make[1]: Leaving directory `/root/nginx/nginx-1.0.5'
make: *** [build] Error 2

# 分析
[root@nginx nginx-1.0.5]# ./configure --help | grep '\--with-pcre'
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional options for PCRE building
source ????????
# 解决
[root@nginx nginx-1.0.5]# ./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.12/  --with-openssl=/usr/local/openssl/ --with-http_ssl_module --with-http_flv_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/ --user=nginx  --group=nginx
[root@nginx nginx-1.0.5]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx/nginx-1.0.5'
cd ../pcre-8.12/ \
&& if [ -f Makefile ]; then make distclean; fi \
&& CC="gcc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \ 
./configure --disable-shared
checking for a BSD-compatible install... /usr/bin/install -c 
checking whether build environment is sane... make[1]: *** [../pcre-8.12//Makefile]
 
# ok 总结:--with-pcre=DIR 是设置源码目录,而不是编译安装后的目录。

1.3 zlib安装
2 nginx安装
2.1 首先添加用户nginx,实现以之运行nginx服务进程:
# groupadd -r nginx
# useradd -r -g nginx nginx

2.2 接着开始编译和安装:
# ./configure \
  --prefix=/usr/local/nginx \
  --error-log-path=/data/applogs/nginx/error.log \
  --http-log-path=/data/applogs/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=/usr/local/nginx/client/ \
  --http-proxy-temp-path=/usr/local/nginx/proxy/ \
  --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ \
  --http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
  --http-scgi-temp-path=/usr/local/nginx/scgi \
  --with-pcre \
  --with-openssl=openssl源码目录 \
  --withpcre=pcre源码目录 \
  --withzlib=zlib源码目录

# make && make install

# 检查是否安装成功
#  /usr/local/nginx/sbin/nginx -t
#   结果显示:
#   nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
#   nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    
# 说明:
# 1、Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用此功能需要事先安装gperftools,而后在编译nginx添加--with-google_perftools_module选项即可。
# 2、如果想使用nginx的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。

2.3、为nginx提供SysV init脚本:

新建文件/etc/rc.d/init.d/nginx,内容如下:

#!/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/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

而后为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/nginx

添加至服务管理列表,并让其开机自动启动:
# chkconfig --add nginx
# chkconfig nginx on

而后就可以启动服务并测试了:
# service nginx start


# 碰到问题
# 参考资料 /etc/init.d/functions详解 
#  链接 https://www.linuxidc.com/Linux/2017-09/147065.htm
# https://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html

. /etc/rc.d/init.d/functions 

NCNF4:~ # killproc nginx 
varpid=nginx 
1 pidof -c -m -o 44741 -o 44731 -o %PPID -x nginx 
2 pidof -c -m -o 44741 -o 44731 -o %PPID -x nginx 
pidof: invalid options on command line! 
pidof: invalid options on command line! 

#原因:pidof 没有参数 -c -m
#解决:修改__pids_pidof 函数
__pids_pidof() { 
        # pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \ 
        #       pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}" 
        pidof -o $$ -o $PPID -o %PPID -x "$1" || \ 
                pidof -o $$ -o $PPID -o %PPID -x "${1##*/}" 
}