部署在github
生成public
进入public
在github创建一个新的存储库github名字.github.io
1
2
3
4
5
6
| git init
git add .
git commit -m "首次提交"
git branch -M main
git remote add origin git@github.com:github名字/github名字.github.io.git
git push -u origin main
|
自动部署脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
| #!/bin/bash
echo -e "\033[1;36m========================================\033[0m"
echo -e "\033[1;36m Hugo 站点部署工具 - GitHub Pages\033[0m"
echo -e "\033[1;36m========================================\033[0m"
echo ""
# 检查是否在 Hugo 项目根目录
if [ ! -d "content" ] && [ ! -d "themes" ]; then
echo -e "\033[1;31m❌ 错误:请在 Hugo 项目根目录运行此脚本\033[0m"
echo "当前目录: $(pwd)"
echo "期望找到: content/ 或 themes/ 文件夹"
exit 1
fi
# 检查 hugo 命令是否可用
if ! command -v hugo &> /dev/null; then
echo -e "\033[1;31m❌ 错误:找不到 hugo 命令,请确保已安装 Hugo\033[0m"
exit 1
fi
# 1. 构建静态文件
echo -e "\033[1;33m[1/4] 🛠️ 正在生成静态文件...\033[0m"
hugo --minify
if [ $? -ne 0 ]; then
echo -e "\033[1;31m❌ Hugo 构建失败!请检查错误信息。\033[0m"
read -p "按回车键继续..."
exit 1
fi
# 2. 进入公共文件夹
echo -e "\033[1;33m[2/4] 📁 进入公共文件夹...\033[0m"
cd public
# 3. 初始化一个临时 Git 仓库用于部署
echo -e "\033[1;33m[3/4] 📦 准备部署到 GitHub Pages...\033[0m"
# 创建或清空 .git 目录(避免子模块问题)
if [ -d ".git" ]; then
rm -rf .git
fi
git init
# 设置用户信息
git config user.email "sports0808008@users.noreply.github.com"
git config user.name "sports0808008"
# 确保使用 main 分支
git checkout -b main
# 添加所有文件
git add --all .
# 提交更改
msg="🚀 自动部署:$(date +'%Y-%m-%d %H:%M:%S')"
if [ $# -gt 0 ]; then
msg="$*"
fi
git commit -m "$msg"
# 4. 推送到 GitHub Pages
echo -e "\033[1;33m[4/4] ⬆️ 推送到 GitHub Pages...\033[0m"
# 直接推送到 main 分支(现在远程已经有这个分支了)
if git push --force git@github.com:sports0808008/sports0808008.github.io.git main:main; then
echo ""
echo -e "\033[1;32m✅ 部署成功!\033[0m"
echo -e "\033[1;34m🌐 访问地址: https://sports0808008.github.io\033[0m"
echo -e "\033[1;32m⏰ 更新时间: $(date +'%Y-%m-%d %H:%M:%S')\033[0m"
echo ""
else
echo -e "\033[1;31m❌ 推送到 main 分支失败!\033[0m"
echo -e "\033[1;33m正在尝试推送到 master 分支...\033[0m"
# 尝试推送到 master 分支(备用方案)
if git push --force git@github.com:sports0808008/sports0808008.github.io.git main:master; then
echo ""
echo -e "\033[1;32m✅ 部署成功!\033[0m"
echo -e "\033[1;34m🌐 访问地址: https://sports0808008.github.io\033[0m"
echo -e "\033[1;33m📝 注意:使用了 master 分支而不是 main 分支\033[0m"
echo -e "\033[1;32m⏰ 更新时间: $(date +'%Y-%m-%d %H:%M:%S')\033[0m"
echo ""
else
echo -e "\033[1;31m❌ 所有推送尝试都失败了!\033[0m"
echo -e "\033[1;33m请检查以下可能的问题:\033[0m"
echo "1. GitHub 仓库权限是否正确"
echo "2. SSH 密钥是否已添加到 GitHub"
echo "3. 仓库名称是否正确: sports0808008/sports0808008.github.io"
fi
fi
# 返回原始目录
cd ..
read -p "按回车键退出..."
|
报错解决方案:配置 SSH 密钥
第1步:生成 SSH 密钥
1
| ssh-keygen -t ed25519 -C "你的邮箱地址"
|
- 按回车使用默认保存路径 (
~/.ssh/id_ed25519) - 可以设置密码(推荐)或直接回车跳过
第2步:启动 SSH 代理并添加密钥
1
2
3
4
5
| # 启动 ssh-agent
eval "$(ssh-agent -s)"
# 添加 SSH 私钥到代理
ssh-add ~/.ssh/id_ed25519
|
第3步:复制公钥并添加到 GitHub
1
2
| # 显示并复制公钥内容
cat ~/.ssh/id_ed25519.pub
|
然后进行以下操作:
- 复制终端显示的全部内容(以
ssh-ed25519 AAAAC3... 开头) - 登录 GitHub → 点击头像 → Settings → SSH and GPG keys
- 点击 “New SSH key”
- Title 填写 “My Windows PC” 或其他标识
- Key 类型选择 “Authentication Key”
- 在 Key 框中粘贴刚才复制的内容
- 点击 “Add SSH key”
第4步:测试连接
ssh -T git@github.com
如果看到 Hi sports0808008! You've successfully authenticated... 就说明成功了!
部署在云服务器
自动上传脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
| #!/bin/bash
echo -e "\033[1;36m========================================\033[0m"
echo -e "\033[1;36m Hugo 站点部署工具 - xtkyu.top\033[0m"
echo -e "\033[1;36m========================================\033[0m"
echo ""
# 0. 更宽松的目录检查:检查是否存在 content 文件夹和 hugo 可执行文件
if [ ! -d "content" ] && [ ! -d "themes" ]; then
echo -e "\033[1;31m❌ 错误:请在 Hugo 项目根目录运行此脚本\033[0m"
echo "当前目录: $(pwd)"
echo "期望找到: content/ 或 themes/ 文件夹"
exit 1
fi
# 检查 hugo 命令是否可用
if ! command -v hugo &> /dev/null; then
echo -e "\033[1;31m❌ 错误:找不到 hugo 命令,请确保已安装 Hugo\033[0m"
exit 1
fi
# 1. 构建静态文件
echo -e "\033[1;33m[1/4] 🛠️ 正在生成静态文件...\033[0m"
hugo
if [ $? -ne 0 ]; then
echo -e "\033[1;31m❌ Hugo 构建失败!请检查错误信息。\033[0m"
read -p "按回车键继续..."
exit 1
fi
# 2. 进入公共文件夹
echo -e "\033[1;33m[2/4] 📁 进入公共文件夹...\033[0m"
cd public
# 3. 智能Git配置:只在第一次运行时初始化
echo -e "\033[1;33m[3/4] 📦 配置Git仓库...\033[0m"
if [ ! -d ".git" ]; then
echo "初始化新的Git仓库并设置远程..."
git init
git remote add deploy ssh://tk@xtkyu.top:96/home/git/blog.git
# 设置默认分支为master
git checkout -b master
else
echo "使用现有的Git仓库"
# 确保远程仓库存在
if ! git remote | grep -q "deploy"; then
echo "添加远程仓库..."
git remote add deploy ssh://tk@xtkyu.top:96/home/git/blog.git
fi
fi
# 4. 添加和提交所有更改
git add --all .
msg="🚀 自动部署:$(date +'%Y-%m-%d %H:%M:%S')"
if [ $# -gt 0 ]; then
msg="$*"
fi
git commit -m "$msg"
# 5. 推送到服务器
echo -e "\033[1;33m[4/4] ⬆️ 推送到服务器...\033[0m"
git push --force deploy master:master
if [ $? -eq 0 ]; then
echo ""
echo -e "\033[1;32m✅ 部署成功!\033[0m"
echo -e "\033[1;34m🌐 访问地址: http://xtkyu.top\033[0m"
echo -e "\033[1;32m⏰ 更新时间: $(date +'%Y-%m-%d %H:%M:%S')\033[0m"
echo ""
else
echo ""
echo -e "\033[1;31m❌ 推送失败!\033[0m"
echo -e "\033[1;33m正在尝试修复...\033[0m"
git remote remove deploy 2>/dev/null
git remote add deploy ssh://tk@xtkyu.top:96/home/git/blog.git
git push --force deploy master:master
fi
read -p "按回车键退出..."
|
准备工作
已经搭建完毕的本地博客
一台windows/mac电脑
一台云服务器(本教程以腾讯云为例)
本地电脑安装一个ssh连接工具
服务器上安装配置git
安装依赖工具包
1
| yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel
|
安装编译工具
1
| yum install -y gcc perl-ExtUtils-MakeMaker package
|
删除旧版本git
回到根目录
下载git到家目录
1
| wget https://www.kernel.org/pub/software/scm/git/git-2.34.0.tar.gz --no-check-certificate
|
如果提示没有wget命令,先安装:
解压文件
1
| tar -zxf git-2.34.0.tar.gz
|
安装git
进入到git目录下
编译(时间可能有点久,稍微等待一下)
1
| make all prefix=/usr/local/git
|
安装git到/usr/local/git下
1
| make install prefix=/usr/local/git
|
配置git环境变量
将git加入PATH目录中
1
| echo 'export PATH=$PATH:/usr/local/git/bin' >> /etc/bashrc
|
使配置生效
查看git版本
创建用户配置SSH
创建用户并设置密码
这里的tk改成你自己的用户名,并且为用户设置一个密码。
例如:tk是用户名
1
2
3
| adduser tk
passwd tk # 注意是刚创建的用户名 tk
然后按提示输入两次密码(输入时不会显示,正常现象)。
|
为用户分配权限
把用户添加到wheel组
本地创建密钥
在本机电脑终端输入(cmd):
第一次回车后输入y后面就是一直回车了,新建一个密钥,注意如果之前你将你的本地hexo博客部署到GitHub的时候生成的那个密钥是不可用的在重新生成这个密钥,不然上传的时候就会出错。
一般来说在你创建的时候命令行会提示你密钥文件的路径,windows电脑一般是C:\Users\ASUS.ssh
复制id_rsa.pub文件中的内容备用。
配置公钥
回到服务器终端,切换用户
tk就是添加的用户
创建.ssh文件夹
1
| mkdir .ssh && chmod 700 .ssh
|
或者
1
2
3
| # 1. 创建目录并设置权限
mkdir -p ~/.ssh
chmod 700 ~/.ssh
|
新建authorized_keys
1
| touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
|
或者
1
2
3
4
5
6
7
8
| # 2. 创建 authorized_keys
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
# 3. 验证
ls -la ~/.ssh
# 4. 如果需要,修复家目录权限
chmod 755 ~
sudo chown -R tk:tk ~
|
查看repo的用户信息
本地查看是否在.ssh下有公钥私钥,如果没有需要创建:
将刚刚id_rsa.pub公钥中的内容,复制粘贴到文件里,保存退出。(注意这里是输入i编辑,esc退出编辑模式,:wq保存)
git仓库配置
退出用户登录
此时应该是root用户,创建git目录,并修改目录的所有权和用户权限
1
2
3
| mkdir /home/git/
chown -R tk:tk /home/git/
chmod -R 755 /home/git/
|
进入目录,建立git仓库,修改权限
1
2
3
| cd /home/git/
git init --bare blog.git
chown tk:tk -R blog.git
|
报错:
1
| git config --global init.defaultBranch master
|
新建钩子文件 post-receive
1
| vim /home/git/blog.git/hooks/post-receive
|
进入文本编辑器,粘贴下面两行。
1
2
| #!/bin/bash
git --work-tree=/home/blog --git-dir=/home/git/blog.git checkout -f
|
保存退出。然后修改文件权限
1
| chmod +x /home/git/blog.git/hooks/post-receive
|
nginx配置
首先创建blog目录用户nginx转发
1
2
3
| mkdir /home/blog/
chown -R tk:tk /home/blog/
chmod -R 755 /home/blog/
|
1 第一种方法安装nginx
两种方法任选其一,如果yum安装成功就不用第二种。
启动nginx
1
| systemctl start nginx.service
|
查看nginx服务状态,绿色的active说明启动成功
1
| systemctl status nginx.service
|
第二种方法安装nginx
针对yum无法安装的情况,这里介绍第二种方法,源码安装。
回到家目录
下载安装包,鉴于下载地址的时效性,附上
1
| wget http://nginx.org/download/nginx-1.21.4.tar.gz
|
解压
1
| tar -zxf nginx-1.21.4.tar.gz
|
进入目录,编译安装
1
2
3
| cd nginx-1.21.4
./configure
make install prefix=/usr/local/nginx
|
配置nginx环境变量,并使环境变量生效
1
2
| echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> /etc/bashrc
source /etc/bashrc
|
查看nginx版本号
配置Systemd服务
1
| vim /lib/systemd/system/nginx.service
|
在文本编辑器中粘贴以下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
| [Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp= true
[Install]
WantedBy=multi-user.target
|
内容说明:
1
2
3
4
5
6
7
8
9
10
11
| [Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
|
设置开机自启动
1
| systemctl enable nginx.service
|
重启电脑
重启后,查看nginx服务状态,绿色的active说明启动成功
1
| systemctl status nginx.service
|
修改nginx转发配置
在浏览器中输入服务器公网ip地址,测试是否能打开。
查看nginx的默认配置文件的安装位置
修改nginx的默认配置,其中cd后边就是刚刚查到的安装位置,每个人可能都不一样,我这里是/usr/local/nginx/conf/nginx.conf
1
| vim /usr/local/nginx/conf/nginx.conf
|
在文本编辑器中找到如下位置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| server {
listen 80 default_server; # 监听IPv4的80端口,设为默认服务
listen [::]:80 default_server; # 监听IPv6的80端口,设为默认服务
root /home/blog; # 网站根目录(需替换为实际路径,修改为创建的blog目录地址)
server_name www.example.com; # 域名(需替换为实际域名或公网IP)
# 加载默认服务块的配置文件
include /etc/nginx/default.d/*.conf;
location / { # 根路径配置(当前为空,需补充)
}
error_page 404 /404.html; # 自定义404错误页面
location = /40x.html { # 404页面的精确匹配(应与上行路径一致)
}
}
|
优化后:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| server {
listen 8888 default_server; # 修改为监听8888端口(IPv4)
listen [::]:8888 default_server; # 修改为监听8888端口(IPv6)
server_name www.example.com; # 改为实际域名或IP
root /home/blog;
index index.html index.htm;
# 安全响应头
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
# 静态文件处理
location / {
try_files $uri $uri/ =404;
}
# 错误页面配置
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
# 禁止访问隐藏文件
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# 禁用favicon和robots.txt的日志噪声
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| server {
listen 8888 default_server;
listen [::]:8888 default_server;
server_name www.example.com;
root /home/blog;
index index.html index.htm;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
}
|
可以使用反向代理隐藏8888端口
1
2
3
4
5
6
7
8
9
10
11
| server {
listen 80;
server_name example.com; # 改为你的域名或IP
location / {
proxy_pass http://127.0.0.1:88; # 代理到本机 8888 端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
|
保存退出。重启nginx服务
1
| systemctl restart nginx.service
|
在hexo站点配置文件中配置
打开你本地的hexo博客所在文件,打开站点配置文件_config.yml
1
2
3
4
| deploy:
type: git
repository: tk@100.0.0.100:/home/git/blog.git #用户名@服务器Ip:git仓库位置
branch: master
|
注意:如果云服务器ssh端口不是22,站点配置文件_config.yml具体配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # 1. 进入项目目录
cd I:\Blog\hexo-theme-Fomalhaut-main
# 2. 初始化仓库(如果未初始化)
git init
# 3. 关联远程仓库(指定SSH端口57)
git remote add origin ssh://tk@157.82.12.146:57/home/git/blog.git
#后面可以不用
# 4. 添加文件并提交
git add .
git commit -m "Initial commit"
# 5. 首次推送
git push -u origin master# 如果分支是main则替换为main
|
在本地hexo博客根目录下,打开终端,部署
1
2
3
| hexo clean
hexo g
hexo d
|
如果提示ERROR Deployer not found:git错误,先安装:
1
| npm install hexo-deployer-git --save
|
从远程仓库克隆到本地
1. 直接克隆(指定端口57)
1
| git clone ssh://tk@157.82.12.146:57/home/git/blog.git
|
2. 如果克隆失败
检查服务器仓库权限:
1
| ssh -p 57 tk@157.82.12.146 "ls -ld /home/git/blog.git"
|
1
| drwxr-xr-x 7 tk tk 4096 4月 20 13:08 /home/git/blog.git
|
如果权限不足,需修正:
1
| ssh -p 57 tk@157.82.12.146 "chmod -R 755 /home/git/blog.git"
|