Hugo 博客系统 建立全流程
①京东云上面-Rocky Linux 9.0 64位 创建该系统
②进入该系统后更新yum服务包:
yum update
③配置hugo运行所需环境:
3.1安装git
yum install git
安装go
yum install go
安装nano
yum install nano
通过安装ruby来实现安装sass
yum install ruby
安装ruby的运行环境
yum install ruby-devel
Ruby 自带一个叫做 RubyGems 的系统,用来安装基于 Ruby 的软件。我们可以使用这个系统来 轻松地安装 Sass
gem install sass
如下sass常用更新、查看版本、sass命令帮助等命令:
gem update sass
现在hugo的所需环境都已安装,现在可以安装hugo了,这里通过snap安装hugo。
yum install -y snapd
启动并使用snapd服务开机自启
systemctl enable snapd;systemctl start snapd
symlink链接一下
symlink from /snap to /var/lib/snapd/snap
这个也是必要选项,但是我不太懂这个的意思。
ln -s /var/lib/snapd/snap /snap
最后终于到了安装hugo了,使用snap安装hugo。
sudo snap install hugo
④用hugo服务来生成一个博客网站
不要以为这里就成功了,后续还有一些web服务器配置,请耐心继续沿着教程。
现在请通过hugo建立一个网站。
hugo new site hercity --format yaml
这里的hercity是网站的名称,请自适应用自己喜欢的名称。
有了网站后,大家可以去挑选自己喜欢的主题模板,这里我用的是PaperMod
进入到刚刚建立的网站"hercity"主目录下,使用git下载模板。
git clone https://github.com/adityatelange/hugo-PaperMod themes/PaperMod --depth=1
然后在使用nano 命令编辑“hercity”主目录下的hugo.yaml文件,最尾端添加一行命令来指定主题。
theme: ["PaperMod"]
至此,个人网站已经搭设完成。现在到了比较繁琐的下一步,配置web服务器。
⑤安装 Nginx
首先确保系统已安装最新版 Nginx。
sudo dnf install nginx
设置Nginx开机自启
sudo systemctl enable nginx
启动Nginx服务
sudo systemctl start nginx
配置 Nginx 反向代理,新建一个 Nginx 配置文件,用nano编辑配置文件。
sudo nano /etc/nginx/conf.d/hugo.conf
将下列代码写入该配置文件
server {
listen 80;
server_name sun2mao.cn www.sun2mao.cn; # 替换为你的域名
location / {
proxy_pass http://localhost:1313; # 转发到 Hugo 服务的 1313 端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 可选:静态文件缓存配置
# location /assets/ {
# expires 1y;
# add_header Cache-Control "public";
# }
}
写完后,CTRL+X ,然后Y,然后回车。
检查配置语法并重启 Nginx
sudo nginx -t
重新加载配置
sudo systemctl reload nginx
配置防火墙,开放 HTTP(80)和 HTTPS(443)端口。
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
重启防火墙(有可能云主机默认是没有开防火墙的)。
先开启防火墙
systemctl start firewalld
重启防火墙
sudo firewall-cmd --reload
验证 Hugo 服务配置,确保 Hugo 已绑定到 0.0.0.0:1313 (域名记得换成自己的域名)。
hugo server --bind=0.0.0.0 --port=1313 --baseURL=http://sun2mao.cn
现在访问下自己的域名,是不是已经可以正常呈现出来了。如果一切正常,恭喜你成功了,但是仍有两步路要走。
⑥使用 Let’s Encrypt 自动申请 SSL 证书:
安装 Certbot
sudo dnf install certbot python3-certbot-nginx
获取 SSL 证书(www.sun2mao.cn记得改成自己的域名地址)
sudo certbot --nginx -d your-domain.com -d www.sun2mao.cn
Certbot 会自动修改 Nginx 配置并启用 HTTPS。
验证自动续签,证书到期前会自动续签,手动测试续签。
sudo certbot renew --dry-run
一切正常后就到了最后一步了,因为你终端一断开,你的hugo网站就停止了,我们要让它一直运行,这个时候就需要配置systemctl服务来托管它了。
⑦ 托管 Hugo 服务(后台运行)
建议使用 systemd 托管 Hugo,避免终端关闭后服务停止。
sudo nano /etc/systemd/system/hugo.service
使用nano编辑此配置文件
[Unit]
Description=Hugo Static Site Server
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/root/hercity
ExecStart=/var/lib/snapd/snap/bin/hugo server --bind=0.0.0.0 --port=1313 --baseURL=http://sun2mao.cn --appendPort=false --disableFastRender
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
User=root # 替换为实际用户,应该是root
Group=root # 当前用户属于哪个群组,应该也是root
WorkingDirectory=/root/hercity #等于号后面接hugo网站主目录地址
ExecStart=/var/lib/snapd/snap/bin/hugo #等于号后面接hugo执行程序地址
baseURL=http://sun2mao.cn #等于号后面接你的域名地址
修改完后,CTRL+X,按Y,按回车
最后启用并启动服务:
sudo systemctl daemon-reload
sudo systemctl enable hugo
sudo systemctl start hugo
通过以上步骤,Nginx 会将来自 80/443 端口的请求转发到 Hugo 的 1313 端口,用户可直接通过域名访问网站。
至此教程结束。
大家搭建的过程中肯定会出现各种各样的问题,也不用问别人,直接问DeepSeek就行了。
跟着他一步一步排查,解决即可,我就是这么过来的。
希望能帮到大家。