rocky 安装Nginx和基本操作

在 Rocky Linux 上安装 Nginx 的步骤如下： 
 1. 更新系统 
 确保系统是最新的： 
 sudo dnf update -y
 
 2. 安装 Nginx 
 Rocky Linux 默认仓库包含 Nginx，可以直接安装： 
 sudo dnf install nginx -y
 
 3. 启动并设置开机自启 
 sudo systemctl start nginx
sudo systemctl enable nginx
 
 4. 检查 Nginx 状态 
 sudo systemctl status nginx
 
 如果看到 active (running) ，说明 Nginx 已成功运行。 
 5. 配置防火墙（如果启用） 
 允许 HTTP (80) 和 HTTPS (443) 流量： 
 sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
 
 6. 测试访问 
 在浏览器输入服务器的 IP 地址，应该能看到 Nginx 默认欢迎页面： 
 http://你的服务器IP
 
 （可选）调整 Nginx 配置 
 主配置文件位于 /etc/nginx/nginx.conf ，网站配置文件通常在 /etc/nginx/conf.d/ 或 /etc/nginx/sites-available/ （需手动创建）。 
 修改配置后，测试并重载 Nginx： 
 sudo nginx -t # 检查配置语法
sudo systemctl reload nginx # 重新加载配置
 
 这样，Nginx 就已经在 Rocky Linux 上安装并运行了！ 🚀 
 Nginx 基本操作 
 1. 启动、停止、重启 Nginx 
 sudo systemctl start nginx # 启动
sudo systemctl stop nginx # 停止
sudo systemctl restart nginx # 重启（强制重新加载）
sudo systemctl reload nginx # 平滑重载（不中断服务，仅重新加载配置）
 
 2. 查看 Nginx 状态 
 sudo systemctl status nginx # 查看运行状态
sudo nginx -t # 检查配置文件语法（测试配置是否正确）
 
 3. 设置开机自启 
 sudo systemctl enable nginx # 开机自动启动
sudo systemctl disable nginx # 取消开机自启
 
 4. 查看 Nginx 版本 
 nginx -v # 查看版本
nginx -V # 查看详细版本及编译参数
 
 5. 检查 Nginx 进程 
 ps aux | grep nginx # 查看 Nginx 进程
 
 6. 日志查看 
 Nginx 日志默认位置： 
 
 访问日志： /var/log/nginx/access.log 
 错误日志： /var/log/nginx/error.log 
 
 实时查看日志： 
 sudo tail -f /var/log/nginx/access.log # 实时监控访问日志
sudo tail -f /var/log/nginx/error.log # 实时监控错误日志
 
 7. 配置文件管理 
 
 主配置文件 ： /etc/nginx/nginx.conf 
 站点配置 ：通常在 /etc/nginx/conf.d/ 或 /etc/nginx/sites-available/ （需手动创建） 
 
 修改配置后，测试并重载： 
 sudo nginx -t # 测试配置是否正确
sudo systemctl reload nginx # 重新加载配置（不中断服务）
 
 8. 虚拟主机（Server Block）配置示例 
 在 /etc/nginx/conf.d/example.conf 添加： 
 server {
 listen 80;
 server_name example.com www.example.com;
 root /var/www/example;
 index index.html;

 location / {
 try_files $uri $uri/ =404;
 }
}
 
 然后测试并重载： 
 sudo nginx -t && sudo systemctl reload nginx
 
 9. 卸载 Nginx 
 sudo dnf remove nginx -y # 卸载 Nginx
sudo rm -rf /etc/nginx/ # 删除配置文件（谨慎操作！）
 
 掌握这些基本操作，就可以管理 Nginx 服务了！ 🚀