下载nginx软件包:nginx-1.22.1.tar.gz
# 解压nginx软件包
tar -zxvf nginx-1.22.1.tar.gz -C /usr/local/src
cd /usr/local/src/nginx-1.22.1/
# 安装编译环境
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel
# 预编译
./configure --prefix=/usr/local/nginx
# 编译安装
make
make install
注意:启动服务前修改一下配置文件(80端口被占用),修改nginx的端口为8081。同时关闭firewalld和selinux。
# 修改配置文件端口
vim /usr/local/nginx/conf/nginx.conf
# 进入启动目录,启动服务
cd /usr/local/nginx/sbin
./nginx
# 快速停止
./nginx -s stop
# 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s quit
# 重新加载配置
./nginx -s reload
① 编辑服务文件:vim /usr/lib/systemd/system/nginx.service
(输入a)
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
② 加载服务并重启
# 重新加载系统服务
systemctl daemon-reload
# 查看之前的nginx服务进程,将其干掉
ps -ef | grep nginx
kill -9 xxxx
# 重启服务
systemctl restart nginx
# 设置开机自启动
systemctl enable nginx
# 查看nginx状态
systemctl status nginx
# 下载软件
wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar -zxvf nginx-1.22.0.tar.gz -C /usr/local/src
cd /usr/local/src/nginx-1.22.0/
# 安装nginx依赖包
yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
vim src/core/nginx.h
vim src/http/ngx_http_header_filter_module.c
vim src/http/ngx_http_special_response.c
# 第29行
"
web " CRLF
"
" CRLF "