插件窝 干货文章 centos6.4下nginx1.12.1安装的方法

centos6.4下nginx1.12.1安装的方法

Nginx 安装 status bash 214    来源:    2025-04-26

CentOS 6.4 下安装 Nginx 1.12.1 的方法

在 CentOS 6.4 上安装 Nginx 1.12.1 可以通过以下几种方法实现:

方法一:从 Nginx 官方源安装(推荐)

  1. 添加 Nginx 官方源

    sudo rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    
  2. 安装 Nginx 1.12.1

    sudo yum install nginx-1.12.1
    
  3. 启动 Nginx 服务

    sudo service nginx start
    
  4. 设置开机启动

    sudo chkconfig nginx on
    

方法二:从源码编译安装

  1. 安装依赖

    sudo yum groupinstall "Development Tools"
    sudo yum install pcre-devel zlib-devel openssl-devel
    
  2. 下载 Nginx 1.12.1 源码

    wget http://nginx.org/download/nginx-1.12.1.tar.gz
    tar zxvf nginx-1.12.1.tar.gz
    cd nginx-1.12.1
    
  3. 配置编译选项

    ./configure --prefix=/usr/local/nginx \
               --with-http_ssl_module \
               --with-http_stub_status_module \
               --with-pcre
    
  4. 编译并安装

    make
    sudo make install
    
  5. 创建启动脚本: 创建 /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:      /usr/local/nginx/conf/nginx.conf
    # pidfile:     /usr/local/nginx/logs/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:" | 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
    
  6. 设置权限并添加服务

    sudo chmod +x /etc/init.d/nginx
    sudo chkconfig --add nginx
    sudo chkconfig nginx on
    
  7. 启动 Nginx

    sudo service nginx start
    

方法三:使用 EPEL 源安装(版本可能较旧)

  1. 安装 EPEL 源

    sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
    
  2. 安装 Nginx

    sudo yum install nginx
    

    注意:EPEL 源中的 Nginx 版本可能不是 1.12.1

验证安装

无论使用哪种方法安装,都可以通过以下命令验证:

nginx -v

应该会显示:

nginx version: nginx/1.12.1

注意事项

  1. CentOS 6.4 已经比较老旧,建议升级到更新的版本以获得更好的安全性和功能支持
  2. 如果遇到依赖问题,可能需要先安装或更新相关依赖包
  3. 防火墙可能需要配置以允许 HTTP(80) 和 HTTPS(443) 端口访问