准备
- 客户端,服务端都安装 hugo
- 服务端, 创建一个空的git仓库
# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装 Git 和 Nginx (Web 服务器)
sudo apt install git nginx -y
# 安装 Hugo
# 建议直接安装 extended 版本以支持更多主题,Ubuntu 24 的仓库版本通常较新
sudo apt install hugo -y
mkdir blog.git
cd blog.git
git init --bare
- 客户端创建一个博客项目
hugo new site blog
添加主题 https://themes.gohugo.io/ 选择一个主题, 放在theme目录 更新: hugo.toml 例如: theme = “PaperMod”
删除theme里面的.git避免子目录问题
配置远程push权限
- 编辑 ~/.ssh/config
Host myblog.com
HostName 192.168.1.100
User root
IdentityFile ~/.ssh/user.pem
IdentitiesOnly yes
- 添加remote url
git remote add deploy myblog.com:/srv/myblog/blog.git
hooks自动部署
vim hooks/post-receive
#!/bin/bash
# 路径定义
TMP_DIR=/srv/www/blog-fxio/tmp
GIT_DIR=/srv/www/blog-fxio/blog.git
PUBLIC_DIR=/srv/www/blog-fxio/blog-html
# 1. 强制同步源码目录
echo "--- 正在更新源码 ---"
cd $TMP_DIR
git --git-dir=$GIT_DIR --work-tree=. checkout -f fxio.site
# 2. 运行 Hugo 构建
echo "--- 正在构建 HTML ---"
cd $TMP_DIR
# 升级后的 Hugo 路径确认
hugo --destination $PUBLIC_DIR --gc --minify
# 3. 权限修正
chown -R www-data:www-data $PUBLIC_DIR
echo "--- 部署成功! ---"
https
sudo apt install python3-certbot-nginx -y
sudo certbot --nginx -d fxio.site