更新时间:2023 年 7 月
简介
Nginx 官网:nginx
Nginx(读音:engine x)是一款简单易用的 HTTP 服务器,也可以作为反向代理服务器。其支持四层、七层代理(1.9.0 版本开始支持四层代理),适用于 HTTP 代理、邮件代理、通用 TCP/UDP 代理等场景。其稳定性、丰富的模块库、灵活的配置和低资源占用在业界有极高美誉
Nginx 的版本
nginx 各版本下载:nginx: download
-
mainline version
主线版本,版本号通常是奇数,目前最新但是还未经过大量测试的版本
-
stable version(推荐使用)
最新稳定版本,版本号通常为偶数,经过大量测试,企业中经常使用该版本
-
lagacy version(推荐使用)
历史稳定版本,往期的稳定版本
Nginx 安装方式
- DNF/YUM 安装:适用于可连接互联网的主机,默认安装一些常用插件(可以用 nginx -V 查看插件)
- 编译安装:可以自定义目录、自定义插件和功能
除非需要自定义插件和功能,一般选择 DNF/YUM 安装
安装
安装过程参考:Installing nginx
准备
主机 | IP | 操作系统 | 安装服务 |
---|---|---|---|
nginx.skynemo.cn | 192.168.1.191 | AlmaLinux release 9.2 Minimal Install | nginx |
设置主机名
# 重新登录会话生效
$ hostnamectl set-hostname nginx.skynemo.cn
关闭防火墙和 selinux
# 关闭防火墙 firewall 并设置开机不启动
$ systemctl disable --now firewalld
# 关闭 selinux 并设置开机不启动
$ setenforce 0
$ sed -i 's/=enforcing/=disabled/g' /etc/sysconfig/selinux
配置 aliyun 的 yum 仓库(可选)
配置参考:almalinux 镜像
# 备份
$ ls /etc/yum.repos.d/*.repo | awk '{print "cp "$0,$0".bak"}' | sh
# 替换镜像地址配置
$ sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^# baseurl=https://repo.almalinux.org|baseurl=https://mirrors.aliyun.com|g' \
-i.bak \
/etc/yum.repos.d/almalinux*.repo
#运行 yum makecache 生成缓存
$ dnf makecache
安装 vim、yum-utils、wget(可选)
$ dnf install vim yum-utils wget -y
方式一:DNF 安装(推荐)
配置 nginx 的 yum 仓库
# 此处的配置默认使用 stable 版本,根据需要安装的版本修改 enabled
$ vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
安装稳定版本
# 查看版本
$ dnf list nginx.x86_64 --showduplicates | sort -r
nginx.x86_64 1:1.24.0-1.el9.ngx nginx-stable
nginx.x86_64 1:1.22.1-1.el9.ngx nginx-stable
nginx.x86_64 1:1.22.0-1.el9.ngx nginx-stable
nginx.x86_64 1:1.20.2-1.el9.ngx nginx-stable
nginx.x86_64 1:1.20.1-14.el9.alma appstream
......
# 默认配置即为稳定版本,直接安装即可
$ dnf install nginx -y
# 或者:安装指定版本
$ dnf -y install nginx-1.24.0-1.el9.ngx.x86_64
# 查看版本信息
$ nginx -v
nginx version: nginx/1.24.0
# 启动 nginx,并设置开机启动
$ systemctl enable --now nginx
安装主线版本(生产中不推荐)
# 卸载刚才安装的稳定版本
$ yum remove nginx -y
# 配置 yum 仓库文件(/etc/yum.repos.d/nginx.repo)的 mainline 版本为 enabled
# 也可以 vim 配置文件进行修改
$ yum-config-manager --enable nginx-mainline
# 安装
$ yum install nginx -y
# 查看版本信息
$ nginx -v
nginx version: nginx/1.25.1
# 启动 nginx,并设置开机启动
$ systemctl enable --now nginx
方式二:源码安装
configure
与 make
configure
通常是软件自带的脚本,主要是用来检查系统环境是否满足安装软件包的条件,并根据当前系统环境和配置并生成 Makefile
文件,该文件为编译、安装、升级 nginx 指明了相应参数
make
是一个通用的编译安装工具,可以根据 configure
生成的 Makefile
文件,编译、安装软件或清理过程中产生的文件
Nginx 的 configure
脚本运行格式
./configure [options]
# 在软件目录下执行 configure --help ,可以查看所有 configure 参数和详细解释
$ ./configure --help
# nginx 常用 configure 选项如下:
--prefix # 指定 nginx 编译安装的目录;
--user=*** # 指定 nginx 软件的属主
--group=*** # 指定 nginx 软件的属组
--with-*** # 指定编译某模块
--without-** # 指定不编译某模块
--add-module # 编译第三方模块
Nginx 常用 configure
选项
# 常用格式如下(添加了一些常用模块)
./configure --prefix=/application/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
make 命令格式
make [options]
# 常用的 make 选项如下
make clean # 重新预编译时,通常执行这条命令删除上次的编译文件
make build # 编译,可省略 build 参数
make install # 安装
make modules # 编译模块
make upgrade # 在线升级
查看Makefile文件,可以看到编译命令 make 的常用参数以及作用
$ cat Makefile
default: build
clean:
rm -rf Makefile objs
.PHONY: default clean
build:
$(MAKE) -f objs/Makefile
install:
$(MAKE) -f objs/Makefile install
modules:
$(MAKE) -f objs/Makefile modules
upgrade:
/usr/sbin/nginx -t
kill -USR2 `cat /var/run/nginx.pid`
sleep 1
test -f /var/run/nginx.pid.oldbin
kill -QUIT `cat /var/run/nginx.pid.oldbin`
.PHONY: build install modules upgrade
安装
创建安装目录及用户
# 创建 nginx 运行用户
$ groupadd -r nginx
$ useradd -r -g nginx -s /sbin/nologin nginx
# 创建软件源码存放目录
$ mkdir -p /application/src
# 创建软件存放目录
$ mkdir -p /application/nginx
安装依赖
$ dnf install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
下载解压源码包
下载链接地址:nginx: download
# 指定版本
$ NGX_VERSION=1.24.0
# 复制下载链接,wget 下载
$ wget http://nginx.org/download/nginx-${NGX_VERSION}.tar.gz -O /application/src/nginx-${NGX_VERSION}.tar.gz
# 解压到 /usr/local/src
$ tar -xf /application/src/nginx-${NGX_VERSION}.tar.gz -C /application/src/
编译并安装 Nginx
# 查看目录
$ ls /application/src/nginx-${NGX_VERSION}/
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
# 进入软件路径,运行 configure 配置
$ cd /application/src/nginx-${NGX_VERSION}/
# 配置,指定 html 默认路径 /application/nginx,指定 nginx.conf 在 /etc/nginx/,等配置
$ ./configure --prefix=/application/nginx \
--user=nginx \
--group=nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--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
# 再次查看目录,可以发现多了 Makefile 文件以及 objs 目录
$ ls /application/src/nginx-${NGX_VERSION}/
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
# make 编译安装
$ make && make install
启动 Nginx
# 查看版本
$ nginx -v
nginx version: nginx/1.24.0
# 可以执行命令启动 nginx,不推荐,一般使用 systemd 管理
$ nginx
# 检查进程
$ ps -ef | grep nginx
root 24921 1 0 00:27 ? 00:00:00 nginx: master process nginx
nginx 24922 24921 0 00:27 ? 00:00:00 nginx: worker process
root 24924 13746 0 00:27 pts/0 00:00:00 grep --color=auto nginx
其他设置
加入systemd管理
# 源码安装需要手动添加至systemd管理
$ vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
# 若要指定 pidfile,则 nginx.conf 中应该配置同一个 pidfile,且 Nginx 启动用户对该文件具有读写权限
# 否则会导致 systemctl start nginx 卡住
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target
# 重新加载 systemd
$ systemctl daemon-reload
# 用 systemctl 运行,并设置开机启动
$ systemctl enable --now nginx
卸载
DNF 安装的卸载
$ dnf remove nginx -y
源码安装的卸载
# 查看 nginx 是否启动
$ ps -ef | grep nginx
root 18057 1 0 11:05 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 18058 18057 0 11:05 ? 00:00:00 nginx: worker process
root 18060 1526 0 11:05 pts/0 00:00:00 grep --color=auto nginx
# 停止 nginx 服务
$ nginx -s quit
# 或者
$ systemctl disable --now nginx
# 查看 nginx 服务是否在运行
$ ps -ef | grep nginx
root 20534 20222 0 09:39 pts/1 00:00:00 grep --color=auto nginx
# 删除相应的目录即可
$ rm -rf /etc/nginx
$ rm -rf /application/nginx*
常用命令
查看帮助
# 查看帮助
$ nginx -h
nginx version: nginx/1.24.0
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-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: /application/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
Nginx 自带命令
# 启动 nginx
# 除了测试外,生产中 nginx 的启动停止一般使用 systemd 管理,不使用命令
$ nginx
# 启动并指定配置文件
$ nginx -c [conf file]
# 立即停止 nginx
$ nginx -s stop
# 优雅停止 nginx
$ nginx -s quit
# 重新打开日志文件
$ nginx -s reopen
# 检查配置文件
$ nginx -t [conf file]
# 重新加载配置文件
$ nginx -s reload
# 查看 nginx 的详细信息(包括插件)
$ nginx -V
# 设置全局变量(覆盖配置文件的值)
# 通过设置全局变量,让nginx在前端运行
$ nginx -g "daemon off;"
# 此时 ctrl +c,则 nginx 就退出了。可以使用 ctrl + z 放置后台运行
使用 systemd 管理控制 nginx
# 启动 nginx
$ systemctl start nginx
# 重启 nginx
$ systemctl restart nginx
# 重新加载配置文件
$ systemctl reload nginx
# 设置开机启动
$ systemctl enable nginx
# 查看 nginx 运行状态
$ systemctl status nginx