Nginx是一款优秀的开源软件,支持在多平台运行. Centos拥有良好的系统结构和工具软件生态系统, 是一款基于Linux的非常流行的发行版本.我们选择将他作为本书的操作系统环境
- 编译环境准备
1.1. 操作系统准备
(1) 系统服务安装
Centos可用最小化安装, 安装完毕之后, 用如下命令补充工具
yum -y install epel-release #安装扩展工具包yum源
yum install net-cools wget nscd lsof #安装工具
(2)DNS缓存
编辑/etc/resolv.conf配置DNS服务器, 打开NSCD服务, 缓存CDN,提高域名解析响应速度
systemctl start ncsd.service #启动NSCD服务
systemctl enable
(3)修改文件打开数量限制
操作系统默认单进程最大打开文件数目为1024, 要想实现高并发, 可以把单进程的文件打开数调整为65536
echo "* soft nofile 65536 #*表示所用用户
\* hard nofile 65536" >>
- Nginx源码编译
2.1. Nginx源码获取
mkdir -p /opt/data/source
cd /opt/data/source
wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar
2.2. 编译配置参数
编译Nginx源码文件时,首先需要通过编译配置命令configure进行编译配置.可以通过./configure --help
获得更多的编译配置参数
2.3 代码编译
安装编译工具以及依赖库,脚本如下:
编译所有功能模块,脚本如下
#执行编译配置
./configure
#代码编译及安装
make&make install
可以根据需求灵活调整
2.4.添加第三方模块
Nginx的功能是以模块方式存在的,同时支持添加第三方开发的功能模块. 在执行configure时,通过–add-module=PATH参数指定第三方模块的代码路径,在make时就可以进行同步编译了
添加第三方静态模块的方法如下:
./configure --add-module=../ngx_http_proxy_connect_module
添加第三方动态模块
./configure --add-dynamic-module=../ngx_http_proxy_connect_module \
--wit-compat
- Nginx部署
3.1. 环境配置
Nginx编译成功之后,为了便于操作维护, 建议把nginx执行文件的路径添加到环境变量中, 可以通过一下命令来完成
cat > /etc/profile.d/nginx.sh<<EOF
PATH=$PATH:/usr/local/nginx/sbin
EOF
source
在Centos操作系统中, 配置文件通常放在/etc目录下, 建议将Nginx的conf目录软连接到/etc目录下
3.2. 命令行参数
Nginx执行文件的命令行参数可以通过-h来获取,Nginx命令行参数如下:
nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
上述代码的具体参数解释说明如下:
- v参数: 显示Nginx执行文件的版本信息
- V参数: 显示Nginx执行文件的版本信息和编译配置参数
- t参数: 进行配置文件语法检查,测试配置文件的有效性
- T参数: 进行配置文件语法检查,测试配置文件有效性.同时输出所有有效配置内容
- q参数: 在测试配置文件有效性,同时输出所有有效配置内容
- s参数: 发送信号给Nginx主进程,信号可以是一下四个:
stop: 快速关闭
quit: 正常关闭
reopen: 重新打开日志文件
reload: 重新加载配置文件,启动一个加载新配置文件的Woker Process,正常关闭一个加载旧配置文件的Worker Process
- p参数: 指定Nginx的执行目录, 默认为configure时的安装目录, 通常为/usr/local/nginx
- c参数: 指定nginx.conf文件的位置, 默认为conf/nginx.conf
- g参数: 外部指定配置文件的全局指令
3.3. 注册系统服务
Centos系统环境中使用systemd进行系统和服务管理, 可以按需守护进程,并通过systemctl 命令进行systemd的监测和控制.为了方便Nginx应用进程的维护和管理, 此处把Nginx注册成系统服务, 由systemd进行服务管理
cat >/usr/lib/systemd/system/nginx.conf<<EOF
[Unit] #记录service文件的通用信息
Description=The Nginx HTTP and reverse proxy server #Nginx服务描述信息
After=network.target remote-fs.target nss-lookup.target # Nginx服务启动依赖, 在指定服务之后启动
[Service] #记录service文件的service信息
Type=forking #标准UNIX Daemon使用的启动方式
PIDFile=logs/nginx.pid # Nginx服务的pid文件位置
ExecStartPre=/usr/bin/rm -f logs/nginx.pid #Nginx服务启动前,删除旧的pid文件
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q #Nginx服务启动前执行配置文件检测
ExecStart=/usr/local/nginx/sbin/nginx -g "pid logs/nginx.pid;" #启动Nginx服务
ExecReload=/usr/local/nginx/sbin/nginx -t -q #Nginx服务重启前执行配置文件监测
ExecReload=/usr/local/nginx/sbin/nginx -s reload -g "pid logs/nginx.pid;" #重启Nginx服务
ExecStop=/bin/kill -s HUP $MAINPID
systemctl enable nginx #开机启动
systemctl start nginx #启动服务
systemctl stop nginx #暂停服务
systemctl reload nginx #重启服务
systemctl status nginx #查看服务状态