1. 程式人生 > >Hexo安裝和配置

Hexo安裝和配置

文章標題 line 訪問 ali all mail 表示 安裝 代碼

Hexo安裝和配置

1. Git安裝和設置

  • github
brew install git          #Mac電腦使用brew安裝 
sudo apt-get install git  #Ubuntu系統使用這條命令安裝 

然後設置好git賬戶
使用Github Page搭建博客, 需要在github建立倉庫,倉庫名為username.github.io

  • gitcafe
    因為github速度稍微慢一點,所以用作備份站,主站用國內的gitcafe。
  1. 先到https://gitcafe.com/projects/new頁面註冊一個新的項目,項目名需要與你的用戶名相同,默認分支選擇gitcafe-pages,項目主頁也是相同的 http://username.gitcafe.com

  2. 采用和github同樣的key文件,在 ~/.ssh/id_rsa.pub
    如果沒有,單獨生成一個ssh-key

ssh-keygen -t rsa -C "emailaddress" -f ~/.ssh/gitcafe

在SSH的文件夾下,可以看到gitcafe私鑰和公鑰文件:

gitcafe
gitcafe.pub

生成配置文件:

touch ~/.ssh/config

添加以下內容:

Host gitcafe.com www.gitcafe.com
IdentityFile ~/.ssh/gitcafe
  1. 登錄網站,將SSH文件夾下的 GitCafe.pub 中的內容復制到公鑰框中即可。

  2. 測試是否連上,共用的key,輸入:

ssh -T [email protected] -i ~/.ssh/id_rsa

單獨的key,輸入:

ssh -T [email protected] -i ~/.ssh/gitcafe

2. Node.js安裝

brew install node  #最新版的node.js的包中已經集成了npm包管理工具

使用以下命令驗證是否安裝成功

node -v
npm -v

如果運行hexo命令時,發現錯誤:

-bash: hexo: command not found

是沒有指定nodejs所致。可以使用下面方法解決:

如果機器沒有安裝過node,那麽首先brew install nvm

安裝nvm。

其次需要在shell的配置文件(~/.bashrc, ~/.profile, or ~/.zshrc)中添加如下內容:

export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

然後設置別名

nvm ls
#v0.12.7

nvm use 0.12.7
#Now using node v0.12.7

nvm alias default 0.12.7
#default -> 0.12.7 (-> v0.12.7)

如果之前通過brew install node方式安裝過node,那麽需要先刪除系統中存在的node:

brew remove --force node
sudo rm -r /usr/local/lib/node_modules

brew prune
sudo rm -r /usr/local/include/node

# 檢查brew是否正常
brew doctor

3. Hexo安裝與設置

Node, npm和Git都安裝成功, 開始安裝hexo

npm install hexo -g  #-g表示全局安裝, npm默認為當前項目安裝
hexo version

如果遇到報錯

{ [Error: Cannot find module ‘./build/Release/DTraceProviderBindings‘] code: ‘MODULE_NOT_FOUND‘ }
{ [Error: Cannot find module ‘./build/default/DTraceProviderBindings‘] code: ‘MODULE_NOT_FOUND‘ }
{ [Error: Cannot find module ‘./build/Debug/DTraceProviderBindings‘] code: ‘MODULE_NOT_FOUND‘ }

則用下列語句安裝
npm install hexo --no-optional

Hexo使用命令:

cd ~/git
hexo init hexo  #執行init命令初始化到你指定的hexo目錄
cd hexo
npm install    #install before start blogging
hexo generate       #自動根據當前目錄下文件,生成靜態網頁
hexo server         #運行本地服務

瀏覽器輸入http://localhost:4000就可以看到效果。

目錄結構

.
├── .deploy       #需要部署的文件
├── node_modules  #Hexo插件
├── public        #生成的靜態網頁文件
├── scaffolds     #模板
├── source        #博客正文和其他源文件, 404 favicon CNAME 等都應該放在這裏
|   ├── _drafts   #草稿
|   └── _posts    #文章
├── themes        #主題
├── _config.yml   #全局配置文件
└── package.json

4. 添加博文

hexo new "postName"  #新建博文,其中postName是博文題目

博文會自動生成在博客目錄下source/_posts\postName.md

文件自動生成格式:

title: "It Starts with iGaze: Visual Attention Driven Networkingwith Smart Glasses"  #博文題目
date: 2014-11-21 11:25:38      #生成時間
tags:                    #標簽, 多個標簽也可以使用格式[tag1, tag2, tag3,...]
- tag1
- tag2
- tag3
categories: [cat1,cat2,cat3]
---
正文, 使用 Markdown 語法書寫

如果不想博文在首頁全部顯示, 並能出現閱讀全文按鈕效果, 需要在你想在首頁顯示的部分下添加

