如何在 Rocky Linux 9 上使用 Nginx 安装 Hugo Site Generator
静态网站是使用静态内容页面或纯 HTML、JavaScript 或 CSS 代码构建的。内容不是动态生成的,不涉及任何数据库或服务器端处理。此类网站速度快得令人难以置信,并且不受依赖于动态代码或 SQL 的攻击的影响。
Hugo 是一个用 Go 编写的开源静态站点生成器。它通过将以 Markdown 语言编写的帖子转换为 HTML 来生成内容。 Hugo 还附带一个内置服务器来托管它生成的网站。您还可以在 Amazon S3、GitHub 页面、Google Cloud、Netlify、Vercel 等上托管 Hugo 网站。
在本教程中,我们将探讨如何安装 Hugo 以在由 Rocky Linux 9 提供支持的服务器上创建静态网站。我们将使用 Nginx 服务器来托管该网站,并使用 Let's Encrypt SSL 证书来保护我们的安装。
先决条件
运行 Rocky Linux 9 的服务器。
具有 sudo 权限的非 root 用户。
指向您的服务器的完全限定域名 (FQDN)。出于我们的目的,我们将使用 hugo.example.com
作为域名。
确保一切都已更新。
$ sudo dnf update
安装基本实用程序包。其中一些可能已经安装。
$ sudo dnf install wget curl nano unzip yum-utils tree -y
第 1 步 - 配置防火墙
第一步是配置防火墙。 Rocky Linux 使用 Firewalld 防火墙。检查防火墙的状态。
$ sudo firewall-cmd --state
running
防火墙适用于不同的区域,公共区域是我们将使用的默认区域。列出防火墙上所有活动的服务和端口。
$ sudo firewall-cmd --permanent --list-services
它应该显示以下输出。
cockpit dhcpv6-client ssh
Wiki.js 需要 HTTP 和 HTTPS 端口才能运行。打开它们。
$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --add-service=https --permanent
重新加载防火墙以应用更改。
$ sudo firewall-cmd --reload
第 2 步 - 安装 Nginx
Rocky Linux 9 附带旧版本的 Nginx。您需要下载官方 Nginx 存储库来安装最新版本。
创建并打开 /etc/yum.repos.d/nginx.repo
文件以创建官方 Nginx 存储库。
$ sudo nano /etc/yum.repos.d/nginx.repo
将以下代码粘贴到其中。
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
按 Ctrl + X 并在出现提示时输入 Y 来保存文件。
安装 Nginx 服务器。
$ sudo dnf install nginx -y
验证安装。
$ nginx -v
nginx version: nginx/1.24.0
启用并启动 Nginx 服务器。
$ sudo systemctl enable nginx --now
检查服务器的状态。
$ sudo systemctl status nginx
? nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
Active: active (running) since Mon 2023-10-30 08:24:05 UTC; 7s ago
Docs: http://nginx.org/en/docs/
Process: 4265 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 4266 (nginx)
Tasks: 2 (limit: 4402)
Memory: 1.9M
CPU: 7ms
CGroup: /system.slice/nginx.service
??4266 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"
??4267 "nginx: worker process"
Oct 30 08:24:05 hugo.example.com systemd[1]: Starting nginx - high performance web server...
第 3 步 - 安装 Git
Hugo 需要 Git 才能工作。运行以下命令安装Git
$ sudo dnf install git -y
验证 Git 安装。
$ git --version
git version 2.39.3
第 4 步 - 安装 Hugo
由于 Hugo 是使用 Go 语言构建的,因此安装非常容易,因为它不需要任何依赖项并且可以独立运行。
下载雨果.在我们的教程中,我们将安装 Hugo 的扩展版本。扩展版本在处理图像时可以将图像编码为Webp格式。如果您希望 Hugo 即时为您转换图像并缩小其尺寸,这非常有用。扩展版本还包括一个 SASS 编译器,在某些情况下您需要它时会很有用。
为 Hugo 创建一个目录。
$ sudo mkdir /var/www/html/hugo -p
切换到新创建的目录。
$ cd /var/www/html/hugo
从 GitHub 发布页面下载 Hugo。在撰写本教程时,v0.119.0 是我们将下载的最新版本。
$ sudo wget https://github.com/gohugoio/hugo/releases/download/v0.119.0/hugo_extended_0.119.0_linux-amd64.tar.gz
从存档中提取文件。
$ sudo tar -xzf hugo_extended_0.119.0_linux-amd64.tar.gz
您将在目录中看到以下三个文件。
$ ls
hugo hugo_extended_0.119.0_linux-amd64.tar.gz LICENSE README.md
将 hugo
二进制文件移动到 /usr/bin
目录。
$ sudo mv hugo /usr/bin/hugo
该文件已经设置了可执行权限,因此您无需设置它。
确认安装。
$ hugo version
hugo v0.119.0-b84644c008e0dc2c4b67bd69cccf87a41a03937e+extended linux/amd64 BuildDate=2023-09-24T15:20:17Z VendorInfo=gohugoio
您还可以使用 --help
标志检查所有可用的选项。
$ hugo --help
您将得到以下输出。
hugo is the main command, used to build your Hugo site.
Hugo is a Fast and Flexible Static Site Generator
built with love by spf13 and friends in Go.
Complete documentation is available at https://gohugo.io/.
Usage:
hugo [flags]
hugo [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
config Print the site configuration
convert Convert your content to different formats
deploy Deploy your site to a Cloud provider.
env Print Hugo version and environment info
gen A collection of several useful generators.
help Help about any command
import Import your site from others.
list Listing out various types of content
mod Various Hugo Modules helpers.
new Create new content for your site
server A high performance webserver
version Print Hugo version and environment info
Flags:
-b, --baseURL string hostname (and path) to the root, e.g. https://spf13.com/
-D, --buildDrafts include content marked as draft
-E, --buildExpired include expired content
-F, --buildFuture include content with publishdate in the future
--cacheDir string filesystem path to cache directory
--cleanDestinationDir remove files from destination not found in static directories
--clock string set the clock used by Hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00
--config string config file (default is hugo.yaml|json|toml)
--configDir string config dir (default "config")
-c, --contentDir string filesystem path to content directory
--debug debug output
-d, --destination string filesystem path to write files to
--disableKinds strings disable different kind of pages (home, RSS etc.)
--enableGitInfo add Git revision, date, author, and CODEOWNERS info to the pages
-e, --environment string build environment
--forceSyncStatic copy all files when static is changed.
--gc enable to run some cleanup tasks (remove unused cache files) after the build
-h, --help help for hugo
--ignoreCache ignores the cache directory
--ignoreVendorPaths string ignores any _vendor for module paths matching the given Glob pattern
-l, --layoutDir string filesystem path to layout directory
--logLevel string log level (debug|info|warn|error)
--minify minify any supported output format (HTML, XML etc.)
--noBuildLock don't create .hugo_build.lock file
--noChmod don't sync permission mode of files
--noTimes don't sync modification time of files
--panicOnWarning panic on first WARNING log
--poll string set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes
--printI18nWarnings print missing translations
--printMemoryUsage print memory usage to screen at intervals
--printPathWarnings print warnings on duplicate target paths etc.
--printUnusedTemplates print warnings on unused templates.
--quiet build in quiet mode
--renderToMemory render to memory (only useful for benchmark testing)
-s, --source string filesystem path to read files relative from
--templateMetrics display metrics about template executions
--templateMetricsHints calculate some improvement hints when combined with --templateMetrics
-t, --theme strings themes to use (located in /themes/THEMENAME/)
--themesDir string filesystem path to themes directory
--trace file write trace to file (not useful in general)
-v, --verbose verbose output
-w, --watch watch filesystem for changes and recreate as needed
Use "hugo [command] --help" for more information about a command.
删除文件夹中的所有剩余文件,因为不再需要它们。
$ sudo rm -rf *.*
第 5 步 - 创建一个新的 Hugo 站点
发出以下命令创建一个新的 Hugo 网站。
$ sudo hugo new site howtoforge
您将得到以下输出。
Congratulations! Your new Hugo site was created in /home/navjot/hugo/howtoforge.
Just a few more steps...
1. Change the current directory to /home/navjot/hugo/howtoforge.
2. Create or install a theme:
- Create a new theme with the command "hugo new theme <THEMENAME>"
- Install a theme from https://themes.gohugo.io/
3. Edit hugo.toml, setting the "theme" property to the theme name.
4. Create new content with the command "hugo new content <SECTIONNAME>/<FILENAME>.<FORMAT>".
5. Start the embedded web server with the command "hugo server --buildDrafts".
See documentation at https://gohugo.io/.
运行tree
命令来检查站点的结构。
$ tree howtoforge/
您将得到以下输出。
howtoforge/
??? archetypes
? ??? default.md
??? assets
??? content
??? data
??? hugo.toml
??? i18n
??? layouts
??? static
??? themes
8 directories, 2 files
切换到Hugo目录。
$ cd howtoforge
第 6 步 - 安装 Hugo 主题
下一步是安装 Hugo 主题。有两种方法可以实现,并且都需要 Git。第一种方法简单明了,涉及克隆主题的 GitHub 存储库。这在大多数情况下很有用,也是最简单的方法。通过此方法更新很容易,尽管此方法确实涉及下载许多您不需要的不必要的东西。有一些主题,尤其是付费主题,没有 GitHub 存储库,您可以从中获取它们。此类主题通常作为压缩档案提供。在这种情况下,您需要手动提取存档并手动更新主题。第三种方法涉及将主题作为 Git 子模块获取。这需要您将 Hugo 目录初始化为 Git 存储库。这是推荐的方法,如果您想将站点部署到 GitHub、Netlify 或任何其他托管服务,该方法非常有用。将您的 Hugo 站点推送到 GitHub 也可以确保自动备份,无需任何额外的工作。
我们将介绍所有三种方法。还有另一种使用 Hugo Go 模块安装 Hugo 主题的方法。仅当主题可作为 Hugo 模块使用时,此方法才有效。但是,我们不会讨论它,因为它超出了本文的范围。
方法 1 - 克隆 Hugo 主题
这是安装 Hugo 主题最常用的方法。在我们的教程中,我们将安装 Paper 主题。切换到Hugo目录。
$ cd /var/www/html/hugo/howtoforge
运行以下命令来克隆主题的 GitHub 存储库。 --depth=1
标志仅获取最新的提交。
$ sudo git clone https://github.com/nanxiaobei/hugo-paper themes/Paper --depth=1
要更新通过此方法下载的主题,请运行以下命令。
$ cd ~/hugo/howtoforge/themes/Paper
$ sudo git pull
方法 2 - 从压缩档案安装主题
这是所有方法中最简单的。获取主题文件的存档。 Paper 主题没有发布部分,因此您无法下载特定版本。因此,我们将下载 GitHub 主存档。您可以从其 GitHub 存储库页面获取该链接。
$ cd ~
$ wget https://github.com/nanxiaobei/hugo-paper/archive/refs/heads/main.zip
将存档解压到主题目录中。 -q
标志抑制输出,-d
标志指定输出目录。
$ sudo unzip -q main.zip -d /var/www/html/hugo/howtoforge/themes/
要更新主题,您需要下载最新的存档并再次解压。确保备份所有已更改的文件。
仅当您的 Hugo 站点托管为 Git 项目时,下面的两种方法才有效。否则,您不需要使用以下方法。
方法 3 - 将主题安装为 Git 子模块
切换到 Hugo 文件夹。
$ cd /var/www/html/hugo/howtoforge
将 Hugo 文件夹初始化为 Git 存储库。这是重要的一步。这允许您将 Hugo 站点推送到 GitHub,以便稍后使用 Netlify、AWS、Vercel 等进行托管。
$ sudo git init
运行以下命令将主题添加为 Git 子模块。
$ sudo git submodule add https://github.com/nanxiaobei/hugo-paper themes/paper
要更新作为子模块添加的主题,请从 Hugo 文件夹运行以下命令。
$ sudo git submodule update --remote --merge
配置雨果
使用您选择的方法安装主题后,打开 Hugo 根目录中的 hugo.toml
文件。从 Hugo v0.110.0 开始,默认配置文件名为 hugo.toml
。如果您使用的是旧版本的 Hugo,您将在 Hugo 目录中找到文件 config.toml。
$ sudo nano hugo.toml
将以下行添加到文件中。该值应与主题的目录匹配。对于 paper 主题,如果您使用存档安装,则为 hugo-paper-theme
目录;如果您使用上面的 Git 克隆或子模块方法,则为 Paper
目录。
theme = 'hugo-paper-main'
另外,将变量 baseURL
的值更改为您网站的域名。并更改您网站的标题
。
您的配置文件应如下所示。
baseURL = 'https://hugo.example.com/'
languageCode = 'en-us'
title = 'Howtoforge Hugo Demo'
theme = 'hugo-paper-main'
完成后,按 Ctrl + X 并在出现提示时输入 Y 来保存文件。
第 7 步 - 创建帖子和页面
要创建新帖子,请发出以下命令。
$ sudo hugo new post/first-post.md
这将在 content/post
文件夹中创建一个新的 markdown 文件 first-post.md
,其标题为 First Post
。在您最喜欢的编辑器中打开文件并编辑详细信息并为您的第一篇文章编写一些内容。
+++
title = 'First Post'
date = 2023-10-30T03:32:38Z
draft = true
+++
## Introduction
This is **bold** text, and this is *emphasized* text.
Visit the [Howtoforge](https://linux教程) website!
写完帖子后,删除 draft=true
。按 Ctrl + X 并在出现提示时输入 Y 来保存文件。
要创建新页面,请发出以下命令。
$ sudo hugo new about.md
这将在 content
文件夹中创建一个名为 About 的新页面。打开它并以与帖子相同的方式进行编辑。
配置主题
在发布之前,我们需要对主题进行一些更改以显示菜单栏。这些更改将在 Hugo 的配置文件中完成,而不是在主题的配置中完成。
切换到Hugo目录。
$ cd /var/www/html/hugo/howtoforge
打开 hugo.toml
文件进行编辑。
$ sudo nano hugo.toml
将以下代码粘贴到底部。
author = "Navjot Singh"
copyright = "Copyright © 2023, Navjot Singh; all rights reserved."
paginate = 3
enableInlineShortcodes = true
# needed to render raw HTML (e.g. <sub>, <sup>, <kbd>, <mark>)
[markup]
[markup.goldmark]
[markup.goldmark.renderer]
unsafe = true
[menu]
[[menu.main]]
identifier = "about"
name = "About"
url = "/about/"
weight = 10
[taxonomies]
category = "categories"
tag = "tags"
series = "series"
按 Ctrl + X 并在出现提示时输入 Y 来保存文件。
这些是使网站正常运行的一些基本设置。在这里,我们将网站配置为每页显示 3 个帖子,将“关于”页面显示为菜单项,并设置类别、标签和帖子系列。我们还启用了 Hugo 的内联短代码,并为 Goldmark Markdown 渲染启用了原始 HTML。如果您不想在帖子中嵌入原始 HTML,您可以禁用它。
第 8 步 - 发布 Hugo 网站
要构建和发布站点,只需运行以下命令。
$ sudo hugo
您将得到类似的输出。
Start building sites …
hugo v0.119.0-b84644c008e0dc2c4b67bd69cccf87a41a03937e+extended linux/amd64 BuildDate=2023-09-24T15:20:17Z VendorInfo=gohugoio
| EN
-------------------+-----
Pages | 7
Paginator pages | 0
Non-page files | 0
Static files | 11
Processed images | 0
Aliases | 3
Sitemaps | 1
Cleaned | 0
Total in 46 ms
这将在 public
文件夹中创建并发布静态 Hugo 网站。这些是我们需要在 Nginx 配置中指定的文件,以便站点正常工作。进行任何更改后再次运行命令,您的网站将立即更新。
第 9 步 - 安装 SSL
在继续之前,我们需要安装 Certbot 工具并为我们的域安装 SSL 证书。
要安装 Certbot,我们将使用 Snapd 软件包安装程序。 Snapd 始终带有最新稳定版本的 Certbot,您应该使用它。
Snapd 工具需要 Epel 存储库才能工作。
$ sudo dnf install epel-release -y
我们将使用 Snapd 安装 Certbot。安装快照。
$ sudo dnf install snapd -y
启用并启动 Snap 服务。
$ sudo systemctl enable snapd.socket --now
创建 Snapd 工作所需的链接。
$ sudo ln -s /var/lib/snapd/snap /snap
$ echo 'export PATH=$PATH:/var/lib/snapd/snap/bin' | sudo tee -a /etc/profile.d/snapd.sh
确保您的 snapd 版本是最新的。请等待几秒钟,然后再发出以下命令。
$ sudo snap install core
$ sudo snap refresh core
安装证书机器人。
$ sudo snap install --classic certbot
使用以下命令确保可以通过创建到 /usr/bin
目录的符号链接来运行 Certbot 命令。
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
验证安装。
$ certbot --version
certbot 2.7.3
生成 SSL 证书。
$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m [email -d hugo.example.com
上面的命令会将证书下载到服务器上的 /etc/letsencrypt/live/hugo.example.com
目录中。
生成 Diffie-Hellman 组证书。
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
检查 Certbot 续订调度程序服务。
$ sudo systemctl list-timers
您会发现 snap.certbot.renew.service
是计划运行的服务之一。
NEXT LEFT LAST PASSED UNIT ACTIVATES
Wed 2023-10-29 10:00:17 UTC 57min left Wed 2023-10-18 08:49:39 UTC 12min ago dnf-makecache.timer dnf-makecache.service
Wed 2023-10-29 12:18:00 UTC 3h 15min left - - snap.certbot.renew.timer snap.certbot.renew.service
Thu 2023-10-30 00:00:00 UTC 14h left Wed 2023-10-18 08:10:08 UTC 52min ago logrotate.timer logrotate.service
对该过程进行一次演练,以检查 SSL 续订是否正常工作。
$ sudo certbot renew --dry-run
如果没有看到任何错误,则一切都已准备就绪。您的证书将自动更新。
第10步-配置Nginx
创建并打开文件 /etc/nginx/conf.d/hugo.conf
进行编辑。
$ sudo nano /etc/nginx/conf.d/hugo.conf
将以下代码粘贴到 hugo.conf
文件中。将 hugo.example.com
的所有实例替换为您的域名。
## enforce HTTPS
server {
listen 80;
listen [::]:80;
server_name hugo.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name hugo.example.com;
root /home/navjot/hugo/howtoforge/public;
index index.html
access_log /var/log/nginx/hugo.access.log;
error_log /var/log/nginx/hugo.error.log;
client_max_body_size 20m;
gzip on;
http2_push_preload on; # Enable HTTP/2 Server Push
ssl_certificate /etc/letsencrypt/live/hugo.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/hugo.example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/hugo.example.com/chain.pem;
ssl_session_timeout 1d;
# Enable TLS versions (TLSv1.3 is required upcoming HTTP/3 QUIC).
ssl_protocols TLSv1.2 TLSv1.3;
# Enable TLSv1.3's 0-RTT. Use $ssl_early_data when reverse proxying to
# prevent replay attacks.
#
# @see: https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_early_data
ssl_early_data on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384';
ssl_prefer_server_ciphers on;
ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
# OCSP Stapling ---
# fetch OCSP records from URL in ssl_certificate and cache them
ssl_stapling on;
ssl_stapling_verify on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
add_header X-Early-Data $tls1_3_early_data;
location / {
try_files $uri $uri/ =404;
}
# Media
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|mp4|ogg|ogv|webm|htc)$ {
expires 30d;
}
# CSS and Js
location ~* \.(css|js)$ {
expires 7d;
}
}
# This block is useful for debugging TLS v1.3. Please feel free to remove this
# and use the `$ssl_early_data` variable exposed by NGINX directly should you
# wish to do so.
map $ssl_early_data $tls1_3_early_data {
"~." $ssl_early_data;
default "";
}
上述配置将所有 HTTP 请求重定向到 HTTPS,并将作为 Hugo 服务的代理通过您的域提供服务。
按 Ctrl + X 并在出现提示时输入 Y 来保存文件。
打开文件 /etc/nginx/nginx.conf
进行编辑。
$ sudo nano /etc/nginx/nginx.conf
在 include /etc/nginx/conf.d/*.conf;
行之前添加以下行。
server_names_hash_bucket_size 64;
按 Ctrl + X 并在出现提示时输入 Y 来保存文件。
验证您的 Nginx 配置。
$ sudo nginx -t
如果您没有看到任何错误,则表示您可以开始了。重新加载 Nginx 服务器。
$ sudo systemctl reload nginx
第 11 步 - 配置 SELinux
更改 OpenEMR 的文件安全上下文。
$ sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/hugo/howtoforge/public(/.*)?"
应用政策。
$ sudo restorecon -Rv /var/www/html/hugo/howtoforge/public/
允许 Nginx 连接到网络。
$ sudo setsebool -P httpd_can_network_connect 1
第 12 步 - 运行站点
现在,您可以通过在网络浏览器中打开 https://hugo.example.com
来验证您的安装。您将看到以下页面,表明安装成功。
第 13 步 - 更新 Hugo
更新 Hugo 非常简单。从 Hugo 的 GitHub 页面下载最新版本。
$ cd /var/www/html/hugo
$ sudo wget https://github.com/gohugoio/hugo/releases/download/v0.119.0/hugo_extended_0.119.0_linux-amd64.tar.gz
提取存档。
$ sudo tar -xzf hugo_extended_0.119.0_linux-amd64.tar.gz
并将 Hugo 二进制文件替换为最新版本。 -f
标志会覆盖二进制文件而不提示。如果您想要提示覆盖,可以使用 -i
标志。
$ sudo mv -f hugo /usr/bin/hugo
就这样。更新 Hugo 就像替换二进制文件一样简单。
第 14 步 - 备份和恢复 Hugo
要进行备份,请创建整个 Hugo 文件夹的存档。
$ cd /var/www/html/hugo
$ sudo tar -czvf howtoforge.tar.gz howtoforge/
要在新服务器上恢复它,请提取存档并安装二进制文件,然后就可以开始了。
$ cd /var/www/html/hugo
$ sudo tar -xzf howtoforge.tar.gz
结论
关于如何使用 Nginx 在 Rocky Linux 9 服务器上设置 Hugo 静态站点生成器的教程到此结束。如果您有任何问题或反馈,请在下面的评论中分享。