Hexo部署到阿里云

部署环境与准备

1.环境

本地 Windows11 操作系统

阿里云 Debian12 服务器

1panel Linux服务器运维管理面板

2.准备

Hexo 本地博客

MobaXterm 连接服务器

云服务器配置 Git

1.查看 git 的版本

1
git version 

2 创建 git 用户并且修改权限

1
2
3
4
5
6
7
8
adduser elykia
passwd elykia
chmod 740 /etc/sudoers
vim /etc/sudoers root
在这里加入:
root ALL=(ALL) ALL
elykia ALL=(ALL) ALL
:wq! #退出

3.本地 windows10 使用 Gitbash 创建密钥(若有可以省去)

1
ssh-keygen -t rsa

4.将本地创建 id_rsa.pub 中文件复制

1
2
3
4
su elykia
mkdir ~/.ssh
vim ~/.ssh/authorized_keys
cat ~/.ssh/authorized_keys

13.本地测试

1
ssh -v elykia@47.238.174.200

出现 Welcome to Alibaba Cloud Elastic Compute Service !则代表部署成功

云服务器网站配置

1.创建网站目录并且设置权限

1
2
3
su root
mkdir /opt/1panel/apps/openresty/openresty/www/sites/blog/index
chown elykia:elykia -R /opt/1panel/apps/openresty/openresty/www/sites/blog/index

5.建立 git 仓库

1
2
3
4
su root
cd /opt/1panel/apps/openresty/openresty/www/sites/blog/index
git init --bare blog.git
chown elykia:elykia -R blog.git

6.同步网站根目录

1
2
3
4
5
vim blog.git/hooks/post-receive

#添加以下内容
#!/bin/sh
git --work-tree=/opt/1panel/apps/openresty/openresty/www/sites/blog/index --git-dir=/opt/1panel/apps/openresty/openresty/www/sites/blog/index/blog.git checkout -f

7.修改权限

1
chmod +x /opt/1panel/apps/openresty/openresty/www/sites/blog/index/blog.git/hooks/post-receive

8.在 windows10 本地 hexo 目录修改_config.yml 文件

1
2
3
4
deploy:
type: git
repository: elykia@127.0.0.1:/opt/1panel/apps/openresty/openresty/www/sites/blog/index/blog.git #用户名@服务器Ip:git仓库位置
branch: master

9.在本机 gitbash 部署

1
2
hexo clean
hexo g -d

常见报错

  1. git-upload-pack: 未找到命令
1
2
bash: git-upload-pack: command not found
fatal: Could not read from remote repository.

解决方法

1
sudo ln -s  /usr/local/git/bin/git-upload-pack  /usr/bin/git-upload-pack

2.git-receive-pack: 未找到命令

1
2
bash: git-receive-pack: command not found
fatal: Could not read from remote repository.

解决方法

1
sudo ln -s /usr/local/git/bin/git-receive-pack  /usr/bin/git-receive-pack

3.无法远程连接获取

1
fatal: Could not read from remote repository.

解决方法

1
重试或者 删掉本地ssh公钥重新上传至服务器

4.key 出错

1
Host key verification failed.

解决方法

1
ssh-keygen -R 你要访问的IP地址

参考资料:

  1. Hexo部署到腾讯云或阿里云)