Centos7.2安装Nginx

前端之家收集整理的这篇文章主要介绍了Centos7.2安装Nginx前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的系统是Centos7.2 64位,首先yum update

编译安装Nginx

yum update

安装缺少的依赖包

yum -y install gcc gcc-c++ make automake autoconf libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

这里下载Nginxwen'd'ban,下载的文件放在 /usr/local
解压,cd进入解压后的文件夹,执行

./configure --prefix=/usr/local/Nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make -j 128 && make install

常用编译选项说明在 这里
关于yum安装与源码安装的不同,请见这里

启动/停止Nginx

/usr/local/Nginx/sbin/Nginx          #启动Nginx
/usr/local/Nginx/sbin/Nginx -s stop  #停止Nginx

启动 Nginx 服务后,可以通过 ps -aux | grep Nginx 查看进程

开机启动

注:参考自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:      /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/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:.*--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

猜你在找的CentOS相关文章