阿里云ECS搭建hexo博客自动部署

阿里云ECS Ubuntu搭建hexo博客

  • 注册域名shendawei.cn

  • 购买阿里云ECS t5

  • 连接ecs实例

    • ECS控制台管理终端在线连接
    • Xshell用户密码连接
    • Xshell ssh连接
  • 服务器配置

    • 安装Nginx服务器托管

      • apt-get install nginx
      • 启动nginx
        • systemctl start nginx.service
        • systemctl stop nginx.service
        • systemctl status nginx.service
        • systemctl reload nginx.service
        • systemctl restart nginx.service
      • 异常nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
      • 远程IP或域名无法访问Nginx
        • 默认安全组没有开通http/https规则
        • 实例安全组sg-default添加安全组规则
        • 入方向,http 80、https 443,授权对象0.0.0.0/0:代表所有内容实例或外部地址都可访问
    • 也可通过服务器镜像,来安装服务器环境

      • 为了学习服务器原理,所以使用apt-get install单个软件的安装方式
    • 创建安全组

      • sg-20190909-dawei
        • Web Server Linux模板:默认配置http 80、https 443规则
      • sg-defalut
        • 创建ecs实例时,系统默认安全组:默认无配置http 80、https 443规则
      • 关于安全组与实例的基本关系
        • 一个实例至少有一个安全组
        • 一个实例可以加入多个安全组
        • 多个实例可以共享同一个安全组
      • 自创建的sg-20190909-dawei,默认未加入到实例ecs,故外部(本地机器)访问Nginx失败
    • 域名解析

      • 通过域名访问网站IP地址
      • 添加www和@两条主机记录
      • 学习了解dns解析各种类型意义
    • 创建git用户及blog仓库及hook自动部署文档钩子

      • https://www.cnblogs.com/DawaTech/p/7249734.html

        1
        2
        3
        #!/bin/sh
        # rm -rf /var/www/blog/*
        git --work-tree=/var/www/blog --git-dir=/home/git/repos/blog.git checkout -b blog -f
        1
        2
        3
        git --work-tree=/var/www/blog --git-dir=/home/git/repos/blog.git checkout blog -f
        将blog分支拷贝到work-tree(blog)
        git clone时,blog将是默认分支
      • GIT_DIR和GIT_WORK_TREE的妙用
        https://www.yinqisen.cn/blog-718.html[Your branch and 'origin/master' have diverged, and have # and # different commits each, respectively](/Your branch and ‘origin/master’ have diverged, and have # and # different commits each, respectively)

  • 本地配置

    • 安装git环境和node.js环境

    • 安装hexo及初始化blog

    • 配置deploy

      • hexo d无响应,因为deploy 参数冒号后面,必须加空格,且服务器路径用绝对路径

      • hexo d: fatal: ‘master’ does not appear to be a git repository

        1
        https://blog.csdn.net/L_Bill/article/details/84654588
      • Error

        1
        2
        3
        4
        5
        # Deployment
        deploy:
        type:git
        url:git@shendawei.cn:git/repos/blog.git
        branch:master
        1
        2
        3
        4
        5
        # Deployment
        deploy:
        type: git
        repo: git@shendawei.cn:git/repos/blog.git
        branch: master
      • Right

        1
        2
        3
        4
        5
        # Deployment
        deploy:
        type: git
        repo: git@shendawei.cn:/home/git/repos/blog.git
        branch: master
  • 自动部署博客(静态文件) && 手动提交博客(源文件)

    • 方案一:创建新分支,存储源文件(source)

      • 成熟方案,github pages就是例子
        1
        git push -u origin HEAD:source
    • 方案二:发布到新分支pages

      • 待测试方案,通过思考deploy的原理想到

        1
        deploy branch: pages
      • 报错

        1
        git --work-tree=/var/www/blog --git-dir=/home/git/repos/blog.git checkout -b blog -f
      • remote: fatal: A branch named 'blog' already exists.
        
        1
        * 正确
        git --work-tree=/var/www/blog --git-dir=/home/git/repos/blog.git checkout blog -f 将blog分支拷贝到work-tree(blog)
  • 编写markdown文章,自动发布,域名博客浏览