1.安装部署nginx

(1)安装

tar zxf nginx-1.14.0.tar.gz
vim auto/cc/gcc             #CFLAGS="$CFLAGS -g"##注销日志的debug模式,否则安装会产生多余的垃圾文件(占内存)
vim src/core/nginx.h #define NGINX_VER          "nginx/"    ##删除版本,其他主机访问的时候看不到版本
yum install -y gcc pcre-devel openssl-devel
./configure --prefix=/usr/local/nginx --with-file-aio
make && make install

(2)开启:nginx

/usr/local/nginx/sbin/nginx      //直接开启
ln -s /usr/local/nginx/sbin/nginx /sbin/    //做软链接,然后直接开启
nginx       //直接开启

(3)nginx默认属于nobody用户,可以新建nginx用户,使其属于nginx用户(也可以不设置)

ps -aux | grep nginx         //查看nginx属于那个用户//nobody用户##生成nginx用户,管理nginx
useradd -s /sbin/nologin编辑主配置文件:
vim /usr/local/nginx/conf/nginx.conf   user nginx nginx;worker_processes 2;
nginx -s reload         //重启
ps -aux | grep nginx    //再次查看,属于nginx用户//nginx

注:

nginx -t 配置文件语法检测
nginx -s stop 关闭
nginx -s reload 重启
nginx -v 查看版本信息
nginx -V 查看版本信息以及编译信息(加入了那些模块)

(3) 语法检测设置:编写配置文件会显示颜色

