Hugo搭建个人博客

Hugo

Hugo is one of the most popular open-source static site generators. With its amazing speed and flexibility, Hugo makes building websites fun again.

搭建环境准备

  • Debian 12
  • Go
  • Git

安装Hugo

下载 Hugo 二进制文件

首先,访问 Hugo 的 GitHub Releases 页面 获取最新版本的 Hugo 二进制文件。

选择适合 Debian 12 的版本(例如 hugo_0.139.0_linux-amd64.tar.gz),右键复制下载链接,进入终端连接到VPS

1mkdir hugo/
2cd hugo/
3wget https://github.com/gohugoio/hugo/releases/download/v0.139.0/hugo_0.139.0_linux-amd64.tar.gz
4#解压下载的文件
5tar -xzvf hugo_0.139.0_linux-amd64.tar.gz

将 Hugo添加到系统的 PATH 环境变量

使用文本编辑器打开文件。例如,使用 nano 编辑 ~/.bashrc

1nano ~/.bashrc
2#在文件的末尾,添加以下行
3export PATH=$PATH:/path/to/your/directory/hugo
4#使更改生效
5source ~/.bashrc
6#确认 Hugo 是否已经成功添加到 PATH 中
7which hugo

/path/to/your/directory/hugo为解压后Hugo的文件路径

配置Hugo自启

创建一个 systemd 服务文件来配置 Hugo 服务

1sudo nano /etc/systemd/system/hugo.service

hugo.service内容如下:

 1[Unit]
 2Description=Hugo Static Site Generator
 3After=network.target
 4
 5[Service]
 6Type=simple
 7ExecStart=/path/to/your/directory/hugo server --bind=0.0.0.0 --port=1313 --appendPort=false --baseURL="https://blog.example" --liveReloadPort 443
 8WorkingDirectory=/path/to/your/directory/
 9Restart=on-failure
10User=null
11Group=null
12
13[Install]
14WantedBy=multi-user.target

WorkingDirectory:设置为你的 Hugo 网站目录,例如 /path/to/your/directory/hugo_site,这是 Hugo 项目的根目录

启动 Hugo 服务

重新加载 systemd 配置并启动 Hugo

1sudo systemctl daemon-reload
2sudo systemctl enable hugo
3sudo systemctl start hugo

这样Hugo就搭建成功

使用 Hugo 构建
主题 StackJimmy 设计