在armbian上利用1panel和lucky部署Hexo博客网站

在armbian服务器上创建git裸仓库

可以先创建一个git账号,禁止git用户登录shell,这样git通过ssh服务登录就会被拒绝。我这里因为是在内网上使用,就直接使用root账户。

  1. home/gitHexo/下创建一个仓库hexoNext.git
    mkdir -p /home/gitHexo/hexoNext.git

  2. 进入hexoNext.git目录: cd /home/gitHexo/hexoNext.git

  3. 以裸仓库的形式初始化仓库,**–bare**初始化为裸仓库就是没有自己的工作区。
    git init --bare

  4. 赋与这个hexoNext.git这个目录可读可写的权限。

    # 将目录所有者改为当前用户 (如果用 root 执行,则为 root)
    chown -R root:root /home/gitHexo/hexoNext.git
    # 设置权限 (所有者完全控制,同组用户读取执行,其他用户读取执行)
    chmod -R 755 /home/gitHexo/hexoNext.git
  5. 查看一下文件的归属权限,就是属于哪个账号的,操作权限。 ls -l

    #以上所有代码如下:
    root@uefi-x86:~# mkdir -p /home/gitHexo/hexoNext.git
    root@uefi-x86:~# cd /home/gitHexo/hexoNext.git
    root@uefi-x86:/home/gitHexo/hexoNext.git# git init --bare
    hint: Using 'master' as the name for the initial branch. This default branch
    #.......省略了一部分
    hint: git branch -m <name>
    Initialized empty Git repository in /home/gitHexo/hexoNext.git/
    root@uefi-x86:/home/gitHexo/hexoNext.git# chown -R root:root /home/gitHexo/hexoNext.git
    root@uefi-x86:/home/gitHexo/hexoNext.git# ls -l
    total 32
    drwxr-xr-x 2 root root 4096 May 26 10:54 branches
    -rw-r--r-- 1 root root 66 May 26 10:54 config
    -rw-r--r-- 1 root root 73 May 26 10:54 description
    -rw-r--r-- 1 root root 23 May 26 10:54 HEAD
    drwxr-xr-x 2 root root 4096 May 26 10:54 hooks
    drwxr-xr-x 2 root root 4096 May 26 10:54 info
    drwxr-xr-x 4 root root 4096 May 26 10:54 objects
    drwxr-xr-x 4 root root 4096 May 26 10:54 refs
    root@uefi-x86:/home/gitHexo/hexoNext.git# cd ..
    root@uefi-x86:/home/gitHexo# cd ..
    root@uefi-x86:/home# ls -l
    total 16
    drwxr-xr-x 3 root root 4096 May 26 10:50 gitHexo

在服务器上配置 Git 钩子 (post-receive)

Git 钩子是在 Git 操作过程中的特定时间点自动执行的脚本。post-receive 钩子在服务器成功接收到推送 (push) 后执行。我们将利用这个钩子将推送过来的 Hexo 文件检出 (checkout) 到 你网站根目录。我这里的根目录是home/hexo

  1. 创建钩子文件:
    进入裸仓库的 hooks 目录,并创建一个名为 post-receive 的文件。

    vim /home/gitHexo/hexoNext.git/hooks/post-receive
  2. 编辑钩子内容:
    vim 编辑器中,按 i 进入插入模式,然后粘贴以下脚本内容:

    #!/bin/bash
    # 指定 Hexo 网站文件存放的目录 (!!!!务必替换为你的 1Panel 网站实际根目录!!!!)
    WORK_TREE="/home/hexo"
    # 指定 Git 裸仓库的路径 (!!!!务必替换为你的裸仓库实际路径!!!!)
    GIT_DIR="/home/gitHexo/hexoNext.git"

    # 执行 Git checkout 命令,强制将内容检出到工作目录
    git --work-tree=${WORK_TREE} --git-dir=${GIT_DIR} checkout -f

    echo "Hexo blog deployed to ${WORK_TREE}"

    重要说明:

    • --work-tree: 必须 修改为你在 1Panel 中创建的那个网站的 实际根目录路径
    • --git-dir: 必须 修改为你刚刚创建的 Git 裸仓库的路径 (blog.git)。
    • checkout -f: -f 参数表示强制检出,会覆盖目标目录中已有的文件。
  3. 保存并退出 Vim:
    按下 ESC 键退出插入模式,然后输入 :wq! 并按回车键,强制保存并退出 Vim。

  4. 赋予钩子执行权限:
    新创建的钩子文件默认没有执行权限,需要添加:

    chmod +x /home/gitHexo/hexoNext.git/hooks/post-receive

以上所有代码:

# 创建文件夹
root@uefi-x86:~# mkdir -p /home/gitHexo/hexoNext.git
# 转到文件夹
root@uefi-x86:~# cd /home/gitHexo/hexoNext.git
# 初始化为裸仓库
root@uefi-x86:/home/gitHexo/hexoNext.git# git init --bare
hint: Using 'master' as the name for the initial branch. This default branch name
# ...省略
hint: git branch -m <name>
Initialized empty Git repository in /home/gitHexo/hexoNext.git/
# 创建 钩子文件
root@uefi-x86:/home/gitHexo/hexoNext.git# vim /home/gitHexo/hexoNext.git/hooks/post-receive
root@uefi-x86:/home/gitHexo/hexoNext.git# cd
# 给各个文件权限
root@uefi-x86:~# chown -R root:root /home/gitHexo/hexoNext.git
root@uefi-x86:~# chmod -R 755 /home/gitHexo/hexoNext.git
root@uefi-x86:~# chmod +x /home/gitHexo/hexoNext.git/hooks/post-receive

