之前博客是在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
- 安裝nvm之前執行以下命令:
sudo apt-get update
sudo apt-get install build-essential libssl-dev
- nvm執行腳本(最新版請去官網查看):
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash
- 更新環境變量:
source ~/.profile
- 列出所有nodejs版本:
nvm ls-remote
- 安裝最新版本:
nvm install 8.5.0
安裝git
sudo apt install git
開始搭建Hexo
- 安裝hexo框架
npm install -g hexo-cli
- 進入你用來存放博客的文件夾,執行:
hexo init
npm install
- 把博客同步到Github Page上還需要兩個插件:
npm install hexo -server --save
npm install hexo-deployer-git --save
- 我的_config.yml:
# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/
# Site
title: yuanwenjun's blog
subtitle: It's wonderful life!
description:
author: yuanwenjun
language: zh-CN
timezone:
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: https://yuanwenjun.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: 10
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: 8
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
theme: Hacker
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo:
github: git@github.com:yuanwenjun/yuanwenjun.github.io.git
coding: git@git.coding.net:ablog/ablog.git
branch: master