❗ 本文用户的用户组被设置为 root 。

Linux-Nginx建站

准备工作

安装 Nginx

1
sudo apt update && sudo apt install nginx

配置防火墙

1
2
3
4
5
6
7
sudo ufw allow 'Nginx Full'         # 允许 Nginx 全部流量

# 其他常用规则
sudo ufw status # 展示防火墙规则
sudo ufw status numbered # 展示防火墙规则编号
sudo ufw allow <port>/tcp # 允许某个端口的 TCP 流量
sudo ufw delete <num> # 删除规则

添加完 Nginx 规则后,应该可以访问网站了。

建立网页

创建网页文件

使用下述命令创建文件,并将文本复制进去后,即可创建一个最简单的网页文件。

1
2
3
mkdir -p /var/www/test && nano /var/www/test/index.html
chmod -R a+r /var/www
sudo systemctl reload nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<html lang="">
<!-- Text between angle brackets is an HTML tag and is not displayed.
Most tags, such as the HTML and /HTML tags that surround the contents of
a page, come in pairs; some tags, like HR, for a horizontal rule, stand
alone. Comments, such as the text you're reading, are not displayed when
the Web page is shown. The information between the HEAD and /HEAD tags is
not displayed. The information between the BODY and /BODY tags is displayed.-->
<head>
<title>Enter a title, displayed at the top of the window.</title>
</head>
<!-- The information between the BODY and /BODY tags is displayed.-->
<body>
<h1>Enter the main heading, usually the same as the title.</h1>
<p>Be <b>bold</b> in stating your key points. Put them in a list:</p>
<ul>
<li>The first item in your list</li>
<li>The second item; <i>italicize</i> key words</li>
</ul>
<p>Improve your image by including an image.</p>
<p>
<img src="https://i.imgur.com/SEBww.jpg" alt="A Great HTML Resource" />
</p>
<p>
Add a link to your favorite
<a href="https://www.dummies.com/">Web site</a>. Break up your page
with a horizontal rule or two.
</p>
<hr />
<p>
Finally, link to <a href="page2.html">another page</a> in your own Web
site.
</p>
<!-- And add a copyright notice.-->
<p>&#169; Wiley Publishing, 2011</p>
</body>
</html>

配置 Nginx

1
2
sudo nano /etc/nginx/nginx.conf
sudo systemctl reload nginx

将下方文本添加在 http{} 内。

1
2
3
4
5
6
server {
listen 80;
server_name 二级域名.你的域名.顶级域名;
root /var/www/test;
index index.html;
}

现在访问网站应该可见如下网页。

❗ 如果出现错误 403 ,有可能是文件权限设置错误。