使用Hexo初创我的博客

Hexo博客搭建

  1. 确认Node.js环境已安装

  2. 终端切换到root用户

    1
    $ sudo su
  3. 利用npm安装淘宝镜像cnpm

    1
    $ npm install -g cnpm --registry=https://registry.npm.taobao.org

    (-g为全局安装)

  4. 利用cnpm安装hexo

    1
    $ cnpm install -g hexo-cli
  5. 在/Users/macluo建立我的blog文件夹。

    确认位置确实是在/Users/macluo

    1
    $ pwd

    建立文件夹blog(注意要全小写,以便于后期部署调试)。

    1
    $ mkdir blog
  6. 以管理员身份在此文件夹用hexo初始化部署博客

    1
    $ sudo hexo init
  7. 由于hexo依旧需要从github clone,因此init命令还是会由于GitHub网速报错,将无效文件夹删除掉,我们将5、6、7步的命令换用国内源用gitee翻译如下即可正常部署。

    1
    2
    3
    4
    5
    6
    $ git clone https://gitee.com/weilining/hexo-starter.git blog
    $ cd blog
    $ git submodule init
    $ git submodule update
    $ npm config set registry http://registry.npm.taobao.org
    $ npm i
  1. 运行hexo

    1
    $ hexo s

    发生如下问题

    • 默认端口4000被占用,排查发现,4000,5000等端口均被占用,更改为4001端口。后来 重启排除占用后,也可以直接使用默认的4000端口。

      hexo server -p 4001
      
    • 浏览器找不到服务器localhost,排查发现,是hosts加速github未部署好,[[使用Homebrew搭建Git环境#^2d3ccb|系统原来的host文件不能省略]]。因此设置要更改。

  2. 新建一篇文章。

    使用命令

    1
    $ hexo n "文章名" 

    会自动创建文件/Users/macluo/blog/source/_posts/“文章名”.md。
    因此直接将写好的markdown文章拖入此位置也可行。

  3. 归档并重新部署调整。
    在blog根目录下,运行命令

    1
    2
    3
    $ hexo clean
    $ hexo g
    $ hexo server -p 4001

    完成一篇新博客的书写与部署。

更换主题

下载主题Ayer到blog文件夹blog/themes/ayer

1
$ git clone https://gitee.com/shen-yu/hexo-theme-ayer.git themes/ayer

启用主题 ,将blog文件夹的_config.yml中的
theme值由默认的landscape改为ayer。

1
2
3
4
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: ayer

静态博客部署至Gitee

阅读官方文档-部署,可知操作如下

  1. 确保Git环境已搭建好
  2. 在blog文件夹内安装hexo-deployer-git插件
    1
    $ npm install hexo-deployer-git --save
  3. 在_config.yml文件内填入Gitee仓库的ssh,确保Gitee Pages服务可用(完成实名认证)
  4. 使用hexo部署服务
    1
    $ hexo d
  5. 根据提示补齐所需账号密码等数据
  6. 待hexo自动调用git完成上传后,确认git状态
    1
    $ git status
  7. 在Gitee开启Gitee Pages服务

管理文章

blog包括本地文件夹与服务器端文件夹,还有未写完的文章。
将未写完的文章放在了我的Onedrive中随时续写。

使用Git管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sh-3.2# git add .
warning: 正在添加嵌入式 git 仓库:themes/ayer
提示:You've added another git repository inside your current repository.
提示:Clones of the outer repository will not contain the contents of
提示:the embedded repository and will not know how to obtain it.
提示:If you meant to add a submodule, use:
提示:
提示: git submodule add <url> themes/ayer
提示:
提示:If you added this path by mistake, you can remove it from the
提示:index with:
提示:
提示: git rm --cached themes/ayer
提示:
提示:See "git help submodule" for more information.