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

[经验分享] 生产场景:实战nginx源码编译安装

[复制链接]

尚未签到

发表于 2018-11-14 12:42:37 | 显示全部楼层 |阅读模式
  生产场景:nginx实战安装
  一、准备环境:
  1.1 操作系统:centos 6、7   
  
  安装常用软件
yum install tree telnet dos2unix sysstat lrzsz nc nmap zip unzip -y  1.2 官网下载ngnx源码包nginx-1.12.2.tar.gz,并隐藏nginx版本号和修改nginx软件名
  下载nginx源码包nginx-1.12.2.tar.gz,并隐藏nginx版本号和修改nginx软件名(此步骤省略)。
  二、开始安装nginx
  2.1 开始安装nginx并启动测试
####################快速安装nginx#############################  
mkdir /server/tools -p
  
mkdir /application
  
yum install openssl openssl-devel pcre pcre-devel -y
  
useradd www -s /sbin/nologin -M
  
cd /server/tools/
  
rz -y  #上传优化好隐藏nginx版本号和修改nginx软件名字为Tengine的模板或者直接下载官网wget http://nginx.org/download/nginx-1.12.2.tar.gz,建议上传优化好的模板
  
tar xf nginx-1.12.2.tar.gz
  
cd nginx-1.12.2
  
./configure --user=www --group=www --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module
  
make
  
make install
  
ln -s /application/nginx-1.12.2/ /application/nginx
  
####################快速安装nginx#############################
  检查语法并启动nginx
/application/nginx/sbin/nginx -t  
/application/nginx/sbin/nginx
  
[root@web01 nginx-1.12.2]# ps -ef|grep nginx
  
root     25150     1  0 16:39 ?        00:00:00 nginx: master process /application/nginx-1.12.2 sbin/nginx
  
www      25151 25150  0 16:39 ?        00:00:00 nginx: worker process
  
root     25164 16972  0 16:41 pts/0    00:00:00 grep nginx
  浏览器打开web02 IP查看是否可以看到nginx主页:
  http://10.0.0.8/
DSC0000.jpg

  测试完成后关闭nginx服务。
/application/nginx/sbin/nginx -s stop  2.2 优化nginx配置文件
  
  2.2.1 优化nginx.conf配置文件
cd /application/nginx/conf/  
cp nginx.conf{,.ori}
  
egrep -v "^$|#" nginx.conf.default >nginx.conf       #最小化nginx配置
  查看默认配置文件
[root@web01 conf]# cat nginx.conf  
worker_processes  1;
  
events {
  
    worker_connections  1024;
  
}
  
http {
  
    include       mime.types;
  
    default_type  application/octet-stream;
  
    sendfile        on;
  
    keepalive_timeout  65;
  
    server {
  
        listen       80;
  
        server_name  localhost;
  
        location / {
  
            root   html;
  
            index  index.html index.htm;
  
        }
  
        error_page   500 502 503 504  /50x.html;
  
        location = /50x.html {
  
            root   html;
  
        }
  
    }
  
}
  vim nginx.conf把server标签移除,并在http标签中加入include extra/www.conf;和include extra/status.conf;
[root@web01 conf]# vim nginx.conf  
worker_processes  1;
  
events {
  
    worker_connections  1024;
  
}
  
http {
  
    include       mime.types;
  
    default_type  application/octet-stream;
  
    sendfile        on;
  
    keepalive_timeout  65;
  
    include extra/www.conf;
  
    #include extra/status.conf;
  
}
  2.2.2 优化www.conf配置文件
  增加www.conf目录extra及配置文件www.conf
cd /application/nginx/conf  
mkdir extra
  
[root@web01 extra]# vim extra/www.conf      #添加server标签,www1.etiantian.com用于监控www.etiantian.com是否正常
  
    server {
  
        listen       80;
  
        server_name   www1.etiantian.com ;
  
        location / {
  
            root   html/www;
  
            index  index.html index.htm;
  
        }
  
    }
  配置完成后,重启nginx生效
/application/nginx/sbin/nginx -t  
/application/nginx/sbin/nginx -s reload
  2.2.3 优化status.conf配置文件
  1) 增加status.conf目录及配置文件
[root@web01 conf]# pwd  
/application/nginx/conf
  
[root@web01-14 conf]# vim extra/status.conf
  
### status
  
    server {
  
    listen       80;
  
    server_name  status.etiantian.com;
  
    access_log off;
  
    location / {
  
        stub_status on;
  
        access_log off;
  
        allow 10.0.0.0/24;
  
        deny all;
  
    }
  
    }
  2) 也可以用location的方式实现状态配置,例如在任意一个虚拟主机里面为server标签增加如下配置,例如www.conf的server标签增加
    location /nginx_status {  
        stub_status on;
  
        access_log off;
  
        allow 10.0.0.0/24;    #允许IP段访问
  
        deny all;            #禁止IP段访问
  
    }
  例如放到www.conf的server标签里面
