环境准备

  • 安装node.js

    1
    2
    sudo apt update
    sudo apt install nodejs npm
  • 查看版本

    1
    2
    node -v
    npm -v

搭建博客

  • 安装hexo框架

    1
    2
    npm install -g hexo-cli
    hexo -v # 查看版本
  • 创建博客的根目录

    1
    2
    mkdir blog
    cd blog
  • 搭建hexo博客

    1
    sudo hexo init
  • 本地端口启动博客!

    1
    hexo s
  • 创建博客

    1
    2
    3
    hexo new "My first Post" # 创建新博客
    hexo clean # 清理
    hexo generate # 生成

部署到远端

  • 部署到gitee/github

    创建一个新的仓库,YourGithubName.github.io(必须一致!)

    1
    npm install --save hexo-deployer-git #在blog目录下安装git部署插件
  • 配置_config.yml

    1
    2
    3
    4
    5
    6
    # Deployment
    ## Docs: https://hexo.io/docs/deployment.html
    deploy:
    type: git
    repo: https://gitee.com/YourGiteeName/YourGiteeName.gitee.io.git
    branch: master
  • 部署到仓库

    1
    hexo d	# 部署到Github仓库里
  • 然后就可以通过 https://YourGithubName.github.io 地址访问博客啦!

更换主题

  • 下载博客主题

    1
    2
    git clone https://github.com/litten/hexo-theme-yilia.git themes/yilia  #下载yilia主题到本地
    # 修改hexo根目录下的 _config.yml 文件 : theme: yilia
  • 更新

    1
    2
    3
    hexo clean	# 清理一下
    hexo generate # 生成
    hexo d # 部署到远程Github仓库

搭建个人公网博客

  • 本地Linux:

    1
    2
    3
    apt install git
    ssh-keygen -t rsa # 选择 rsa 保存位置为/root/.ssh/id_rsa
    cat /root/.ssh/id_rsa.pub # 查看公钥内容(后续添加到远程Linux信任的key里面)
  • 远程Linux:

    • 创建Git裸仓

      1
      2
      3
      4
      5
      6
      7
      apt install git
      adduser ming # 创建新用户ming
      su ming # 切换用户为ming
      git init --bare /home/ming/ming.git # 创建裸git仓库
      ls -l /home/home #查看生成的文件
      total 4
      drwxrwxr-x 7 ming ming 4096 Jun 28 00:37 ming.git

      可见 ming.git 的所属用户和用户组都是 ming。 如果创建ming.git 时是root 身份,可用 chown -R ming:ming /home/ming/ming.git 更改ming.git 的所属用户和用户组为ming

    • 配置 Git 工作目录:

      该目录用于存放 "本地Linux " 提交上来 (给 远程 Linux) 的 html 等文件,后续安装 Nginx 将该目录下的 html 呈现在公网。

      1
      2
      3
      mkdir -p /var/www/minghexo
      chmod -R 777 /var/www/minghexo
      vim /home/ming/ming.git/hooks/post-update # 配置Git hooks

      post-update 种写入 git --work-tree=/var/www/huaehexo --git-dir=/home/huae/huae.git checkout -f

    • 添加SSH信任

      创建authorized_keys文件,把本地Linux的 id_rsa.pub 添加到此 authorized_keys,该id_rsa.pub 对应的(本地Linux)帐户就能提交 git 内容到本远程主机

      1
      2
      mkdir -p  /home/ming/.ssh
      vim /home/ming/.ssh/authorized_keys

      此时可以通过ssh <用户名>@<服务器公网IP>测试,如果不需要输入密码并连接成功,则没有问题。

  • 本地Linux:

    • 配置 hexo 博客使其能自动推送到远程Linux的git仓库

      1
      ~/blog$ vim _config.yml

      写入内容:

      1
      2
      3
      4
      5
      6
      7
      # Deployment
      ## Docs: https://hexo.io/docs/one-command-deployment
      deploy:
      type: git
      # repo: <git仓库主人>@<远程Linux ip > : <到达 git仓库的路径>
      repo: ming@47.110.137.82:/home/ming/ming.git
      branch: master
    • hexo 博客文件夹中安装 部署工具

      1
      2
      cd blog
      npm install hexo-deployer-git --save
    • 执行部署,部署到远程

      1
      hexo -d
  • 远程Linux配置Nginx:

    • 安装nginx

      1
      apt install nginx
    • 配置nginx

      1
      vim /etc/nginx/sites-enabled/default

      default文件的server{}中添加

      1
      root /var/www/minghexo;
    • 注意 /etc/nginx/nginx.conf 文件中 http { } 花括号里要填入include /etc/nginx/sites-enabled/*;

    • 启动nginx

      1
      service nginx restart

​ 此时用浏览器访问服务器远程IP,就可以访问到博客页面啦!