设为首页 收藏本站
查看: 893|回复: 0

[经验分享] 轻量级HTTP服务器Nginx

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2018-11-12 08:34:41 | 显示全部楼层 |阅读模式
[root@localhost ~]# groupadd -r nginx            #创建系统组  
[root@localhost ~]# useradd -r -g nginx nginx    #创建系统用户
  
[root@localhost ~]# id nginx                     #查看nginx用户相关信息
  
uid=494(nginx) gid=491(nginx) groups=491(nginx)  #uid and  gid都为nginx
  
[root@localhost nginx-1.6.2]# ./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临时文件路径
  
[root@localhost nginx-1.6.2]# make && make install
  
##加入PATH环境变量
  
[root@localhost ~]# vim /etc/profile.d/nginx.sh
  
#在里面加入如下语句
  
export PATH=/usr/local/nginx/sbin:$PATH
  
[root@localhost ~]# . /etc/profile.d/nginx.sh# source一下
  
[root@localhost nginx-1.6.2]# nginx -t   # 对nginx配置文件进行语法检查
  
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  
nginx: [emerg] 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}  # 创建目录
  
[root@localhost nginx-1.6.2]# nginx -t            # 再进行检查
  
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  
nginx: configuration file /etc/nginx/nginx.conf test is successful
  
# 可以看出其语法OK
  
[root@localhost ~]# /usr/local/nginx/sbin/nginx    # 启动nginx
  
[root@localhost ~]# ss -ntl            #查看80端口是否开启
  

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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633905-1-1.html 上篇帖子: 解决Nginx的connect() to 127.0.0.1:8080,selinux-J S Chu的技术家园 下篇帖子: nginx之rewrite规则未加引号导致检查nginx语法报错
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表