首先你需要一臺Lunix主機——一臺連上外網的Lunix的電腦。
我在CONOHA買的VPS,相同配置下,性價比比DO和VULTR好那么一丟丟。
買到主機后的配置我就pass不寫了,直接來說在終端狀態下,然后搭建Hexo+Gitlab博客
安裝請時刻注意自己在什么目錄下!我是在root用戶下完成的!因為node安裝需要高級權限。
1.安裝Node.js
- 下載
找到最新的下載地址,用wget命令下載到本地
wget https://nodejs.org/dist/v4.4.4/node-v4.4.4-linux-x64.tar.xz
解壓命令,是xz文件。SO
xz -d node-v4.4.4-linux-x64.tar.xz
tar -xvf node-v4.4.4-linux-x64.tar
或者
tar -xvJf node-v4.4.4-linux-x64.tar.xz
文件就被解壓到當前文件夾下了
mv node-v4.4.4-linux-x64 node-v4.4.4
mv node-v4.4.4 /usr/local/node
- 配置環境變量
# 編輯 /etc/profile (使用vim)
vim /etc/profile
# 在底部添加 PATH 變量
export PATH=$PATH:/usr/local/node/bin
# 保存退出,先按exit鍵,再按shift+:
wq
# 最后保存并使其生效即可
source /etc/profile
2.安裝Hexo
這里我是在 /User/Zing目錄下操作的!Zing是我的用戶名!
# 創建目錄
mkdir hexo
# 切換目錄
cd hexo
# 安裝Git(已安裝可跳過)
yum install git-core
# 安裝 Hexo
npm install -g hexo-cli
# 初始化 Hexo
hexo init
- 安裝插件
npm install hexo-generator-index --save
npm install hexo-generator-archive --save
npm install hexo-generator-category --save
npm install hexo-generator-tag --save
npm install hexo-server --save
npm install hexo-deployer-git --save
npm install hexo-deployer-heroku --save
npm install hexo-deployer-rsync --save
npm install hexo-deployer-openshift --save
npm install hexo-renderer-marked --save
npm install hexo-renderer-stylus --save
npm install hexo-generator-feed --save
npm install hexo-generator-sitemap --save
- 修改Hexo配置文件
提示:key對應沒有值的時候,冒號后面一定要有空格!否則會報錯
例如: timezone:會報錯,timezone:?則不會。
# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/
# Site 站點信息設置
title: Zing's Blog #站名
subtitle: 不忘初心 方得始終 #副標題
description: #站描述
author: Zing #作者
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: http://blog.prozin.xyz
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: true
tab_replace:
# 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/
theme: landscape
# Deployment 這里設置了Git獲
#這里一定要注意不要寫錯了,否則部署到Github上會出問題
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: git@github.com:mocorochio/micorochio.github.io.git
branch: master
message: '站點更新:{{now("YYYY-MM-DD HH/mm/ss")}}'
3.部署到GitHub
- 1.配置本機全局git環境(如果已經配置過請忽略)
首先請使用郵箱注冊github賬號,否則會影響下面操作,記住你注冊的郵箱。
另外,請在VPS上設置你的git
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
- 2.生成SSH秘鑰
先確定你的VPS 有沒有生成過ssh的key,
驗證
less ~/.ssh/id_rsa.pub
如果報錯
執行下面代碼
# -C后面跟住你在github的用戶名郵箱,這樣公鑰才會被github認可
ssh-keygen -t rsa -C example@126.com
# 查看 公鑰內容 稍后加入Github 賬戶的 sshkey中
less ~/.ssh/id_rsa.pub
你會看到一堆代碼
如果沒有報錯
# -C后面跟住你在github的用戶名郵箱,這樣公鑰才會被github認可
ssh-keygen -t rsa -C example@126.com
# 回車后,輸入一個文件夾名字,存儲新的SSH 秘鑰
.ssh/github
# 查看 公鑰內容 稍后加入Github 賬戶的 sshkey中
less ~/.ssh/id_rsa.pub
當然,也有可能遇到這種情況,你可以選擇覆蓋(確認沒有人使用當前的SSH秘鑰),或者不覆蓋 (如圖)
- 3.創建博客工程
博客工程創建要注意!!!,看看 漠然 怎么說的
創建一個新項目,項目名稱為 用戶名.github.io ,比如我的Github用戶名是mritd,則創建的項目名為 mritd.github.io
用戶名是你的github用戶名哦!千萬別弄錯了,不然訪問不到的!
所以我的是git@github.com:mocorochio/micorochio.github.io.git
- 4.將ssh秘鑰添加到github中
生成后,需要將秘鑰放到github上去,登錄你的github賬號
進入秘鑰設置面板:https://github.com/settings/ssh
在執行less ~/.ssh/id_rsa.pub
你會看到一堆長密碼
這一段密碼全部復制到github上
- 5.配置Hexo,編譯與部署
還記得我們在_config.yml里最后一段的配置嗎?
# Deployment 這里設置了Git獲
#這里一定要注意不要寫錯了,否則部署到Github上會出問題
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: git@github.com:mocorochio/micorochio.github.io.git
branch: master
message: '站點更新:{{now("YYYY-MM-DD HH/mm/ss")}}'
這里,repo就是你創建的博客工程
最后一步,編譯,上傳靜態代碼
# 編譯
hexo generate
# 在主機的hexo目錄下 執行以下命令將自動更新到Github
hexo d
4.修改主題和配置配置文件
- 4.1安裝主題NexT
ps:我們現在在目錄hexo下
git clone https://github.com/iissnan/hexo-theme-next themes/next
并在目錄hexo下的_config.yml中
# 找到 theme: 修改后面的參數,默認是 landscape
theme: next
4.2配置主題
源碼拷貝出來太多了,所以貼出next的使用說明供大家參考4.3找主題
https://hexo.io/themes/
訪問:
看,現在通過 昵稱.github.io
可以訪問靜博客了
http://micorochio.github.io
最后感謝漠然-mrtid
本文參考了以下站點文章:
Github 搭建 Hexo 靜態博客
Hexo 主題 next