Nginx 是一款非常适合在现代服务器上使用的 Web 服务器软件。它不仅提供了高效的 HTTP 服务,还可以作为反向代理、负载均衡器和电子邮件代理。在 CentOS 9 Stream 上安装 Nginx 是一个简单的过程,下面我们将逐步介绍如何完成这一任务。
步骤 1: 更新系统
在开始安装 Nginx 之前,请确保您的系统是最新的。运行以下命令以更新 CentOS 9 Stream:
bash
sudo dnf update
步骤 2: 安装 Nginx
Nginx 通常在 CentOS 9 Stream 的默认软件库中可用。运行以下命令来安装 Nginx:
bash
sudo dnf install nginx
安装完成后,启用 Nginx 以便它在系统启动时自动启动:
bash
sudo systemctl enable nginx
然后启动 Nginx 服务,并验证其状态:
bash
sudo systemctl start nginx
sudo systemctl status nginx
最后,验证 Nginx 的版本:
bash
nginx -v
步骤 3: 配置防火墙
如果您的服务器受到防火墙保护,您需要允许 HTTP (80) 和 HTTPS (443) 流量通过。使用以下命令来配置防火墙:
bash
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
步骤 4: 验证 Nginx 安装
安装完成后,您可以通过在浏览器中访问您的服务器 IP 地址来验证 Nginx 是否正常工作:
bash
http://your-IP-address
如果一切顺利,您将看到默认的 Nginx 欢迎页面。
步骤 5: 了解 Nginx 配置文件的结构
虽然 Nginx 安装时不需要配置,但了解配置文件的位置和 Nginx 根目录是非常重要的。以下是一些关键信息:
- Nginx 配置目录:/etc/nginx
- Nginx 根目录:/usr/share/nginx/html
- 主/全局配置文件:/etc/nginx/nginx.conf
这些文件和目录将在您需要自定义 Nginx 配置时发挥作用。