插件窝 干货文章 centos6.5下怎么安装Nginx

centos6.5下怎么安装Nginx

Nginx 安装 bash status 585    来源:    2025-04-25

CentOS 6.5 下安装 Nginx 的详细指南

CentOS 6.5 是一个较旧的系统版本,安装 Nginx 需要一些额外的步骤。以下是完整的安装方法:

方法一:使用 EPEL 仓库安装

  1. 添加 EPEL 仓库

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

    yum install nginx -y
    
  3. 启动 Nginx 并设置开机启动

    service nginx start
    chkconfig nginx on
    

方法二:从 Nginx 官方仓库安装(推荐)

  1. 创建 Nginx 官方仓库文件

    vi /etc/yum.repos.d/nginx.repo
    
  2. 添加以下内容

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/6/$basearch/
    gpgcheck=0
    enabled=1
    
  3. 安装 Nginx

    yum install nginx -y
    
  4. 启动 Nginx 并设置开机启动

    service nginx start
    chkconfig nginx on
    

方法三:从源代码编译安装

  1. 安装编译依赖

    yum groupinstall "Development Tools" -y
    yum install pcre-devel zlib-devel openssl-devel -y
    
  2. 下载并解压 Nginx 源代码

    wget http://nginx.org/download/nginx-1.12.2.tar.gz
    tar -zxvf nginx-1.12.2.tar.gz
    cd nginx-1.12.2
    
  3. 配置和编译

    ./configure --prefix=/usr/local/nginx --with-http_ssl_module
    make
    make install
    
  4. 创建启动脚本

    vi /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
    
  5. 设置权限并添加服务

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

    service nginx start
    

验证安装

无论使用哪种方法安装,都可以通过以下命令验证 Nginx 是否正常运行:

curl -I 127.0.0.1

或者在浏览器中访问服务器的 IP 地址,应该能看到 Nginx 的欢迎页面。

注意事项

  1. CentOS 6.5 默认使用较旧版本的 OpenSSL,如果需要支持 TLS 1.2,可能需要升级 OpenSSL。

  2. 如果使用防火墙,需要开放 80 端口:

    iptables -I INPUT -p tcp --dport 80 -j ACCEPT
    service iptables save
    
  3. 对于生产环境,建议使用官方仓库或源代码编译安装,以获取最新版本和安全更新。

  4. CentOS 6.5 已经结束生命周期(EOL),建议升级到 CentOS 7 或更高版本以获得更好的安全性和支持。