nginx安装
1.nginx编译安装
#在nginx官网获取安装包
[root@webserver mnt]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
#解压安装包
[root@webserver mnt]# tar zxf nginx-1.24.0.tar.gz
[root@webserver mnt]# cd nginx-1.24.0/
#安装编译nginx需要的环境软件
[root@webserver mnt]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
#创建运行nginx的用户,-M不指定家目录
[root@webserver nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@webserver nginx-1.24.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
#配置编译环境
[root@webserver nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \ #指定安装路径
--user=nginx \ # 指定nginx运行用户
--group=nginx \ # 指定nginx运行组
--with-http_ssl_module \ # 支持https://
--with-http_v2_module \ # 支持http版本2
--with-http_realip_module \ # 支持ip透传
--with-http_stub_status_module \ # 支持状态页面
--with-http_gzip_static_module \ # 支持压缩
--with-pcre \ # 支持正则
--with-stream \ # 支持tcp反向代理
--with-stream_ssl_module \ # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透传ip
#开始编译
[root@webserver nginx-1.24.0]# make
#安装nginx
[root@webserver nginx-1.24.0]# make install
#完成安装以后,有四个主要的目录
[root@webserver nginx-1.24.0]# ls /usr/local/nginx/
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
#conf 保存nginx所有的配置文件目录
#html 保存nginx服务器的web文件
#logs 保存nginx服务器访问日志错误日志等
#sbin 保存nginx二进制启动脚本
2.添加nginx到环境变量
[root@webserver nginx-1.24.0]# echo export PATH=$PATH:/usr/local/nginx/sbin/ >> ~/.bash_profile
#使新添加的参数生效
[root@webserver nginx-1.24.0]# source ~/.bash_profile
#验证版本及编译参数
[root@webserver nginx-1.24.0]# nginx -V
3.平滑升级和回滚
nginx的平滑升级与回滚是在不重启服务的情况下来操作,避免正在访问的服务中断
1.编译要升级版本
#对要升级的版本进行编译
[root@nginx mnt]# tar zxf nginx-1.26.1.tar.gz
[root@nginx mnt]# cd nginx-1.26.1/
[root@nginx nginx-1.26.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
#关闭编译时的显示的调试信息
[root@nginx nginx-1.26.1]# vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g"
#修改HTTP响应头信息的server软件信息
[root@nginx nginx-1.26.1]# vim src/core/nginx.h
#define NGINX_VERSION "6.6.6"
#define NGINX_VER "fjw/" NGINX_VERSION
[root@nginx nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginx nginx-1.26.1]# make
2. 平滑升级
#切换到二进制启动脚本目录,进行平滑升级
[root@nginx ~]# cd /usr/local/nginx/sbin/
#对旧版本进行备份
[root@nginx sbin]# cp nginx nginx.old
#把要升级的新版本拷贝过来
[root@nginx sbin]# /bin/cp -f /mnt/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin/nginx
[root@nginx sbin]# ll
total 11156
-rwxr-xr-x 1 root root 5746872 Jul 25 01:08 nginx
-rwxr-xr-x 1 root root 5671480 Jul 25 01:05 nginx.old
[root@nginx sbin]# ps aux | grep nginx
root 42282 0.0 0.0 9876 2052 ? Ss 01:10 0:00 nginx: master process
nginx 42283 0.0 0.1 14208 4996 ? S 01:10 0:00 nginx: worker process
root 42287 0.0 0.0 221664 2176 pts/0 S+ 01:10 0:00 grep --color=auto
#-USR2对旧nginx进行平滑升级,重载生成新进程
[root@nginx sbin]# kill -USR2 42282 #旧nginx,masterPID
[root@nginx sbin]# ps aux | grep nginx
root 42282 0.0 0.0 9876 2436 ? Ss 01:10 0:00 nginx: master process
nginx 42283 0.0 0.1 14208 4996 ? S 01:10 0:00 nginx: worker process
root 42288 0.0 0.1 9876 6528 ? S 01:13 0:00 nginx: master process
nginx 42289 0.0 0.1 14208 4996 ? S 01:13 0:00 nginx: worker process
root 42291 0.0 0.0 221664 2176 pts/0 S+ 01:13 0:00 grep --color=auto
#-WINCH回收旧nginx也是masterPID
[root@nginx sbin]# kill -WINCH 42283
[root@nginx sbin]# ps aux | grep nginx
root 42282 0.0 0.0 9876 2436 ? Ss 01:10 0:00 nginx: master process
root 42288 0.0 0.1 9876 6528 ? S 01:13 0:00 nginx: master process
nginx 42289 0.0 0.1 14208 4996 ? S 01:13 0:00 nginx: worker process
root 42294 0.0 0.0 221664 2176 pts/0 S+ 01:14 0:00 grep --color=auto
#查看版本,新版本生效
[root@nginx sbin]# nginx -V
nginx version: fjw/6.6.6
......
测试
[root@nginx sbin]# curl -I 172.25.254.11
#如果旧版本的进程没用的话就可以kill -9删除了
[root@nginx sbin]# kill -9 42283
3.版本回滚
#对nginx版本进行回滚
#如果发现升级后新版本有问题,可以重新垃圾旧版本的worker
[root@nginx sbin]# cp nginx nginx.new
[root@nginx sbin]# ls
nginx nginx.new nginx.old
[root@nginx sbin]# mv nginx.old nginx
mv: overwrite 'nginx'? y
[root@nginx sbin]# ls
nginx nginx.new
#重新拉起旧版本nginx的worker
[root@nginx sbin]# kill -HUP 42282 #master
#回收新版本的worker
[root@nginx sbin]# kill -WINCH 42288 #master的pid
[root@nginx sbin]# nginx -V
nginx version: nginx/1.24.0
测试
[root@nginx sbin]# curl -I 172.25.254.11
#如果新版本的进程没用的话就可以kill -9删除了
[root@nginx sbin]# kill -9 4228
4.nginx生成启动文件systemd
生成systemd可以实现开机自启动
#百度搜索模板
systemd site:nginx.org #搜索内容 site:搜索网址
[root@webserver ~]# cd /lib/systemd/system
[root@webserver system]# vim nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid #指定nginx启动的pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t #指定nginx -t检查配置文件命令
ExecStart=/usr/local/nginx/sbin/nginx #指定nginx启动命令
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#使编写的配置生效
[root@webserver system]# systemctl daemon-reload
#在启动时要确保nginx已经关闭不然会冲突导致报错
[root@webserver system]# systemctl enable --now nginx
nginx核心配置详解
1.nginx主配置文件说明
主配置文件结构:四部分
main block:主配置段,即全局配置段
#事件驱动相关的配置
event {
...
}#http/https 作为web服务器相关配置段
http {
...
}#默认配置文件不包括下面两个部分
#mail 作为邮件服务器相关配置段
mail {
...
}#stream 反向代理相关配置段
stream {
...
}
2.全局配置参数
默认打开全局配置参数
3.events配置参数
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 100000; #单个工作进程最大并发数
use epoll; #使用epoll机制来实现高并发
} #Nginx支持众多的事件驱动,
#比如:select、poll、epoll,只能设置在events模块中设置
4.nginx的高并发配置
#编辑配置文件设置参数
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes auto;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 100000; #设置每个工作进程最大能打开的文件个数,此参数要与系统级别的最大打开文件数相同要与ulimit -n的个数相同
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 100000; #设置单个工作进程最大并发数
use epoll;
}
#设置系统的每个进程打开的最大打开文件数
[root@nginx ~]# vim /etc/security/limits.conf
* - nofile 100000 #打开文件最大个数
* - nproc 100000 #打开程序最大个数
[root@nginx ~]# ulimit -n 100000 #设置当前或者重启shell
[root@nginx ~]# ulimit -n #查看个数
100000
#安装apache工具包中的压力测试工具ab
[root@webserver ~]# dnf install httpd-tools -y
#-n总请求数 -c每次并发数
[root@webserver ~]# ab -n 100000 -c 5000 http://172.25.254.10/index.html