使用Git Hook 自动部署Hexo博客到个人VPS

本文简要介绍如何使用Github Hook功能将Hexo静态博客网页的内容自动同步到个人VPS对应目录下展示。

配置windows 平台下 Visual Studio Code 远程登录VPS


Github部署博客配置

Rstudio撰写Hexo博客配置

VPS购买及配置

  1. vps提供商控制台登录后台界面,获取IP地址端口密码

Visual Studio Code平台远程SSH无密码登录VPS配置

  1. windows11系统本地打开Visual Studio Code ,进入扩展市场,安装Remote - SSH扩展插件,安装完成后重启。

  2. 点击远程资源管理器,选择SSH Targets,点击+号,出现Enter SSH Connection Command框,在此框中按ssh root@IP地址的格式输入ssh连接远程VPS的命令,输入完成后回车

  3. 在出现Select SSH configuration file to update,选择C:\Users\[用户名]\.ssh\config路径保存远程连接的配置文件。

  4. 打开配置文件,按如下格式修改远程VPS的连接信息:

     Host 腾讯云 #自己定义的用户名
     HostName xxx.xxx.xxx.xxx #VPS主机的地址
     User root #VPS主机的用户名,默认root
     Port 22 #SSH登录端口,默认22
    

    完成修改后保存。

  5. 点击远程资源管理器,选择SSH Targets下以**[腾讯云]命名的VPS,在新窗口中建立连接,按提示选择远程VPS为Linux*,输入密码,即可连接VPS。

  6. 进一步配置SSH密钥登录VPS。本地打开Windows Terminal,按如下操作生成密钥<此处参考阮一峰开源书籍SSH 密钥登录 - SSH 教程 - 网道 (wangdoc.com)>。

    cat id_rsa.pub | ssh  root@IP地址 -p 端口号 'mkdir -p ~/.ssh && cat >> .ssh/authorized_keys' 
    
  7. 完成上述工作后,即可采用Visual Studio Code免输入密码登录VPS。

VPS搭建静态博客配置

  1. 搭建nginx

    1. 安装nginx

      nginx是ubuntu发行版软件库中的软件,因此仅需要使用自带的APT包管理器安装nginx。具体安装过程中,先更新包管理器,然后安装软件。

      sudo apt update
      sudo apt install nginx
    

    安装完成后,在Terminal窗口输入nginx -v,窗口显示nginx version: nginx/1.18.0 (Ubuntu),提示安装成功。

    1. 配置防火墙

      正式配置nginx前,需要确保防火墙放行nginx相关端口。ufw是ubuntu发行版默认防火墙,内置nginx的防火墙命令。具体安装过程中,先更新包管理器,然后安装软件。

      sudo ufw app list #执行此命令后,会看到输出可能的应用防火墙规则:Nginx Full,Nginx Http,Nginx Https,OpenSSH
      sudo ufw allow 'Nginx Full' #选择Nginx Full打开全部接口
      sudo ufw status #检查防火墙状态
    
    sudo systemctl reload nginx
    
  2. 安装SSL证书certbot

    sudo snap install core; sudo snap refresh core
    sudo snap install --classic certbot
    sudo ln -s /snap/bin/certbot /usr/bin/certbot
    sudo certbot --nginx
    
  3. 安装git

Baibing Mi
Baibing Mi
Assissant Professor of Epidemiology and Biostatistics

Research interests include epidemiologic study and nutritional interventions related to maternal and child health and the developmental origins of disease.