ddlddx0000 发表于 2018-11-12 08:34:41

轻量级HTTP服务器Nginx

# groupadd -r nginx            #创建系统组  
# useradd -r -g nginx nginx    #创建系统用户
  
# id nginx                     #查看nginx用户相关信息
  
uid=494(nginx) gid=491(nginx) groups=491(nginx)#uid andgid都为nginx
  
# ./configure --prefix=/usr/local/nginx \# nginx安装目录
  
--conf-path=/etc/nginx/nginx.conf\            # nginx配置文件路径
  
--user=nginx\                                 # 指定启动nginx的系统用户
  
--group=nginx\                                 # 制动启动nginx的系统组
  
--error-log-path=/var/log/nginx/error.log \      # 错误日志文件路径
  
--http-log-path=/var/log/nginx/access.log \      # 访问日志路径
  
--pid-path=/var/run/nginx/nginx.pid   \          # pid文件路径
  
--lock-path=/var/lock/nginx.lock   \             # 锁文件路径
  
--with-http_ssl_module   \                     # 启用ssl模块
  
--with-http_stub_status_module \             # 启用NginxStatus功能,以监控nginx当前状态
  
--with-http_gzip_static_module\            # 启用gzip_static_module模块
  
--with-http_flv_module   \                   # 启用流媒体模块
  
--with-http_mp4_module   \                   # 启用mp4流媒体模块
  
--http-client-body-temp-path=/var/tmp/nginx/client \ # 客户端请求报文包体临时文件路径
  
--http-proxy-temp-path=/var/tmp/nginx/proxy\      # 报文反向代理临时文件路径
  
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \   # 报文FastCGI临时文件路径
  
# make && make install
  
##加入PATH环境变量
  
# vim /etc/profile.d/nginx.sh
  
#在里面加入如下语句
  
export PATH=/usr/local/nginx/sbin:$PATH
  
# . /etc/profile.d/nginx.sh# source一下
  
# nginx -t   # 对nginx配置文件进行语法检查
  
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  
nginx: mkdir() "/var/tmp/nginx/client" failed (2: No such file or directory)
  
nginx: configuration file /etc/nginx/nginx.conf test failed
  
# 从上面可以看出其配置文件的语法是OK但是,有些目录在编译安装配置nginx的时候没有自动创建,此处需要手动创建其目录
  
mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi}# 创建目录
  
# nginx -t            # 再进行检查
  
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  
nginx: configuration file /etc/nginx/nginx.conf test is successful
  
# 可以看出其语法OK
  
# /usr/local/nginx/sbin/nginx    # 启动nginx
  
# ss -ntl            #查看80端口是否开启
  

  
###为nginx提供Sysv服务脚本:
  
# vim /etc/rc.d/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="/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:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
  
   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
  
===========
  
# chmod +x /etc/rc.d/init.d/nginx # 添加执行权限
  
# 添加至chkconfig列表中
  
# chkconfig --list nginx   # 查看chkconfig列表
  
service nginx supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add nginx')
  
# chkconfig --add nginx   # 添加nginx至chkconfig
  
# chkconfig --list nginx
  
nginx          0:off1:off2:off3:off4:off5:off6:off
  
# chkconfig --level 2345 nginx on   # 设定2345级别为开启
  
# chkconfig --list nginx
  
nginx          0:off1:off2:on3:on4:on5:on6:off
  
#service nginx start   # 启动nginx服务
  
#ss -tnl # 可以看到80端口已经打开
  
# 测试,此时在远端浏览器中输入:http://172.16.14.1
  
# 如若看到nginx的欢迎信息则表明安装成功,反之没有成功。


页: [1]
查看完整版本: 轻量级HTTP服务器Nginx