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