在Rocky Linux 9上安装Nginx的方法是什么?
引言
Nginx是世界上最受欢迎的Web服务器之一,负责托管一些互联网上最大和最高流量的站点。它是一个轻量级选择,可以用作Web服务器或反向代理。
在本指南中,您将学习如何在Rocky Linux 9服务器上安装Nginx,调整防火墙,管理Nginx进程,并为从单个服务器上托管多个域名设置服务器块。
先决条件
在开始本指南之前,您应该在您的服务器上配置一个具备sudo权限的常规非root用户。您可以通过按照我们的Rocky Linux 9初始服务器设置指南来学习如何配置常规用户帐户。
在完成本教程的最后几个步骤之前,您还可以选择先注册一个域名。如果想了解如何在Silicon Cloud上配置域名,请参考我们的《Silicon Cloud DNS简介》。
当您拥有可用的账户时,请使用非root用户登录开始操作。
第一步-安装Nginx
因为Nginx在Rocky的默认软件库中可用,您可以使用dnf软件包管理器的单个命令进行安装。
使用dnf install 安装nginx软件包。
- sudo dnf install nginx
当提示时,输入y以确认您希望安装nginx。之后,dnf将在您的服务器上安装Nginx及其所需的依赖项。
安装完成后,运行以下命令来启用并启动网络服务器。
- sudo systemctl enable nginx
- sudo systemctl start nginx
当您的服务器重新启动时,这将使Nginx自动重新启动。您的新网络服务器现在应该已经启动并运行,但在测试之前,您可能需要对防火墙配置进行更改。
第二步-调整防火墙。
如果您在Rocky Linux 9的初始服务器设置向导中启用了firewalld防火墙,您需要调整防火墙设置以允许在默认端口80上运行的Nginx Web服务器进行外部连接。
运行下面的命令来永久启用端口80上的HTTP连接:
- sudo firewall-cmd –permanent –add-service=http
为了验证是否正确添加了HTTP防火墙服务,您可以运行:
- sudo firewall-cmd –permanent –list-all
您将看到这样的输出:
public target: default icmp-block-inversion: no interfaces: sources: services: cockpit dhcpv6-client http ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
为了应用这些更改,您需要重新加载防火墙服务。
- sudo firewall-cmd –reload
你的网页服务器现在可以被外部访客访问了。
第三步- 检查您的Web服务器。
此时,您的网络服务器应该已经启动运行。
您可以使用systemctl status命令来确保服务正在运行。
- systemctl status nginx
● nginx.service – The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2022-09-14 21:03:46 UTC; 7min ago Process: 18384 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Process: 18385 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 18386 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Main PID: 18387 (nginx) Tasks: 3 (limit: 10938) Memory: 2.8M CPU: 43ms CGroup: /system.slice/nginx.service ├─18387 “nginx: master process /usr/sbin/nginx” ├─18388 “nginx: worker process” └─18389 “nginx: worker process”
通过此输出所证实,服务已成功启动。然而,测试最佳方法是实际向Nginx请求页面。
你可以通过访问服务器的IP地址来确认Nginx软件正常运行并访问默认的Nginx首页。如果你不知道服务器的IP地址,你可以使用icanhazip.com这个工具来找到它,该工具将提供从互联网其他地方接收到的公共IP地址。
- curl -4 icanhazip.com
当你获得服务器的IP地址后,将其输入到你浏览器的地址栏中。
http://your_server_ip
你应该看到默认的Nginx启动页。
![Nginx default page](https://cdn.silicloud.com/blog-img/blog/img/6564632ba4b2f92e6c725964/37-0.png)
如果您在此页面上,表示您的服务器正在正常运行,并且已准备好进行管理。
步骤4 – 管理Nginx进程
现在您的网络服务器已经启动并运行,让我们来回顾一些服务管理命令。
停止网络服务器的方法是使用systemctl stop。
- sudo systemctl stop nginx
当网页服务器停止时,使用systemctl start来启动。
- sudo systemctl start nginx
要停止然后重新启动服务,请使用systemctl restart命令。
- sudo systemctl restart nginx
如果您只是进行配置更改,Nginx通常可以在不断开连接的情况下重新加载。要实现这一点,请使用systemctl reload命令。
- sudo systemctl reload nginx
在本教程的早些时候,您已经配置了 Nginx 在服务器启动时自动启动。您可以使用 systemctl disable 来禁用此行为。
- sudo systemctl disable nginx
要重新启用在开机时启动的服务,您可以输入:
- sudo systemctl enable nginx
第五步 – 了解重要的Nginx文件和目录
既然您已经知道如何管理Nginx服务,您应该花几分钟时间熟悉一些重要的目录和文件。
内容
- /usr/share/nginx/html: The actual web content, which by default only consists of the default Nginx page you saw earlier, is served out of the /usr/share/nginx/html directory. This can be changed by altering the Nginx configuration files.
服务器配置
- /etc/nginx: The Nginx configuration directory. All of the Nginx configuration files reside here.
- /etc/nginx/nginx.conf: The main Nginx configuration file. This can be modified to make changes to the Nginx global configuration.
- /etc/nginx/conf.d/: This directory contains server block configuration files, where you can define the websites that are hosted within Nginx. A typical approach is to have each website in a separate file that is named after the website’s domain name, such as your_domain.conf.
服务器日志
- /var/log/nginx/access.log: Every request to your web server is recorded in this log file unless Nginx is configured to do otherwise.
- /var/log/nginx/error.log: Any Nginx errors will be recorded in this log.
现在你应该准备好配置网站以托管一个或多个域名了。
步骤6 – 设置服务器块(可选)
在使用Nginx网络服务器时,可以使用服务器块(类似于Apache的虚拟主机)来组织配置细节并从单个服务器托管多个域。在Rocky Linux 9上,服务器块在位于/etc/nginx/conf.d的.conf文件中定义。我们将设置一个名为your_domain的域,但您应该用自己的域名替换它。
默认情况下,Rocky Linux 9上的Nginx配置为从目录 /usr/share/nginx/html 提供文档。虽然对于单个站点来说这很有效,但是如果您要托管多个站点,这将变得难以管理。相对于修改 /usr/share/nginx/html,您将在 /var/www 内创建一个目录结构,以用于 your_domain 网站,同时保留 /usr/share/nginx/html 作为默认目录,在没有其他站点匹配客户端请求时提供服务。
按照以下方式创建您的域名目录,使用-p标志创建必要的父目录:
- sudo mkdir -p /var/www/your_domain/html
接下来,使用$USER环境变量将目录的所有权分配给当前系统用户。
- sudo chown -R $USER:$USER /var/www/your_domain/html
现在您将创建一个示例index.html页面来测试服务器块配置。Rocky Linux 9附带的默认文本编辑器是vi。vi是一个非常强大的文本编辑器,但对于缺乏经验的用户来说可能有些晦涩。您可能希望安装一个更用户友好的编辑器,比如nano,以便在Rocky Linux 9服务器上编辑配置文件。
- sudo dnf install nano
接下来,使用nano或者你喜欢的编辑器创建一个样本index.html页面。
- nano /var/www/your_domain/html/index.html
在内部,添加以下示例HTML代码:
/主机/你的域名/网页目录/index.html
<html>
<head>
<title>Welcome to your_domain</title>
</head>
<body>
<h1>Success! Your Nginx server is successfully configured for <em>your_domain</em>. </h1>
<p>This is a sample page.</p>
</body>
</html>
如果你完成了,保存并关闭文件。如果你正在使用nano,你可以通过按下CTRL + X保存并退出,然后在提示时按Y,然后按Enter键。
为了使Nginx提供此内容,您需要创建一个服务器块,其中的指令指向您的自定义网站根目录。在/etc/nginx/conf.d/your_domain.conf上创建一个新的服务器块。
- sudo nano /etc/nginx/conf.d/your_domain.conf
粘贴以下配置块:
server {
listen 80;
listen [::]:80;
root /var/www/your_domain/html;
index index.html index.htm index.nginx-debian.html;
server_name your_domain www.your_domain;
location / {
try_files $uri $uri/ =404;
}
}
注意我们已将根配置更新为新的目录,并将服务器名称更改为我们的域名。保存并关闭文件。
现在已经启用并配置了两个服务器块,根据它们的监听和服务器名称指令来响应请求(您可以在这里阅读有关Nginx如何处理这些指令的更多信息)。
- your_domain: Will respond to requests for your_domain and www.your_domain.
- default: Will respond to any requests on port 80 that do not match the other two blocks.
接下来,使用 nginx -t 命令来测试确保你的所有 Nginx 文件中没有语法错误。
- sudo nginx -t
如果没有任何问题,请重新启动Nginx以启用您的更改。
- sudo systemctl restart nginx
在您可以从浏览器测试更改之前,您需要更新服务器的 SELinux 安全上下文,以使 Nginx 被允许从 /var/www/your_domain 目录提供内容。
此chcon上下文更新将允许您的自定义文档根目录作为HTTP内容进行提供。
- chcon -vR system_u:object_r:httpd_sys_content_t:s0 /var/www/your_domain/
现在Nginx应该正在为您的域名提供服务。您可以通过导航到http://your_domain来测试。在那里,您应该能够看到类似以下的内容:
结论
现在你已经安装了网络服务器,你有很多选择可以提供内容的类型以及使用的技术来创建更丰富的体验。
要为您的域名设置HTTPS并使用Let’s Encrypt的免费SSL证书,您应该转到如何在Rocky Linux 9上使用Let’s Encrypt来保护Nginx。