源码编译安装Nginx
源码编译安装Nginx
如果需要最新版本
及定制化模块
可以通过源码
安装NGinx,新手推荐用yum
安装;
Nginx的LTS(长期支持)版本
,提供2年支持.只有第二位双数
版本是LTS版本
.
例如: 1.20.1 ; 1.22.0 ; 1.24.0
参考: 搞懂Nginx
参考: nginx从安装到高可用
创建nginx服务用户
# 设置时区为上海
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 创建nginx用户
## -r 不创建home目录
## -s 指定nologin 的shell
useradd -rs /sbin/nologin nginx
安装编译环境依赖包
# 安装编译环境依赖包
yum install -y gcc gcc-c++ make \
pcre pcre-devel zlib zlib-devel \
openssl openssl-devel
下载Nginx源码
nginx的LTS版本
,提供2年支持.只有第二位双数
版本是LTS版本.
例如: 1.20.1 ; 1.22.0 ; 1.24.0
# 安装wget
yum install -y wget
# 下载nginx源码
wget -cP /usr/local/src https://nginx.org/download/nginx-1.24.0.tar.gz
cd /usr/local/src
# 解压
tar xvf nginx-1.24.0.tar.gz
构建编译选项,创建makefile文件
参数 | 解释 |
---|---|
–prefix | 指定Nginx安装的目录 |
–pid-path | Nginx主进程的PID文件。 |
–lock-path | 指定Nginx互斥锁文件路径。用于防止同时启动多个 Nginx 进程。 |
–error-log-path | 指定Nginx错误日志的保存路径。 |
–http-log-path | 指定Nginx访问日志的保存路径 |
–with-http_gzip_static_module | 该模块允许 Nginx 对静态文件进行 Gzip 压缩,可以节省带宽 |
–with-http_ssl_module | http_ssl_module 用于启用 HTTPS 支持 |
–with-http_stub_status_module | stub_status 模块,可以查看 Nginx 的运行状态信息 |
–with-http_realip_module | 启用 HTTP 真实 IP 模块 |
–with-threads | 开启Nginx的线程支持。线程可以提高 Nginx 处理请求的并发能力。 |
–http-client-body-temp-path | 指定客户端请求 body 临时文件的存放位置。 |
–http-proxy-temp-path | 设定http代理临时目录 |
–http-fastcgi-temp-path | 设定fastcgi临时目录 |
–http-uwsgi-temp-path | 设定uwsgi临时目录 |
–http-scgi-temp-path | 设定scgi临时目录 |
cd nginx-1.24.0
# 创建makefile文件
# 编译 执行命令 考虑到后续安装ssl证书 添加下面7个常用模块 如不需要直接执行./configure即可
./configure \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-threads \
--with-http_v2_module \
--with-http_sub_module
# 指定最常用的5个模块
./configure \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads
# 查看Nginx支持的模块
nginx -V 2>&1 | grep 'configure arguments'
编译安装nginx
# 编译安装nginx
make && make install
为Nginx创建服务单元
# 新增自定义一个 nginx.service 文件
vi /etc/systemd/system/nginx.service
nginx服务单元内容如下:
[Unit]
# 服务描述信息,用于标识服务用途(建议与进程名一致)
Description=The nginx HTTP and reverse proxy server
# 定义服务启动顺序依赖:在 network.target 初始化完成后启动
After=network.target
# 弱依赖
Wants=network-online.target
[Service]
# 服务类型:适用于通过 fork() 创建子进程后父进程立即退出的守护进程(如 Nginx)
Type=forking
# 指定pid文件
PIDFile=/run/nginx.pid
# 启动前删除pid文件
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
# 启动nginx前测试配置文件语法
ExecStartPre=/usr/local/nginx/sbin/nginx -t
# 服务启动命令(如果配置文件在默认路径 /usr/local/nginx/conf/nginx.conf,可省略 -c 参数)
ExecStart=/usr/local/nginx/sbin/nginx
# 显式指定配置文件路径的启动方式(按需取消注释)
# ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 服务重载命令:向主进程发送 HUP 信号重新加载配置
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# 服务停止命令:向主进程发送 TERM 信号优雅停止
ExecStop=/usr/local/nginx/sbin/nginx -s stop
# 优雅停止信号,主进程崩溃时生成调试信息
KillSignal=SIGQUIT
# 确保服务在 5 秒内终止
TimeoutStopSec=5
# 仅终止主进程(子进程需自行清理)
KillMode=process
# 启用私有临时目录空间隔离,增强安全性(防止 /tmp 目录信息泄露)
PrivateTmp=true
[Install]
# 定义服务安装目标:当系统进入多用户模式(常规运行状态)时自动启动
WantedBy=multi-user.target
设置Nginx开机自启服务
# 重载
systemctl daemon-reload
# 开机自启动并现在启动
systemctl enable --now nginx
# 查看nginx服务状态
systemctl status nginx
yum安装Nginx
# 安装全部模块的ngninx
yum install nginx-all-modules -y
# 开机自启动并现在启动
# 开机自启动并现在启动服务
systemctl enable --now nginx
yum安装openresty
参考官方文档:https://openresty.org/cn/linux-packages.html#rhel
# 下载openresty的yum源
curl -o /etc/yum.repos.d/openresty.repo https://openresty.org/package/rhel/openresty.repo
# yum安装openresty
yum install -y openresty
# 开机自启动并现在启动服务
systemctl enable --now openresty
openresty
实际上是nginx
的软链接
参考:https://tinychen.com/20210317-openresty-01-introduction-and-installation/