Nginx安装
2022年12月17日大约 4 分钟
版本简介
Nginx 开源版: 只有基础功能,即虚拟主机、反向代理、负载均衡这三大功能
Nginx Plus 商业版:收费,对云原生有很好的支持,如方便整合 k8s
Openresty: 开源免费,将 Nginx 和 Lua 脚本进行了完美的融合,中文官网学习方便(以 Lua 脚本语言的方式增强 nginx 功能)
Tengine: 阿里旗下,开源免费,中文官网学习方便(以 c 语言的方式增强 nginx 功能)
Openresty 和 Tengine 这两个版本是目前主流版本
常用命令
Linux
# 重新加载的配置文件(不中断服务,仅重载配置)
sudo systemctl reload nginx
# 重启 Nginx(完全重启,会短暂中断服务)
sudo systemctl restart nginx
# 检查 nginx 配置文件是否有误
nginx -tWindows
nginx -s reload # 重新加载的配置文件
nginx -s stop # 停止 Nginx
nginx # 启动 Nginx
# 检查 nginx 配置文件是否有误
nginx -tNginx 安装/卸载
# 查看 Linux 系统版本
cat /etc/os-release在 Liunx 中安装 -- 软件包安装(推荐)
- nginx 配置文件路径: /etc/nginx/xxx
- nginx 可执行文件路径:/usr/sbin/nginx
Ubuntu/Debian (apt)
sudo apt update
# 安装 Nginx
sudo apt install nginx
# 临时关闭 SELinux;解决启动 nginx 可能绑定端口权限不足
setenforce 0
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
# 检查运行状态
sudo systemctl status nginxcentos7
# 添加 EPEL 仓库
sudo yum install epel-release
# 安装 Nginx
sudo yum install nginx
# 临时关闭 SELinux;解决启动 nginx 可能绑定端口权限不足
setenforce 0
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
# 检查运行状态
sudo systemctl status nginxCentOS 8+ / RHEL
# 添加 EPEL 仓库
sudo dnf install epel-release -y
# 安装 Nginx
sudo dnf install nginx
# 临时关闭 SELinux;解决启动 nginx 可能绑定端口权限不足
setenforce 0
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
# 检查运行状态
sudo systemctl status nginx在 Liunx 中安装 -- 源码编译安装
下载编译包 nginx-1.27.3.tar.gz
解压编译 - 适用于 CentOS
简单安装
# 解压
tar zxvf nginx-1.27.3.tar.gz
# 编译前需额外安装下面的库
yum install -y gcc # gcc库
yum install -y pcre pcre-devel # perl库
yum install -y zlib zlib-devel # zlib库
# 进入解压目录,编译安装
cd nginx-1.27.3
# 配置 Nginx 的编译选项;
# --prefix 指定安装根目录;默认 /usr/local/nginx
# --conf-path 指定配置文件路径;默认读取 /etc/nginx/nginx.conf,需要手动更改一下
./configure --prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf
# 编译 + 构建
make && make install详细控制 Nginx安装
# 解压
tar zxvf nginx-1.27.3.tar.gz
# 编译前需额外安装下面的库
yum install -y gcc # gcc库
yum install -y pcre pcre-devel # perl库
yum install -y zlib zlib-devel # zlib库
# 进入解压目录,编译安装
cd nginx-1.27.3
# 配置 Nginx 的编译选项;下面是参数说明:
# 简单安装,只需要指定一下 --prefix 和 --conf-path 即可。其他保持默认
#
# @ 必要配置
# --prefix 指定安装根目录;默认 /usr/local/nginx
# --conf-path 指定配置文件路径;默认读取 /etc/nginx/nginx.conf,需要手动更改一下
#
# @ 其他路径配置;以下的 xxx-path 默认都会从 /usr/local/nginx/xxx 中读取,所以可忽略配置
# --sbin-path 指定 nginx 可执行文件的路径;
# --error-log-path 指定错误日志路径
# --pid-path 指定 Nginx PID 文件的路径
#
# @ 启用模块功能
# --with-http_ssl_module 启用 HTTPS/SSL 支持;默认关闭
# --with-http_v2_module 启用 HTTP/2 支持;默认关闭
# --with-http_realip_module 启用 获取客户端的真实 IP 地址;默认关闭
# --with-threads 启用 多线程支持;默认关闭
# --with-file-aio 启用 异步文件 I/O;默认关闭
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--sbin-path=/usr/local/nginx/sbin/nginx \
--error-log-path=/usr/local/nginx/logs/error.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-threads \
--with-file-aio
# 编译 + 构建
make && make install- 启动/重启 nginx
# 临时关闭 SELinux;解决启动 nginx 可能绑定端口权限不足
setenforce 0
# 启动
cd /usr/local/nginx
./nginx
# 关闭
./nginx -s stop
# 重载配置
systemctl reload nginxdocker-compose 安装
卸载 Nginx
此方式适合卸载包管理器安装的 Nginx,对于源码编译安装,删除安装目录即可。
Ubuntu/Debian (apt)
# 停止 Nginx 服务
sudo systemctl stop nginx
sudo systemctl disable nginx # 禁止开机自启
# 卸载
sudo apt purge nginx nginx-common nginx-core # 完全卸载
sudo apt autoremove # 清理依赖
# 删除残留文件
sudo rm -rf /etc/nginx/ # 配置文件
sudo rm -rf /var/log/nginx # 日志文件
sudo rm -rf /var/www/html # 默认网站目录(如果有)
# 验证 Nginx 是否卸载成功
nginx -v # 如果显示 "command not found" 说明卸载成功CentOS/RHEL (yum/dnf)
# 停止 Nginx 服务
sudo systemctl stop nginx
sudo systemctl disable nginx # 禁止开机自启
# 卸载
sudo yum remove nginx # CentOS 7
sudo dnf remove nginx # CentOS 8+/RHEL
# 删除残留文件
sudo rm -rf /etc/nginx/ # 配置文件
sudo rm -rf /var/log/nginx # 日志文件
sudo rm -rf /var/www/html # 默认网站目录(如果有)
# 验证 Nginx 是否卸载成功
nginx -v # 如果显示 "command not found" 说明卸载成功
