NGINX的详细安装教程(超详细)

1:先下载NGINX并上传到服务器

下载地址:http://nginx.org/en/download.html

2:安装环境依赖

#安装gcc编译器
    yum install gcc-c++

#安装PCRE 用于解析正则表达式
    yum install -y pcre pcre-devel
#安装zlib 用于解压和压缩依赖
    yum install -y zlib zlib-devel
#安装 SSL 套接字安全协议层 https
    yum install -y openssl openssl-devel


 

3:解压和配置nginx

#解压nginx
    tar nginx-1.18.0.tar.gz 
#建立临时文件 否则后面会报错
    mkdir -vp /var/temp/nginx 
#在cd 到nginx目录执行如下 如下命令 为了制作makefile
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_ssl_module

#扫盲-->这里是注释不是命令 别拷贝
#prefix 指定nginx目录
#pid-path 指定nginx的pid
#lock-path 锁定安装文件 防止恶心篡改或误操作
#error-log 错误日志
#http-log-path http日志
#http-client-body-temp-path 启用gzip模块 在线实时压缩输出数据流
#http-proxy-temp-path设定客户端请求的临时目录
#http-fastcgi-temp-path 设定fastcgi临时目录
#http-uwsgi-temp-path 设置定uwsgi的临时目录
#http-scgi-temp-path 设置scgi的临时目录
#with-http_ssl_module https需要用到的模块



4:编译nginx

#编译nginx
    make
#安装nginx
    make install
#进入nginx sbin目录
    cd /usr/local/nginx/sbin/
#启动nginx
    ./nginx
#停止命令
    ./nginx -s stop

#刷新配置
    ./nginx -s reload

    

5:常用命令以及自动启动

#本地虚拟机用需要先关闭防火墙的 阿里云和腾讯云 需要将80端口加入策略组
#查看当前防火墙状态
    systemctl status firewalld
#关闭当前防火墙
    systemctl stop firewalld 
#开机防火墙不启动
   systemctl disable firewalld
#配置开机自动启动(官网文件贴在最后 下面是需要注意的地方)
    vim /etc/init.d/nginx
    nginx=”/usr/local/nginx/sbin/nginx” //修改成nginx执行程序的路径。
    NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” //修改成nginx.conf文件的路径。
#添加权限
    chmod a+x /etc/init.d/nginx
#添加到nginx到chkconfig
    chkconfig --add /etc/init.d/nginx
#添加到自动启动
    chkconfig nginx on

#附
    service nginx start
    service nginx stop
    service nginx restart

官方自启动

#!/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 $prog -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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值