1.官网获取 rpm 安装包 创建目录 /usr/local/nginx,将 rpm 包下载到该目录下
根据 [baseurl] 指定的路径,找到 nginx 各版本 rpm 安装包,
- [root@localhost nginx]# wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm
2. 安装依赖组件 根据官方安装步骤,需要先安装依赖工具,实测 CentOS7.5 默认已经加载,无需安装
- yum install yum-utils
3. rpm 安装 执行 rpm 包安装
- [root@localhost nginx]# rpm -ivh nginx-1.20.2-1.el7.ngx.x86_64.rpm
使用 rmp 安装,默认启用了所有 nginx 模块,如下
- [root@localhost nginx]# nginx -V
- nginx version: nginx/1.20.2
- built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
- built with OpenSSL 1.0.2k-fips 26 Jan 2017
- TLS SNI support enabled
- configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
4. 查看 nginx 安装目录 rpm 安装 nginx 完成后,程序文件默认存放在多个目录
- [root@localhost ~]# find / -name nginx
- /etc/logrotate.d/nginx
- /etc/nginx #主目录及配置文件
- /var/log/nginx #日志
- /var/cache/nginx #缓存
- /usr/sbin/nginx #主程序
- /usr/lib64/nginx #组件模块
- /usr/share/nginx #html 主页
特别关注:以上目录其实在安装程序时指定,可以通过查看 nginx 版本命令确认安装时指定的参数,包括各项目录以及常用模块,如 stream
- [root@localhost ~]# nginx -V
- nginx version: nginx/1.20.2
- built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
- built with OpenSSL 1.0.2k-fips 26 Jan 2017
- TLS SNI support enabled
- configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
rpm 安装默认生成系统服务文件,可以通过 service 或 systemctl 命令操作
- [root@localhost ~]# find / -name nginx.service
- /usr/lib/systemd/system/nginx.service
rpm 安装默认已经将主程序添加到系统变量
- [root@localhost ~]# ll /usr/sbin/nginx
- -rwxr-xr-x. 1 root root 1377720 11 月 16 23:03 /usr/sbin/nginx
配置文件存放在 /etc/nginx/nginx.conf,启动时默认加载该路径,也可以在启动时指定配置文件,配置文件结构如下,分为 3 个部分:全局块、events 块、http 块
- worker_processes 2;
-
- events {
- use epoll;
- worker_connections 1024;
- }
-
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- 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;
- }
- }
- }
特别关注:配置文件中指定的所有路径,请在启动前确保已存在
配置文件说明 1.全局块 从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等
- #定义 Nginx 运行的用户和用户组
- user nginx nginx;
-
- #nginx 进程数,建议设置为等于 CPU 总核心数
- worker_processes 8;
-
- #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
- error_log /var/log/nginx/error.log info;
-
- #进程文件
- pid /var/run/nginx.pid;
-
- #nginxworker 最大打开文件数,可设置为系统优化后的 ulimit -n 的结果
- worker_rlimit_nofile 65535;
2.events 块
- events
- {
- #epoll 模型是 Linux 2.6 以上内核版本中的高性能网络 I/O 模型,如果跑在 FreeBSD 上面,就用 kqueue 模型
- use epoll;
-
- #单个 worker 进程最大连接数(nginx 最大连接数=worker 连接数*进程数)
- worker_connections 65535;
- }
3.http 块 这部分应该是 Nginx 服务器配置中最频繁的部分;代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。 http 块分为:http 全局块、server 块。 3.1http 全局块配置
- http
- {
- #nginx 支持的媒体类型库文件
- include mime.types;
-
- #默认媒体文件类型
- default_type application/octet-stream;
-
- #默认编码
- charset utf-8;
-
- #服务器名字的 hash 表大小
- server_names_hash_bucket_size 128;
-
- #上传文件大小限制
- client_header_buffer_size 32k;
-
- #开启高效文件传输模式,实现内核零拷贝
- sendfile on;
-
- #开启目录列表访问,适合下载服务器,默认关闭。
- autoindex off;
-
- #长连接超时时间,单位是秒
- keepalive_timeout 120;
- }
3.2http server 块的配置,即虚拟主机的配置
- server #网站配置区域
- {
- #默认监听 80 端口
- listen 80;
-
- #提供服务的域名主机名
- server_name www.sample.com;
- location / {
- #站点根目录(这里 html 是相对路径,默认网站根目录为:/usr/local/nginx/html)
- root html;
-
- #默认首页文件,多个用空格分开
- index index.thml index.htm;
- }
-
- #出现对应 http 状态码时,使用 50x.html 回应客户
- error_page 500 502 503 504 /50x.html;
-
- location = /50x.thml {
- #指定对应目录
- root html;
- }
- }
默认使用 root 运行即可。
请参考教程 Linux 开机启动方案
通过 nginx 主程序启动服务,以服务器 192.168.11.14 为例
- nginx -c /etc/nginx/nginx.conf
nginx 默认配置将作为 web 服务器运行,启动后在浏览器访问 http://192.168.11.14 即可打开页面,默认首页为 /usr/local/nginx/html/index.html,可以修改内容,显示当前服务器 IP
并发测试可以使用工具: Apache 自带的 ab 压力测试工具 与 nginx 并发控制相关的参数包括 ulimit -n(Linux 操作系统最大打开的文件描述符限制)、worker_processes、worker_rlimit_nofile、worker_connections、epoll(Linux IO 模型)。
特别关注:查看 CPU 核心数(非 CPU 个数,一块 CPU 可以有多个核心)
- [root@localhost ~]# cat /proc/cpuinfo | grep 'processor' | wc -l
- 4
- [root@localhost ~]# lscpu | grep "CPU(s):"
- CPU(s): 4
特别关注:关于 Linux 操作系统最大打开的文件描述符限制 使用 ulimit -n 可即时修改,但重启后就无效了,要设置 /etc/security/limits.conf 配置文件,重启系统后才可以永久生效。 limits.conf 的格式如下
- <domain> <type> <item> <value>
- domain: 限制作用的用户范围,通配符*表示是针对所有用户的限制,也可以指定用户,例如:root
- type: 限制类型
- soft 指的是当前系统生效的最大值(超过后仅警告)
- hard 表明系统中所能设定的最大值(超过后系统错误)
- soft 的限制不能比 hard 限制高
- - 表明同时设置了 soft 和 hard 的值
- item: 限制的具体内容
- nofile - 打开的文件描述符的最大数目
- nproc - 进程最大数量
- value: 具体限制值
-
- 示例:以下限制所有用户打开的文件描述符最大数目为 65535
- * - nofile 65535
配置 ulimit 合并脚本,方便执行
- ulimit -n 65535 && echo -e "*\t-\tnproc\t65535\n*\t-\tnofile\t65535" >> /etc/security/limits.conf
特别关注:ulimit -n 与 limits.conf 配置对 nginx 服务有时不生效 因为 ulimit 和 limits.conf 的配置只针对登录用户启动的应用服务 (nginx -c nginx.conf),而对 systemd 管理的服务不起作用(service nginx start),服务的 limit 要在相应的 service 文件中单独指定,具体可以参考 centos8 平台 nginx 服务配置打开文件限制 max open files limits
location 是 nginx 使用最多的块级指令,建议了解学习 nginx location 配置规则与经验
特别关注 nginx 日志管理比较特殊,请参考 Linux 日志管理经验总结(crontab+logrotate)