[root@web01 extra]# vim extra/www.conf      #添加server标签,www1.etiantian.com用于监控  
    server {
  
        listen       80;
  
        server_name  www.etiantian.com www1.etiantian.com;
  
        location / {
  
            root   html/www;
  
            index  index.html index.htm;
  
        }
  
        location /nginx_status {
  
            stub_status on;
  
            access_log off;
  
            allow 10.0.0.0/24;   #允许IP段访问
  
            deny all;        #禁止IP段访问
  
                }
  
    }
  3) 配置完成后重启nginx让配置生效
/application/nginx/sbin/nginx -t  
/application/nginx/sbin/nginx -s reload
  4)浏览器访问status.etiantian.com查看状态
  2.3 增加日志文件
  2.3.1 增加错误日志和访问日志
  增加错误日志error_log可以在nginx.conf中添加,也可以在www.conf中添加。
error_log  logs/error.log;  log_format用来定义访问日志的格式,在http标签中添加
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
                      '$status $body_bytes_sent "$http_referer" '
  
                      '"$http_user_agent" "$http_x_forwarded_for"';
  
    #access_log  logs/access.log  main;  #访问日志不建议全局添加,所以注释了。
[root@web01 ~]# vim /application/nginx/conf/nginx.conf  
worker_processes  1;
  
error_log  logs/error.log;    #增加错误日志
  
events {
  
    worker_connections  1024;
  
}
  
http {
  
    include       mime.types;
  
    default_type  application/octet-stream;
  
    #add log
  
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  
                      '$status $body_bytes_sent "$http_referer" '
  
                      '"$http_user_agent" "$http_x_forwarded_for"';
  
    #access_log  logs/access.log  main;
  
    sendfile        on;
  
    keepalive_timeout  65;
  
    include extra/www.conf;
  
    include extra/status.conf;
  
}
  在www.conf的server标签中增加 access_log  logs/access_www.log  main;即可
  如果是高并发访问日志加access_log  logs/access_www.log  main gzip buffer=32k flush=5s;经过验证加此项访问日志会出现乱么,暂时未找到乱么原因。
[root@web01 conf]# vim extra/www.conf  
    server {
  
        listen       80;
  
        server_name   www1.etiantian.com ;
  
        location / {
  
            root   html/www;
  
            index  index.html index.htm;
  
        }
  
        access_log  logs/access_www.log  main;
  
        #access_log  logs/access_www.log  main gzip buffer=32k flush=5s;
  
        #access_log off;
  
    }
  配置完成后,重启nginx生效
/application/nginx/sbin/nginx -t  
/application/nginx/sbin/nginx -s reload
  查看日志:
[root@web01 logs]# pwd  
/application/nginx/logs
  
[root@web01 logs]# tailf access_www.log
  在linux客户端机器上面
curl www.etiantian.com或者curl -I www.etaintian.com  然后就出现了访问日志
[root@web01 logs]# tailf access_www.log  
121.76.16.231 - - [11/Aug/2018:16:22:33 +0800] "GET / HTTP/1.1" 200 6002 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "112.74.36.43"
  
121.25.115.6 - - [11/Aug/2018:16:22:34 +0800] "GET / HTTP/1.1" 200 6002 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "112.74.36.43"
  
121.76.16.225 - - [11/Aug/2018:16:22:34 +0800] "GET / HTTP/1.1" 200 6002 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "112.74.36.43"
  
121.76.16.232 - - [11/Aug/2018:16:22:35 +0800] "GET / HTTP/1.1" 200 6002 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "112.74.36.43"
  2.3.2 切割访问日志
  1)写切割脚本
mkdir /server/scripts -p  
mkdir /data/backup/logs -p
  
cd /server/scripts/
  
[root@web01 scripts]# vim cut_nginx_log.sh
  
#!/bin/bash
  
Dateformat=`date +%Y%m%d -d "-1 day"`
  
Basedir="/application/nginx"
  
Nginxlogdir="$Basedir/logs"
  
Logname="access_www"
  
Backuplogdir="/data/backup/logs"
  
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
  
[ -f ${Logname}.log ]||exit 1
  
/bin/mv ${Logname}.log ${Backuplogdir}/${Dateformat}_${Logname}.log
  
$Basedir/sbin/nginx -s reload
  2)定时任务,每天凌晨00:00执行切割脚本
[root@web01 scripts]# crontab -e  
#creat by jeremy 2018-06-28
  
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
  
00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
  查看定时任务
[root@web01 scripts]# crontab -l  
#creat by jeremy 2018-06-28
  
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
  
00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
  查看定时任务效果
[root@web01 logs]# ll /data/backup/logs/  
total 8
  
-rw-r--r-- 1 root root    0 Aug 11 16:49 20180810_access_www.log
  
-rw-r--r-- 1 root root 8135 Aug 11 16:27 20180811_access_www.log



运维网声明 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.yunweiku.com/thread-635016-1-1.html 上篇帖子: ubuntu 安装nginx+php+mysql lnmp的安装 (转载) 下篇帖子: 安装nginx-qq5a1d70810d61d的博客
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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