記錄使用github pages服務搭建個人博客的詳細程序以及遇到的各種坑
hexo+github搭建個人博客教程
1.前言
1.1 準備作業
環境 : win10
-
注冊github賬號:https://github.com/
-
下載安裝node.js:官網下載https://nodejs.org/en/
下載后圖形化界面安裝,安裝成功后運行cmd 輸入node -v,顯示版本資訊即為成功
下載npm:由于新版的nodejs已經集成了npm,所以之前npm也一并安裝好了,同樣可以通過輸入 "npm -v" 來測驗是否成功安裝
-
下載git,配置好用戶名密碼郵箱
2.搭建博客
2.1github創建倉庫
在github網頁新建一個倉庫并命名為你的用戶名.github.io的倉庫,注意:
-
驗證注冊的郵箱
-
倉庫名必須為用戶名.github.io
-
倉庫創建后需要等待一會
2.2 配置ssh-key
首先打開git bush ,執行
$ cd ~/. ssh #檢查本機已存在的ssh密鑰如果提示:No such file or directory 說明你是第一次使用git :
ssh-keygen -t rsa -C "郵件地址"然后連續3次回車,最侄訓生成一個檔案在用戶目錄下,打開用戶目錄,找到
.ssh\id_rsa.pub檔案,記事本打開并復制里面的內容,打開你的github主頁,進入setting -> SSH and GPG keys -> New SSH key:
打開命令列或者git bash,輸入
$ ssh -T [email protected] # 注意郵箱地址不用改如果提示
Are you sure you want to continue connecting (yes/no)?,輸入yes,然后會看到: Hi xxx ...! -
如圖,即為配置成功,然后輸入
$ git config --global user.name "liuxianan"// 你的github用戶名,非昵稱
$ git config --global user.email "[email protected]"// 填寫你的github注冊郵箱
3.配置hexo
3.1 hexo
Hexo是一個簡單、快速、強大的基于 Github Pages 的博客發布工具,支持Markdown格式,有眾多優秀插件和主題,
官網: http://hexo.io
github: https://github.com/hexojs/hexo
-
推薦完全在git bash 執行命令
-
hexo不同版本差別較大,注意區分
-
hexo有兩種_config.yml檔案 ,一個是根目錄下的全域的
_config.yml,一個是各個theme下的3.2hexo安裝
$ npm install -g hexo新建hexo檔案夾,這個檔案夾將作為你存放代碼的地方
切到hexo檔案夾打開命令列,運行
$ hexo init
hexo會自動下載一些檔案到這個目錄,包括node_modules:
$ hexo g # 生成
$ hexo s # 啟動服務
執行以上命令之后,hexo就會在public檔案夾生成相關html檔案,這些檔案將來都是要提交到github去
hexo s是開啟本地預覽服務,打開瀏覽器訪問 http://localhost:4000 即可看到內容
如果埠占用,運行以下:
- 按下
Win+R調出命令列視窗,輸入netstat -aon|findstr "4000",找到指定行最后一列的數字(PID) - 輸入
tasklist|findstr "PID",發現是xxx用了4000埠, - 再次輸入
taskkill /f /t /im xxx.exe結束該行程,
第一次初始化的時候hexo已經幫我們寫了一篇名為 Hello World 的文章,默認的主題比較丑
3.3修改主題
以主題 https://github.com/esappear/hexo-theme-clover 為例:
可在 https://hexo.io/themes/ 查找更多主題
推薦主題 http://theme-next.iissnan.com/getting-started.html
在博客根目錄:
$ git clone https://github.com/blleng/hexo-theme-lx themes/lx
更改站點設定檔案_config.yml:
theme: lx
然后重新執行hexo g來重新生成, 如果出現一些莫名其妙的問題例如樣式崩了,可以先執行hexo clean來清理一下public的內容,然后再來重新生成和發布
3.4 上傳到github
在上傳代碼到github之前,一定要記得先把你以前所有代碼下載下來(雖然github有版本管理,但備份一下總是好的),因為從hexo提交代碼時會把你以前的所有代碼都刪掉,
配置好ssh-key后,配置hexo檔案夾中的_config.yml:
deploy :
type: git
repository: [email protected]:lishoushoua/lishoushoua.git
branch: master
-
注意!deploy后要有一個空格,否則hexo d會報錯
此時直接執行
hexo d的話一般會報如下錯誤:
Deployer not found: github 或者 Deployer not found: git
安裝一個插件:
npm install hexo-deployer-git --save
- 注意!要在git bash 中運行命令, 否則會提示Permission denied (publickey)
打開你的git bash,輸入hexo d就會將本次有改動的代碼全部提交,沒有改動的不會:
重繪github page頁面,即可顯示
3.5常用hexo命令
hexo new "postName" #新建文章
hexo new page "pageName" #新建頁面
hexo generate #生成靜態頁面至public目錄
hexo server #開啟預覽訪問埠(默認埠4000,'ctrl + c'關閉server)
hexo deploy #部署到GitHub
hexo help # 查看幫助
hexo version #查看Hexo的版本
hexo n == hexo new
hexo g == hexo generate
hexo s == hexo server
hexo d == hexo deploy
hexo s -g #生成并本地預覽
hexo d -g #生成并上傳
3.6_config.yml
在Hexo中有兩個很重要的名為_config.yml的檔案,其中一個在站點安裝的根目錄下,另一個在主題目錄下,前者提供了Hexo自身的一些基本配置資訊,后者為你所安裝的主題的相關配置,為了方便區分,我們把前者稱為站點組態檔,后者稱為主題組態檔,
站點組態檔
檔案路徑站點根目錄/_config.yml
這里貼一下個人的部分配置,可以改一下相關內容自行體會一下效果:
# Site
title: cuteerha
subtitle: Live well,love lots,and laugh often
description: 個人學習記錄
keywords:
author: cuteerha
language: en
timezone: Changchun
“title”:博客的名稱,
“subtitle”:根據主題的不同,有的會顯示有的不會顯示,
“description”:主要用于SEO,告訴搜索引擎一個關于站點的簡單描述,通常建議在其中包含網站的關鍵詞,
“author”:作者名稱,用于主題顯示文章的作者,
“language”:語言會對應的決議正在應用的主題中的languages檔案夾下的不同語言檔案,所以這里的名稱要和languages檔案夾下的語言檔案名稱一致,
“timezone”:可不填寫,
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: https://lishoushoua.github.io/lishoushoua.github.io/
root: /lishoushoua.github.io/
permalink: :title/
permalink_defaults:
pretty_urls:
trailing_index: true # Set to false to remove trailing index.html from permalinks
“url”:一般填寫自己的站點鏈接,
“root”:設定根目錄的位置,如果你的網站存放在子目錄中,例如 http://yoursite.com/blog,則應該將你的 url 設為http://yoursite.com/blog 并把 root 設為 /blog/,
“permalink”:生成的鏈接的格式,帶井號的是默認的格式,帶有日期感覺怪怪的,改成了自己喜歡的格式,規則也比較簡單,標簽前面要加英文冒號,
“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:
書寫相關的設定
“new_post_name”:新的博文的檔案名
“default_layout:“ 默認布局
“filename_case: 0“ #把檔案名稱轉換為 (1) 小寫或 (2) 大寫
“render_drafts: false“ 是否顯示草稿
“post_asset_folder: false“ #是否啟動資源檔案夾
“relative_link: false“ #把鏈接改為與根目錄的相對位址
“future: true “
“highlight:“ #代碼塊的設定,Hexo自帶的代碼高亮插件
# Category & Tag
default_category: uncategorized
category_map:
tag_map:
分類和標簽的設定
“default_category”:如果撰寫文章時沒有設定分類,默認的分類選擇,
“category_map”:用于映射分類的別名,
“tag_map”:用法和分類別名是一樣的,
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: next
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy :
type: git
repository: [email protected]:lishoushoua/lishoushoua.git
branch: master
“theme”:主題拓展,可以在主題商店選擇其他主題,這里使用next
“deploy”:部署方式
4.遇到的問題
4.1關于hexo安裝失敗的解決方法
可能國內npm源有問題,鍵入一下代碼即可成功安裝
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install hexo-cli -g
4.2出現*'git'* 不是內部或外部命令,也不是可運行的程式
Windows下配置Git,配置環境變數path
- 找到git安裝路徑中bin的位置,如:D:\Program Files\Git\bin
- 找到git安裝路徑中git-core的位置,如:D:\Program Files\Git\libexec\git-core;
- 右鍵“計算機”->“屬性”->“高級系統設定”->“環境變數”->在下方的“系統變數”中找到“path”->選中“path”并選擇“編輯”->將找到的bin和git-core路徑復制到其中->保存并退出
- 注:“path”中,每個路徑之間要以英文輸入狀態下的分號——“;”作為間隔
4.3 npm ERR! Error: EPERM: operation not permitted
權限問題,使用管理員權限打開命令列,也可以對hexo所在檔案夾修改相應的用戶權限
4.4 git push沒有權限
Permission denied (publickey).fatal: Could not read from remote repository.
-
先看本地是否有ssh檔案
> cd ~/.ssh -
有則把公鑰加到github,參見前文配置密鑰
-
執行 ssh -T [email protected] 出現如下提示,本地公鑰沒有問題
> ssh -T [email protected] Hi lishoushoua! You've successfully authenticated, but GitHub does not provide shell access.
4.5nothing to commit, working tree clean
hexo進行提交博客,使用hexo d命令,但需要是使用 hexo-deployer-git 包的,如果沒有安裝,它檢索不到博文,進而提交時,讓github發現專案沒有發生變化,于是提交失敗,彈出這種提示,
安裝 hexo-deployer-git
npm install hexo-deployer-git
4.6 github page沒有樣式
url地址沒有配置正確,修改_config.yml檔案的url地址和根目錄
url: https://lishoushoua.github.io/lishoushoua.github.io/
root: /lishoushoua.github.io/
有可能是hexo快取的問題,也就是網站根目錄的db.json出了問題,建議清除下快取 清除快取的方法: 執行命令:hexo clean 然后生成靜態博客,在本地預覽:
$hexo clean
$hexo g
$hexo d
4.7 github更換next主題后404
排除主題和 CNAME 問題,出現 404 ,首先判斷是缺少 index.html ,所以肯定就是靜態頁面檔案不全,可以查看.deploy_git 目錄下是否有 index.html,運行以下命令解決:
npm install hexo-generator-index --save
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/5121.html
標籤:其他
上一篇:技術人員該如何站好最后一班崗?
