插件窝 干货文章 CentOS8如何编译安装Nginx1.16.1

CentOS8如何编译安装Nginx1.16.1

Nginx bash sudo http 474    来源:    2025-05-11

CentOS 8 编译安装 Nginx 1.16.1 指南

准备工作

  1. 确保系统已更新:
sudo dnf update -y
  1. 安装必要的开发工具和依赖项:
sudo dnf groupinstall "Development Tools" -y
sudo dnf install pcre-devel zlib-devel openssl-devel wget -y

下载并编译 Nginx 1.16.1

  1. 下载 Nginx 1.16.1 源代码:
wget http://nginx.org/download/nginx-1.16.1.tar.gz
  1. 解压源代码包:
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
  1. 配置编译选项(可根据需要调整):
./configure \
--prefix=/usr/local/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
  1. 编译并安装:
make
sudo make install

配置系统服务

  1. 创建 nginx 用户和组:
sudo useradd -r -s /sbin/nologin nginx
  1. 创建 systemd 服务文件:
sudo vi /etc/systemd/system/nginx.service

添加以下内容:

[Unit]
Description=The nginx HTTP and reverse proxy 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
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
User=nginx
Group=nginx

[Install]
WantedBy=multi-user.target
  1. 重新加载 systemd 并启用服务:
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx

验证安装

  1. 检查 Nginx 版本:
/usr/local/nginx/sbin/nginx -v
  1. 检查服务状态:
sudo systemctl status nginx
  1. 访问默认页面(确保防火墙允许 HTTP 流量):
curl http://localhost

防火墙配置(如果需要)

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

后续配置

  1. 将 Nginx 可执行文件路径添加到 PATH:
echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> ~/.bashrc
source ~/.bashrc
  1. 配置文件位于:
/usr/local/nginx/conf/nginx.conf

现在你已经成功在 CentOS 8 上编译安装了 Nginx 1.16.1。