<!--more-->

5. 主題更改

cd ~/git/hexo
git clone [email protected]:litten/hexo-theme-yilia.git themes/yilia

在./_config.yml,修改主題為yilia
theme: yilia

Hexo主題列表

另外推薦幾個主題:
iissnan/hexo-theme-next
TryGhost/Casper
kywk/hexo-theme-casper #基於Casper
daleanthony/uno
A-limon/pacman
AlxMedia/hueman
ppoffice/hexo-theme-hueman #基於Hueman
xiangming/landscape-plus #基於官方
hexojs/hexo-theme-landscape

查看本地效果

hexo g
hexo s

6. 部署到Git

以上內容都是在本地進行查看, 現在將博客部署到github或gitcafe上

gitcafe

打開./_config.yml,修改如下信息:

type: git
repository: [email protected]:yourname/yourname.git
branch: gitcafe-pages

branch要提交到gitcafe-pages,然後才能在username.gitcafe.io中看到提交的頁面。

Github

deploy:
  type: github
  repo: https://github.com/lmintlcx/lmintlcx.github.io.git
  branch: master

項目主頁需要把 branch 設置為 gh-pages

註意事項

  • 所有鍵的冒號後面留一個空格,如language: zh-CN
  • url不能為空,否則報錯
  • type: github報錯hexo ERROR Deployer not found: github的解決方法:
    先運行 npm install hexo-deployer-git --save
    再改為 type: git

完整配置信息如下:

# Site #站點信息
title: blog Name #標題
subtitle: Subtitle #副標題
description: my blog desc #描述
author: me #作者
language: zh-CN #語言
timezone: Asia/Shanghai #時區
    
# URL
url: http://yoururl.com   #用於綁定域名, 其他的不需要配置
root: /
#permalink: :year/:month/:day/:title/
permalink: posts/title.html
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 #新文章標題
default_layout: post #默認模板(post page photo draft)
titlecase: false #標題轉換成大寫
external_link: true #新標簽頁裏打開連接
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 #日期時間格式
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss

# Pagination #分頁
per_page: 10 #每頁文章數, 設置成 0 禁用分頁
pagination_dir: page

# Extensions #插件和主題
## 插件: http://hexo.io/plugins/
## 主題: http://hexo.io/themes/
theme: next

# Deployment #部署, 同時發布在 GitHub 和 GitCafe 上面
deploy:
- type: git
  repo: [email protected]:username/username.git,gitcafe-pages
- type: git
  repo: [email protected]:username/username.github.io.git,master

# Disqus #Disqus評論系統
disqus_shortname: 

plugins: #插件,例如生成 RSS 和站點地圖的
- hexo-generator-feed
- hexo-generator-sitemap
    

保存之後,便可以使用hexo d上傳到GitCafe。
部署成功,使用username.gitcafe.io進行訪問, 或者設置個性域名,參考官方Wiki。

7.域名

  • 綁定域名
    不綁定域名的話只能通過 your_user_name.github.io 訪問
    申請域名推薦 GoDaddy, 域名解析推薦 DNSPod

  • 綁定頂級域名
    新建文件 CNAME, 無後綴, 純文本文件, 內容為要綁定的域名 blogname.com
    如果要使用 www.blogname.com 的形式, 文件內容改為 www.blogname.com

DNS設置
主機記錄@, 類型A, 記錄值192.30.252.153
主機記錄www, 類型A, 記錄值192.30.252.153
參考 Tips for configuring an A record with your DNS provider

  • 綁定子域名

比如使用域名blogname.com的子域名blog.blogname.com
CNAME文件內容為blog.blogname.com

DNS設置
主機記錄blog, 類型CNAME, 記錄值blogname.github.io
參考 Tips for configuring a CNAME record with your DNS provider

  • Gitcafe 綁定域名

項目管理界面, 左側的導航欄中有自定義域名設置

8.其他配置

站點建立時間
例如 ? 2014 - 2015

站點配置文件新增字段 since
since: 2014

側欄設置
post - 默認行為, 在文章頁面(擁有目錄列表)時顯示
always - 在所有頁面中都顯示
hide - 在所有頁面中都隱藏(可以手動展開)

sidebar: post

頭像設置
編輯站點配置文件, 新增字段 avatar, 頭像的鏈接地址可以是:

網絡地址
https://avatars2.githubusercontent.com/u/4962914?v=3&s=460

站點內的地址
/images/avatar.jpg #頭像圖片放置在主題的 source/images/

avatar: /images/avatar.png

菜單設置
編輯主題配置文件的 menu
若站點運行在子目錄中, 將鏈接前綴的 / 去掉

menu:
  home: /
  archives: /archives
  categories: /categories
  tags: /tags
  commonweal: /404.html
  about: /about