一、安装 pcre pcrepcre 为了支持rewrite功能,我们需要安装pcre
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
yum install pcre*
yum install openssl*


二、下载安装nginx

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

本人安装的是1.12.2版本,自行百度下载
useradd -s /sbin/nologin -M nginx

mkdir /usr/local/nginx
chown -R nginx:nginx /usr/local/nginx
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module --with-http_v2_module \
--with-http_stub_status_module --with-pcre
#参数说明:-----------------------------------
#–with-http_stub_status_module:支持nginx状态查询
#–with-http_ssl_module:支持https
#–with-http_spdy_module:(已改为:with-http_v2_module)支持google的spdy,想了解请百度spdy,这个必须有ssl的支持
#–with-pcre:为了支持rewrite重写功能,必须制定pcre

make
make install

三、命令
/usr/local/nginx/sbin/nginx    #启动 NGINX
/usr/local/nginx/sbin/nginx -s stop    #关闭nginx
/usr/local/nginx/sbin/nginx -s reload   #当你有修改配置文件的时候,只需要reload以下即可

echo 'export PATH=/usr/local/nginx/sbin:$PATH'>>/etc/profile
source /etc/profile


四、加入开机自启项
vim /etc/init.d/nginx    #见下文

自定义编译安装的nginx,需要根据您的安装路径修改下面这两项配置:
nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路径。

nginx官方脚本地址:http://wiki.nginx.org/RedHatNginxInitScript
保存脚本文件后设置文件的执行权限:X

chkconfig nginx on


-----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/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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
#-----------------------------------------------------

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