利用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之前执行以下命令:
sudo apt-get update
sudo apt-get install build-essential libssl-dev
  1. nvm执行脚本(最新版请去官网查看):
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash
  1. 更新环境变量:
source ~/.profile
  1. 列出所有nodejs版本:
nvm ls-remote
  1. 安装最新版本:
nvm install 8.5.0

安装git

sudo apt install git

开始搭建Hexo

  1. 安装hexo框架
npm install -g hexo-cli
  1. 进入你用来存放博客的文件夹,执行:
hexo init
npm install
  1. 把博客同步到Github Page上还需要两个插件:
npm install hexo -server --save
npm install hexo-deployer-git --save
  1. 我的_config.yml:
# 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

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

参考这里

updatedupdated2020-04-292020-04-29
点击刷新