Nginx 编译安装


1、源码选定目录

默认:/data/software/

2、安装openssl、pcre、zlib库

openssl库:https://www.openssl.org/source/openssl-1.0.2j.tar.gz
pcre库:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
zlib库:http://zlib.net/zlib-1.2.8.tar.gz
#yum -y install openssl-devel zlib-deve

#cd /data/software
#tar -xvf pcre-8.39.tar.gz
#cd pcre-8.39
#./configure --enable-shared && make && make install
#echo "/usr/local/lib" >> /etc/ld.so.conf
#ldconfig -V
/usr/local/lib:
    libpcre.so.1 -> libpcre.so.1.2.7
    libpcreposix.so.0 -> libpcreposix.so.0.0.4
    libpcrecpp.so.0 -> libpcrecpp.so.0.0.1

3、安装Nginx

#useradd -M -s /sbin/nologin website

#cd /data/software
#tar -xvf nginx-1.8.1.tar.gz
#tar -xvf zlib-1.2.8.tar.gz
#tar -xvf openssl-1.0.2j.tar.gz

#cd nginx-1.8.1
#./configure --prefix=/data/apps/nginx --conf-path=/data/apps/nginx/conf/nginx.conf \
--error-log-path=/data/logs/nginxlogs/error.log \
--http-log-path=/data/logs/nginxlogs/access.log \
--pid-path=/data/apps/nginx/run/nginx.pid \
--user=website \
--group=website \
--with-http_ssl_module \
--with-http_spdy_module \
--with-http_addition_module \
--with-http_sub_module \
--with-pcre=/data/software/pcre-8.39 \
--with-zlib=/data/software/zlib-1.2.8 \
--with-openssl=/data/software/openssl-1.0.2j/ \
--http-client-body-temp-path=/data/apps/nginx/temp/client_body_temp \
--http-proxy-temp-path=/data/apps/nginx/temp/proxy_temp \
--http-fastcgi-temp-path=/data/apps/nginx/temp/fastcgi_temp \
--http-uwsgi-temp-path=/data/apps/nginx/temp/uwsgi_temp \
--http-scgi-temp-path=/data/apps/nginx/temp/scgi_temp
#make & make install

4、nginx自启动脚本

#vim /etc/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:      /data/apps/nginx/conf/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="/data/apps/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/data/apps/nginx/conf/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' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   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/init.d/nginx

5、启动验证、加开机自启动

#chkconfig nginx on

#service nginx start
Starting nginx:                                            [  OK  ]

#curl -I http://127.0.0.1/
HTTP/1.1 403 Forbidden
Server: nginx 1.8.8
Date: Fri, 25 Nov 2016 09:48:08 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 162
Connection: keep-alive