Hexo Blog Updates to Aliyun and Github Page

aliyun 服务器配置

ubuntu 系统更新

1
2
sudo apt update
sudo apt upgrade

创建新用户

1
2
3
adduser jarvis # jarvis 为用户名
chmod 740 /etc/sudoers
vim /etc/sudoers

找到如下 root ALL=(ALL:ALL) ALL 后,在其下面添加一行

1
jarvis ALL=(ALL:ALL) ALL

切换到新用户目录下

1
2
3
4
su jarvis
cd ~
sudo apt update
sudo apt install vim git htop screenfetch curl wget # 安装常用软件

配置 ssh

在服务器用户目录下创建 ~/.sshauthorized_keys 文件,赋予权限

1
2
3
4
mkdir ~/.ssh
vim ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh/

然后切回本机,将 ~/.ssh/id_rsa.pub 公钥复制到远程服务器的 ~/.ssh/authorized_keys里面;本地测试,验证 ssh 无密码登录

1
ssh -v jarvis@SERVER_IP # -v 参数显示详细信息 verbose

配置 git

创建工作目录 blog,初始化 Git 裸库 blog.git,创建 hook 文件

1
2
3
4
5
6
cd ~
mkdir blog
mkdir repos
cd repos
git init --bare blog.git
vim blog.git/hooks/post-receive # 创建 hook 文件

编辑 hook 内容

1
2
#!/bin/sh
git --work-tree=/home/jarvis/blog --git-dir=/home/jarvis/repos/blog.git checkout -f

添加运行权限

1
chmod +x blog.git/hooks/post-receive

配置 nginx

安装 nginx 并修改对应配置文件

1
2
sudo apt install
sudo vim /etc/nginx/sites-available/default

找到

1
2
3
# include snippets/snakeoil.conf;

root /var/www/html;

替换为

1
2
3
# include snippets/snakeoil.conf;

root /home/jarvis/blog;

此刻直接访问云服务器的公网 IP 会显示 nginx 欢迎界面。

本地 hexo _config.yml 文件配置

配置如下

1
2
3
4
5
6
7
8
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repo:
github: git@github.com:kyrie2to11/kyrie2to11.github.io.git
aliyun: jarvis@aliyun_server_ip:/home/jarvis/blog/blog.git
branch: master

用 aliyun ip 访问博客报错 404 处理

部署完毕,访问 SERVER IP 出现 404 报错, 查看 log,显示没有访问 /home/jarvis/blog 的权限

1
2
3
4
5
6
7
vim /var/log/nginx/error.log

# 输出如下
2024/08/17 19:44:42 [crit] 9007#9007: *14 stat() "/home/jarvis/blog/" failed (13: Permission denied), client: 114.97.236.162, server: _, request: "GET / HTTP/1.1", host: "47.100.101.82"
2024/08/17 19:44:42 [crit] 9007#9007: *14 stat() "/home/jarvis/blog/" failed (13: Permission denied), client: 114.97.236.162, server: _, request: "GET / HTTP/1.1", host: "47.100.101.82"
2024/08/17 19:44:43 [crit] 9007#9007: *14 stat() "/home/jarvis/blog/" failed (13: Permission denied), client: 114.97.236.162, server: _, request: "GET / HTTP/1.1", host: "47.100.101.82"
2024/08/17 19:44:43 [crit] 9007#9007: *14 stat() "/home/jarvis/blog/" failed (13: Permission denied), client: 114.97.236.162, server: _, request: "GET / HTTP/1.1", host: "47.100.101.82"

查看 nginx 所有进程,找到 nginx worker process 为 www-data

1
2
3
4
5
6
7
ps aux | grep nginx

# 输出如下
nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 12637 0.0 0.3 55992 6488 ? S 19:51 0:00 nginx: worker process
www-data 12638 0.0 0.3 55992 5656 ? S 19:51 0:00 nginx: worker process
root 13217 0.0 0.1 6612 2444 pts/5 S+ 20:01 0:00 grep --color=auto nginx

参照如下链接赋予 nginx worker process: www-data 访问博客路径 root /home/jarvis/blog 的权限

1
2
3
gpasswd -a www-data jarvis
chmod g+x /home && chmod g+x /home/jarvis && chmod g+x /home/jarvis/blog
nginx -s reload # 重启 nginx