Featured image of post 搭建Hugo博客

搭建Hugo博客

这是关于搭建本博客的过程

前提:本文档默认你已经拥有一台公网可以访问的云服务器

云服务的操作系统是 AlmaLinux 9.2

一、Hugo程序安装

  • 下载

Hugo 是一个基于Go 语言的框架,可以通过解析markdown文件快速生成静态页面,从而快速方便的创建属于自己的博客。搭建博客的第一步我们先要下载程序,首先访问Hugo的github地址,进入release页面下载最新的程序安装包即可(我使用的是extends和x64版本)。

1
https://github.com/gohugoio/hugo/tags

image-20241113161900513

image-20241113161550090

  • 安装
1
2
3
4
cd /opt/
wget https://github.com/gohugoio/hugo/releases/download/v0.138.0/hugo_extended_0.138.0_Linux-64bit.tar.gz
tar xf hugo_extended_0.138.0_Linux-64bit.tar.gz
mv hugo /usr/local/bin/
  • 验证安装是否成功
1
hugo version

二、搭建博客

  • 选择主题

选择主题的网址:

1
https://themes.gohugo.io/

image-20241113162744820

  • 部署主题
1
2
3
4
5
6
7
8
9
# 创建一个新的站点
hugo new site mysite
# 添加主题
cd /opt/mysite
git clone https://github.com/CaiJimmy/hugo-theme-stack/ themes/hugo-theme-stack
# 复制主题文件到当前工程
cp -r themes/hugo-theme-stack/exampleSite/content/* content/
rm -f hugo.toml
cp themes/hugo-theme-stack/exampleSite/* ./
  • 配置调整

这里都是一些个性化的配置,随你自己去调整。

三、域名和服务器

在aliyun购买了域名,并进行了备案,然后配置dns解析到自己的云主机。

四、配置反向代理

  • 安装nginx
1
2
3
dnf install -y nginx
systemctl start nginx
systemctl enable nginx
  • 配置nginx反向代理

使用Let’s Encrypt免费申请一个tls的证书(具体的操作百度吧,挺简单的)。然后编译安装nginx(其实你也可以二进制安装)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
server {
        listen 443 ssl http2;
        listen [::]:443 http2;
        ssl_certificate       /data/h2c.tech.crt;
        ssl_certificate_key   /data/h2c.tech.key;
        server_name h2c.tech;
        index index.html index.htm;
        error_page 400 = /400.html;

        location /
        {
        proxy_redirect off;
        proxy_pass http://127.0.0.1:1313;
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
}
  • 重启nginx

五、启动Hugo

配置文件修改相关参数,然后只执行hugo server &也行

1
hugo server --config /srv/hugo-server/mysite/hugo.yaml -p 1313 --baseUrl=https://blog.tqg.me
鄂ICP备2024082524号-1
鄂ICP备2024082524号