Nginx 1.26.2 解压版安装与配置教程(适用于 CentOS 8)
CentOS 8 已经停止维护且官方仓库只有1.14
以下是通过下载并解压编译的方式来安装 Nginx 1.26.2 的完整教程。
步骤 1: 准备环境
首先,确保 CentOS 系统已安装所有必需的工具和库,特别是用于编译的软件包。
1.1 更新系统
在开始之前,建议更新系统包:
sudo dnf update -y
1.2 安装依赖项
为了能够编译 Nginx,需要安装开发工具集和依赖库。
sudo dnf groupinstall "Development Tools" -y
sudo dnf install pcre-devel zlib-devel openssl-devel -y
- pcre-devel:支持正则表达式(Nginx 配置文件解析时使用)。
- zlib-devel:用于 Gzip 压缩支持。
- openssl-devel:用于 SSL 加密。
步骤 2: 下载并解压 Nginx 1.26.2
从 Nginx 官方网站下载源代码并解压。
2.1 下载 Nginx 1.26.2
使用 wget
下载 Nginx 1.26.2 版本的源代码:
cd /usr/local/src
sudo wget https://nginx.org/download/nginx-1.26.2.tar.gz
2.2 解压源代码
下载完成后,解压缩文件:
sudo tar -zxvf nginx-1.26.2.tar.gz
cd nginx-1.26.2
步骤 3: 配置编译选项
在解压后的 Nginx 目录中,运行 ./configure
脚本来配置编译选项。
sudo ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_stub_status_module
配置说明:
--prefix=/usr/local/nginx
:指定 Nginx 的安装路径。--with-pcre
:启用 PCRE(Perl Compatible Regular Expressions)支持,通常用于 URL 解析等。--with-http_ssl_module
:启用 SSL/TLS 支持。--with-http_v2_module
:启用 HTTP/2 支持。--with-http_gzip_static_module
:启用静态资源的 Gzip 压缩。--with-http_stub_status_module
:启用 Nginx 状态监控模块。
步骤 4: 编译并安装 Nginx
完成配置后,执行 make
命令进行编译:
sudo make
编译完成后,执行 make install
进行安装:
sudo make install
步骤 5: 配置 Nginx 服务
为了方便地管理 Nginx,我们将其设置为 systemd
服务。
5.1 创建 systemd 服务文件
在 /etc/systemd/system/
下创建一个新的 Nginx 服务文件:
sudo nano /etc/systemd/system/nginx.service
将以下内容粘贴到文件中:
[Unit]
Description=The nginx web server
After=network.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PIDFile=/var/run/nginx.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
5.2 重新加载 systemd 配置
重新加载 systemd
配置并启用 Nginx 开机自启:
sudo systemctl daemon-reload
sudo systemctl enable nginx
5.3 启动 Nginx
启动 Nginx 服务:
sudo systemctl start nginx
步骤 6: 配置防火墙(忽略)
如果CentOS 使用 firewalld
进行防火墙管理,需要允许 HTTP 和 HTTPS 流量。
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --zone=public --add-service=https --permanent
sudo firewall-cmd --reload
步骤 7: 验证 Nginx 是否运行
检查 Nginx 服务是否已成功启动:
sudo systemctl status nginx
curl 127.0.0.1
如果 Nginx 正常运行,能够在浏览器中输入服务器的 IP 地址或域名,看到 Nginx 的欢迎页面。
步骤 8: 配置和管理 Nginx
8.1 编辑 Nginx 配置文件
Nginx 的配置文件位于 /etc/nginx/nginx.conf
,根据需要修改。配置文件示例如下:
sudo nano /etc/nginx/nginx.conf
常见的配置修改:
-
修改端口:
listen 80;
-> 修改为其他端口。 -
配置服务器块(虚拟主机):
server { listen 80; server_name your_domain_or_ip; location / { root /usr/local/nginx/html; index index.html index.htm; } }
8.2 测试配置文件
在修改配置文件后,可以使用以下命令测试 Nginx 配置文件的正确性:
sudo /usr/local/nginx/sbin/nginx -t
如果测试通过,您可以重新加载配置:
sudo systemctl reload nginx
步骤 9: 升级 Nginx
Nginx 是手动编译安装的,因此如果要升级到新版本,需要手动下载并编译新版本。新版本的安装步骤与此过程相同,只需要:
- 停止 Nginx 服务。
- 下载并解压新版本。
- 重新编译和安装。
评论区