mkdir ~/.vim
cp -r /root/nginx-1.17.1/contrib/vim/*  ~/.vim

(4) 除了做软链接,还可以做systemd方式管理

cp /usr/lib/systemd/system/httpd.service /etc/systemd/system/nginx.service
vim /etc/systemd/system/nginx.service
## 修改配置文件内容如下:
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/nginx
ExecReload=/usr/local/nginx/nginx -s reload
ExecStop=/usr/local/nginx/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target

使用systemd方式管理nginx:

systemctl start nginx    //开启
systemctl stop nginx    //关闭
systemctl reload nginx  //刷新(restart)
systemctl status nginx  //查看状态

注:如果服务开启,应使用原来的方式先关闭,在时用systemd方式管理

nginx -s stop

2.版本的升级与回退

tar zxf nginx-1.16.0.tar.gz      //假设1.16为我们要升级的版本
vim auto/cc/gcc
./configure --prefix=/usr/local/nginx --with-file-aio
make

(1)热升级:在线升级

cd ~/nginx-1.16.0/objs
cp -f nginx /usr/local/nginx/sbin/nginx
ps -ef | grep nginx

root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
nginx 18035 18034 0 10:45 ? 00:00:00 nginx: worker process
nginx 18036 18034 0 10:45 ? 00:00:00 nginx: worker process
root 18232 1406 0 10:48 pts/0 00:00:00 grep --color=auto nginx


##使worker不在接收请求,同时打开新版本的nginx的master进程和它的两个线程,实现热升级
kill -USR2 18034(master进程号)
ps -ef | grep nginx     //旧版进程和新版进程同时存在

root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
nginx 18035 18034 0 10:45 ? 00:00:00 nginx: worker process
nginx 18036 18034 0 10:45 ? 00:00:00 nginx: worker process
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 18127 18126 0 10:46 ? 00:00:00 nginx: worker process
nginx 18128 18126 0 10:46 ? 00:00:00 nginx: worker process
root 18232 1406 0 10:48 pts/0 00:00:00 grep --color=auto nginx


kill -WINCH 18034    //关闭旧版本master进程的两个worker线程
ps -ef | grep nginx //旧版本的两个worker线程已经关闭

root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 18127 18126 0 10:46 ? 00:00:00 nginx: worker process
nginx 18128 18126 0 10:46 ? 00:00:00 nginx: worker process
root 18829 1406 0 11:00 pts/0 00:00:00 grep --color=auto nginx


/usr/local/nginx/sbin/nginx -V   //查看此时的nginx版本为 nginx/1.16.0

注: 新版本上线完成后不要结束原来的master进程,防止上线失败需要版本回退;若完全成功后,便可结束原master进程

(2)热回退:若上线失败,需在线回退为原来的版本

cd ~/nginx-1.17.1/objs
cp -f nginx /usr/local/nginx/sbin/nginx
kill -UIP 18034     //唤醒旧版本的master进程,使之产生新的worker线程
ps -ef | grep nginx

root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 20016 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20017 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20146 18034 0 11:26 ? 00:00:00 nginx: worker process
nginx 20147 18034 0 11:26 ? 00:00:00 nginx: worker process
root 20275 1406 0 11:28 pts/0 00:00:00 grep --color=auto nginx


kill -USR2 18126 //新版本的master进程的worker线程不再接收新的用户请求,使回退版本的worker进程接收新的用户请求。
ps -ef | grep nginx

root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 20016 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20017 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20146 18034 0 11:26 ? 00:00:00 nginx: worker process
nginx 20147 18034 0 11:26 ? 00:00:00 nginx: worker process
root 20626 1406 0 11:35 pts/0 00:00:00 grep --color=auto nginx


kill -WINCH 18126    //关闭新版本master进程的两个worker进程
ps -ef | grep nginx //新版本的两个worker线程已经关闭

root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 20146 18034 0 11:26 ? 00:00:00 nginx: worker process
nginx 20147 18034 0 11:26 ? 00:00:00 nginx: worker process
root 20734 1406 0 11:38 pts/0 00:00:00 grep --color=auto nginx


kill -9 18126    //回退完成后可以结束掉新版本的master进程

3.日志切割

ab -c 1 -n 10000 http://172.25.60.1/index.html   //创建访问日志
mv access.log `date +%F -d -1day`_access.log
nginx -s reopen

(1)创建旧日志存放目录

cd /usr/local/nginx
cd logs
mkdir oldlogs

(2)编写脚本

vim /usr/local/nginx/logs/logsbackup.sh
***************************************************************************
#!/bin/bash
LOG_PATH=/usr/local/nginx/logs/oldlogs ##定义切割日志后存放的路径
CUR_LOG_PATH=/usr/local/nginx/logs ##定义nginx日志存放的路径
YESTERDAY=$(date +%F -d -1day)        ##定义日期为昨天mv $CUR_LOG_PATH/access.log $LOG_PATH/${YESTERDAY}_access.log  ##将昨天的access日志文件移动到切割目录,并且重命名
mv $CUR_LOG_PATH/error.log $LOG_PATH/${YESTERDAY}_error.log ##将昨天的error日志文件移动到切割目录,并且重命名
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)   ##生成新的日志
#/usr/local/nginx/sbin/nginx -s reopen
***************************************************************************
chmod +x /usr/local/nginx/logs/logsbackup.sh   //给脚本可执行权限

(3)设置定时执行

crontab -e       //编辑定时任务
*********************************************************
0 0 * * * /bin/bash /usr/local/nginx/logs/logsbackup.sh
#每天的00:00进行日志切割备份
*********************************************************
crontab -l  //查看任务

5.gzip–压缩文字

(1)修改配置文件

vim conf/nginx.conf
********************************33     gzip  on;        //开启gzip压缩34     gzip_min_length 20;    //设置将被gzip压缩的响应的最小长度(K)35     gzip_comp_level 3;  //设置响应的gzip压缩级别(1--9)36     gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;********************************
systemctl reload nginx

(2)测试
打开浏览器,清空缓存(ctrl + shift + delete)
访问nginx服务器(172.25.60.1)
curl查看:ctrl+shift+c—>Network—>reload—>看Size和Transf

nginx的安装部署,热升级与回退,日志切割以及gzip(文字压缩)相关推荐

  1. Nginx详细安装部署教程

    一.Nginx简介 Nginx是一个web服务器也可以用来做负载均衡及反向代理使用,目前使用最多的就是负载均衡,具体简介我就不介绍了百度一下有很多,下面直接进入安装步骤 二.Nginx安装 1.下载N ...

  2. Nginx windows安装部署

    一.Nginx简介 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Ramble ...

  3. nginx - nginx的安装部署

    目录 什么是nginx? 1. nginx的安装 1.1  yum安装和编译安装的区别 1.1.1  yum安装的优点和缺点 1.1.2 编译安装的优点和缺点 1.2  nginx编译安装脚本 1.3 ...

  4. nginx的安装部署

    首先切换到nginx用户(确定已经在Linux服务器上新建了一个nginx用户) su nginx 进行安装前的检查,检查服务器上是否安装了gcc-c++ rpm -qa|grep gcc-c++ 如 ...

  5. nginx软件安装部署

    一.下载 两种安装方式: 1.yum安装软件 a 使用官方yum源进行安装 安装的是最新版本 软件目录结构比较标准 (推荐) b 使用非官方yum源进行安装 安装的不是最新版 目录结构会发生变化 2. ...

  6. 安装部署及升级到Exchange Server 2010

    本文档详细的描述了,如何在Windows Server 2008 R2的环境下安装Exchange Server 2010,包括的内容有: 先检查组织环境: 1.请确保林的功能级别至少为 Window ...

  7. Nginx+Supervisor安装部署.NET Core项目

    部署环境  OS:CentOS7  .NET Core SDK:2.2.402  安装.NET Core SDK  1.1 安装依赖yum install libunwind libicudotnet ...

  8. Nginx编译安装和平滑升级

    一.Nginx的编译安装 1.安装依赖包gcc,gcc-c++,pcre,openssl-devel 命令:yum -y install gcc gcc-c++ pcre-devel openssl- ...

  9. Nginx Keepalived安装部署完整步骤

    安装Keepalived 配置Keepalived 编写nginx监测脚本 启动Keepalived

最新文章

  1. QIIME 2教程. 29参考数据库DataResources(2020.11)
  2. JavaScript 数组拼接打印_JavaScript 数组方法
  3. Windows 技术篇-cmd强制关闭端口、解除端口占用方法,cmd查询端口相关的进程pid并杀死进程实例演示
  4. 【ECCV 2018】Facebook开发姿态转换模型,只需一张照片就能让它跳舞(视频)
  5. springboot---mybits整合
  6. 每日一笑 | 今天是植树节,我想在你心里种点逼树
  7. python 中文编码差异_Python 编码为什么那么蛋疼?
  8. 戴尔SC5020发布,专为提高效率/经济性优化设计的中端存储利器
  9. UnsatisfiedDependencyException
  10. ns账号切换服务器对存档有影响吗,《集合啦!动物森友会》不支持存档转移 更换Switch需要重新游戏...
  11. 音视频编解码常用知识点
  12. opencv学习十三:图像金字塔和图像梯度
  13. 陈强教授《机器学习及R应用》课程 第五章作业
  14. 【活动回顾】大咖分享:流量过后,在线教育的留存和发展
  15. 宏观调控利于粮食安全,调控与市场关系需理顺
  16. Python 类的继承和组合
  17. Adobe Flashplayer orHTML5 Browser with WebGL or CSS3D support required 解决方案
  18. 层次分析法之matlab代码实现
  19. 【嵌入式】STM32基于SPI通信协议OLED屏显示
  20. 测试用例-------邮箱注册

热门文章

  1. java计算机毕业设计智能旅游电子票务系统演示录像2020源码+mysql数据库+系统+部署+lw文档
  2. MDK编译全过程及数据存储
  3. 小组取什么名字好_好消息!这座天桥今年年底完工!取什么名字,等你出主意...
  4. 18位身份证号码编码标准
  5. 云服务器1和1g装什么系统好,1核1g的云服务器装什么系统好
  6. 关于计算机网络隐私的作文,网络安全小心作文
  7. [Leetcode] 377. 组合总和 Ⅳ
  8. mysql+php+模板+条目_html模板
  9. C++ 实现带监视哨的顺序查找
  10. Torque2D MIT 实战记录: Isometric(等轴视距)