创建SSH认证

免密登录就是通过rsa认证,生成公钥和私钥,然后把客户端的公钥告诉git服务器。

  1. 在电脑上任一文件夹右键打开Open Git Bash here输入:
    git clone [email protected]:/home/gitHexo/hexoNext.git
    root:是账号。
    192.168.50.86:armbian服务器ip地址或域名。
    /home/gitHexo/hexoNext.git:裸仓库路径。

    #这里就会要求输入root账号的密码。
    Administrator@DESKTOP-7B17KPG MINGW64 /f/hexo
    $ git clone [email protected]:/home/gitHexo/hexoNext.git
    Cloning into 'hexoNext'...
    [email protected]'s password:
  2. 在本地电脑上设置全局username 和 email

    # Open Git Bash here中输入
    git config --global user.name "name"//自定义用户名
    git config --global user.email "[email protected]"//用户邮箱

    --global “参数”:代表全局的意思。

  3. 创建SSH Key
    查询用户主目录的**.ssh文件夹下是否id_rsa(私钥)id_rsa.pub(公钥),位于C盘-用户-用户名下**,如果是Administrator的登录用户名,那么 “ .ssh”夹就存在 C:\Users\Administrator这个目录下。
    如果不存在 : ssh-keygen -t rsa -C "[email protected]

    再查询户主目录下的”.ssh”文件夹下是否存在id_rsa(私钥)id_rsa.pub(公钥)

  4. 上传公钥到armbian服务器

    • 必须将你的本地电脑的 SSH 公钥(id_rsa.pub(公钥)) 添加到armbian服务器上对应用户(如 root)的 ~/.ssh/authorized_keys 文件中。

      #vi 新建authorized_keys文件并打开
      vi ~/.ssh/authorized_keys
    • 打开本地电脑上的id_rsa.pub(公钥)文件,【ctrl+a】,全选,【ctrl+C 】复制。在vim打开的authorized_keys文件中按【i】进入文件编辑模式(左下角会显示--INSERT--),按【shift+insert】插入粘贴板上刚复制的内容到authorized_keys文件中,按【Esc】退出文件编辑模式(左下角显示的--INSERT--消失),按【shift+:】在文档的最低下左边会显示“ :“号输入光标,输入wq保存文件退出。

免密连接armbian

重复上面的1操作

  1. 在电脑上任一文件夹右键打开Open Git Bash here输入:
    git clone [email protected]:/home/gitHexo/hexoNext.git
    root:是账号。
    192.168.50.86:armbian服务器ip地址或域名。
    /home/gitHexo/hexoNext.git:裸仓库路径。

    # 这时不会要求输入密码
    Administrator@DESKTOP-7B17KPG MINGW64 /f/hexo
    $ git clone [email protected]:/home/gitHexo/hexoNext.git
    Cloning into 'hexoNext'...
    warning: You appear to have cloned an empty repository.

    Administrator@DESKTOP-7B17KPG MINGW64 /f/hexo
  2. 提交下文件到armbian服务器。
    我们拷贝或新建一个文本文件到入刚才clone下来的文件夹,并cd进这个文件夹,进行以下操作:

    Administrator@DESKTOP-7B17KPG MINGW64 /f/hexo
    $ cd hexoNext/

    Administrator@DESKTOP-7B17KPG MINGW64 /f/hexo/hexoNext (master)
    $ git add .

    Administrator@DESKTOP-7B17KPG MINGW64 /f/hexo/hexoNext (master)
    $ git commit -a -m "initial commit"
    [master (root-commit) 58b9ae6] initial commit
    1 file changed, 2185 insertions(+)
    create mode 100644 "\345\206\205\346\240\270\345\207\275\346\225\260.txt"

    Administrator@DESKTOP-7B17KPG MINGW64 /f/hexo/hexoNext (master)
    $ git push
    Enumerating objects: 3, done.
    Counting objects: 100% (3/3), done.
    Delta compression using up to 16 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 12.56 KiB | 1.25 MiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
    To 192.168.50.86:/home/gitHexo/hexoNext.git
    * [new branch] master -> master

    Administrator@DESKTOP-7B17KPG MINGW64 /f/hexo/hexoNext (master)
  3. 在armbian服务器的/home/hexo目录下就能看到你上传的那个文件了。

hexo部署

修改 _config.yml网站配置文件

deploy:
- type: git
repository: [email protected]:/root/git/blog.git
branch: master
#- type: baidu_url_submitter # 百度

双部署:

deploy:
- type: git
repository:
github: [email protected]:jinwei26/jinwei26.github.io.git,main
B860ssh: [email protected]:/home/gitHexo/hexoNext.git,master

参考资料参考视频

  • Hexo搭建技术博客部署在阿里云服务器上教程