如何在 Debian 11 上使用 Hugo 创建网站
在此页
- 先决条件
- 在 Debian 11 上安装 Hugo
- 使用 Hugo 创建网站
- 创建一个关于页面和示例帖子
- 在您的网站上安装主题
- 建立并启动您的网站
- 访问雨果网站
- 结论
Hugo 是一个用 Go 开发的免费开源网站框架。 Hugo 提供可靠且现代的静态站点生成器,可让您轻松创建简单快速的网站。它带有预制模板和其他功能,包括 SEO、评论、分析和其他功能。 Hugo 站点可以在没有任何昂贵的运行时间(如 PHP、Python、Ruby)的情况下运行,并且不需要任何数据库。
在这篇文章中,我们将向您展示如何在 Debian 11 上安装和使用 Hugo 站点生成器。
先决条件
- 运行 Debian 11 的服务器。
- 在服务器上配置了根密码。
在 Debian 11 上安装 Hugo
默认情况下,Hugo 包包含在 Debian 11 默认存储库中。您可以使用以下命令安装它:
apt-get install hugo -y
安装 Hugo 后,您可以使用以下命令验证 Hugo 版本:
hugo version
您应该得到以下输出:
Hugo Static Site Generator v0.80.0/extended linux/amd64 BuildDate: 2021-07-18T09:31:51Z (debian 0.80.0-6+b5)
使用 Hugo 创建网站
在本节中,我们将创建一个名为 hugo.example.com 的新网站。
运行以下命令创建网站:
hugo new site hugo.example.com
创建网站后,您应该获得以下输出:
Congratulations! Your new Hugo site is created in /root/hugo.example.com.
Just a few more steps and you're ready to go:
1. Download a theme into the same-named folder.
Choose a theme from https://themes.gohugo.io/ or
create your own with the "hugo new theme " command.
2. Perhaps you want to add some content. You can add single files
with "hugo new /.".
3. Start the built-in live server via "hugo server".
Visit https://gohugo.io/ for quickstart guide and full documentation.
您可以使用以下命令列出 Hugo 创建的所有文件:
ls -l hugo.example.com
您将获得以下输出:
drwxr-xr-x 2 root root 4096 Nov 13 09:27 archetypes
-rw-r--r-- 1 root root 82 Nov 13 09:27 config.toml
drwxr-xr-x 2 root root 4096 Nov 13 09:27 content
drwxr-xr-x 2 root root 4096 Nov 13 09:27 data
drwxr-xr-x 2 root root 4096 Nov 13 09:27 layouts
drwxr-xr-x 2 root root 4096 Nov 13 09:27 static
drwxr-xr-x 2 root root 4096 Nov 13 09:27 themes
创建一个关于页面和示例帖子
首先,使用以下命令将目录更改为您的网站:
cd hugo.example.com
接下来,使用以下命令创建一个关于页面:
hugo new about.md
您将获得以下输出:
/root/hugo.example.com/content/about.md created
接下来,使用以下命令编辑 about.md 页面:
nano content/about.md
更改文件如下所示:
---
title: "About Us"
date: 2021-11-13T09:28:18Z
draft: false
---
This is About Us page for this website.
接下来,使用以下命令创建示例帖子:
hugo new post/page.md
您将获得以下输出:
/root/hugo.example.com/content/post/page.md created
接下来,使用以下命令编辑示例帖子页面:
nano content/post/page.md
更改文件如下所示:
---
title: "Page"
date: 2021-11-13T09:29:29Z
draft: true
---
# Hugo Page
This is my first hugo website page!
完成后保存并关闭文件。
在您的网站上安装主题
接下来,您需要下载并设置一个主题以访问您的 Hugo 网站。
首先,使用以下命令将目录更改为 themes:
cd themes
接下来,使用以下命令下载 Hugo 主题:
wget https://github.com/digitalcraftsman/hugo-strata-theme/archive/master.zip
接下来,使用以下命令解压缩下载的主题:
unzip master.zip
接下来,使用以下命令重命名提取的主题:
mv hugo-strata-theme-master hugo-strata-theme
接下来,将 config.toml 的内容复制到您站点的配置中。
cat hugo-strata-theme/exampleSite/config.toml > ../config.toml
接下来,使用以下命令编辑 config.toml 文件:
nano ../config.toml
添加/修改以下行:
baseurl = "/"
[[menu.main]]
name = "About"
url = "about"
weight = 5
保存并关闭文件,然后为您的网站创建一个 index.html 文件:
cd ../
nano layouts/index.html
添加以下行:
{{ define "main" }}
{{ if not .Site.Params.about.hide }}
{{ partial "about" . }}
{{ end }}
{{ if not .Site.Params.portfolio.hide }}
{{ partial "portfolio" . }}
{{ end }}
{{ if not .Site.Params.recentposts.hide }}
{{ partial "recent-posts" . }}
{{ end }}
{{ if not .Site.Params.contact.hide }}
{{ partial "contact" . }}
{{ end }}
{{ end }}
保存并关闭文件。
建立并启动您的网站
现在,您需要构建您的网站才能使用它。您可以通过运行以下命令来构建它:
hugo
您将获得以下输出:
{{ with .OutputFormats.Get "RSS" }}{{ .RelPermalink }}{{ end }}
| EN
-------------------+-----
Pages | 8
Paginator pages | 0
Non-page files | 0
Static files | 26
Processed images | 0
Aliases | 2
Sitemaps | 1
Cleaned | 0
Total in 48 ms
接下来,启动 Hugo 服务器并使用以下命令将其与您的服务器 IP 绑定:
hugo server --bind=0.0.0.0 --baseUrl=http://104.245.34.233 -D -F
您将获得以下输出:
{{ with .OutputFormats.Get "RSS" }}{{ .RelPermalink }}{{ end }}
| EN
-------------------+-----
Pages | 11
Paginator pages | 0
Non-page files | 0
Static files | 26
Processed images | 0
Aliases | 3
Sitemaps | 1
Cleaned | 0
Built in 42 ms
Watching for changes in /root/hugo.example.com/{archetypes,content,data,layouts,static,themes}
Watching for config changes in /root/hugo.example.com/config.toml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://104.245.34.233:1313/ (bind address 0.0.0.0)
Press Ctrl+C to stop
此时,Hugo 服务器启动,并监听 1313 端口。
访问雨果网站
现在,打开您的 Web 浏览器并使用 URL http://your-server-ip:1313 访问您的网站。您应该会看到 Hugo 默认页面:

单击左侧窗格中的关于。您应该会在以下屏幕上看到您的关于页面:

结论
恭喜!您已经成功安装了 Hugo 并在 Debian 11 上创建了一个网站。您现在可以使用 Hugo 轻松构建一个安全快速的网站。