利用Windows内置Linux子系统搭建Hexo静态博客

之前博客是在Windows上搭建的,后来Windows 10内置了linux子系统,我就想能不能在这个子系统里搭建一个静态博客,因为这样就无需安装nodejs和git了。中途遇到了很多问题,直到今天才终于成功。下面记录一下过程。

启用linux子系统

首先在设置里启用开发者模式,然后启用或关闭Windows功能里勾选“适用于Linux的Windows子系统”,重启后在命令提示符里输入“bash”后回车,确认后就开始下载子系统了。下载结束后,在开始菜单里会出现一个Ubuntu的图标,打开以后就是Linux子系统了。

安装必备环境

我们知道,Hexo的运行环境是nodejs,所以我们要先在子系统里安装nodejs。这里有一个坑,那就是我直接使用“sudo apt install nodejs”安装以后,在使用npm时总是报错,说什么权限不够,网上找了很多办法,都没有解决。直到今天,我尝试着用nvm安装了最新的nodejs,这个问题才得以解决。

使用nvm安装最新nodejs

  1. 安装nvm之前执行以下命令:

    1
    2
    sudo apt-get update
    sudo apt-get install build-essential libssl-dev
  2. nvm执行脚本(最新版请去官网查看):

    1
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash
  3. 更新环境变量:

    1
    source ~/.profile
  4. 列出所有nodejs版本:

    1
    nvm ls-remote
  5. 安装最新版本:

    1
    nvm install 8.5.0

安装git

1
sudo apt install git

开始搭建Hexo

  1. 安装hexo框架

    1
    npm install -g hexo-cli
  2. 进入你用来存放博客的文件夹,执行:

    1
    2
    hexo init
    npm install
  3. 把博客同步到Github Page上还需要两个插件:

    1
    2
    npm install hexo -server --save
    npm install hexo-deployer-git --save
  4. 我的_config.yml:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    # Hexo Configuration
    ## Docs: https://hexo.io/docs/configuration.html
    ## Source: https://github.com/hexojs/hexo/

    # Site
    title: YW9的博客
    subtitle:
    description:
    author: Owen
    language:
    timezone:

    # URL
    ## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
    url: https://yw9.github.io
    root: /
    permalink: :year/:month/:day/:title/
    permalink_defaults:

    # Directory
    source_dir: source
    public_dir: public
    tag_dir: tags
    archive_dir: archives
    category_dir: categories
    code_dir: downloads/code
    i18n_dir: :lang
    skip_render:

    # Writing
    new_post_name: :title.md # File name of new posts
    default_layout: post
    titlecase: false # Transform title into titlecase
    external_link: true # Open external links in new tab
    filename_case: 0
    render_drafts: false
    post_asset_folder: false
    relative_link: false
    future: true
    highlight:
    enable: true
    line_number: true
    auto_detect: false
    tab_replace:

    # Home page setting
    # path: Root path for your blogs index page. (default = '')
    # per_page: Posts displayed per page. (0 = disable pagination)
    # order_by: Posts order. (Order by date descending by default)
    index_generator:
    path: ''
    per_page: 20
    order_by: -date

    # Category & Tag
    default_category: uncategorized
    category_map:
    tag_map:

    # Date / Time format
    ## Hexo uses Moment.js to parse and display date
    ## You can customize the date format as defined in
    ## http://momentjs.com/docs/#/displaying/format/
    date_format: YYYY-MM-DD
    time_format: HH:mm:ss

    # Pagination
    ## Set per_page to 0 to disable pagination
    per_page: 20
    pagination_dir: page

    # Extensions
    ## Plugins: https://hexo.io/plugins/
    ## Themes: https://hexo.io/themes/
    plugin:
    - hexo-generator-feed
    - hexo-generator-sitemap
    - hexo-generator-baidu-sitemap
    #Feed Atom
    feed:
    type: atom
    path: atom.xml
    limit: 20

    facicon: /favicon.ico

    theme: typing
    #polk

    # Deployment
    ## Docs: https://hexo.io/docs/deployment.html
    deploy:
    type: git
    repo:
    github: git@github.com:yw9/yw9.github.io.git
    branch: master

修改永久链接默认显示方式

参考这里