最近在搭建自己的個人網站,准備在上面內置一個博客模塊,把之前或者以后雜七雜八的總結都放里邊。
大致查了一下在WordPress和Hexo間選用了Hexo,體量較小一點。
先貼上Hexo的官方文檔:Hexo安裝文檔
首先在使用Hexo之前需要在服務器上安裝
安裝git
我們需要首先在系統中安裝git,hexo主題基本都托管在github上,使用git版本管理工具可以很方便地下載更換主題。此外最后我們也可以將博客托管在github並使用Github Pages服務,實現遠程訪問。
關於git的安裝我們可以直接使用命令:
sudo apt-get install git
安裝后使用 git --version 查看是否成功。
接下來需要對我們的git進行配置。
git config --global user.name "Your Name" git config --global user.email "youremail@domain.com"
關於SSH Keys的添加我們放在后面來說。
安裝Node
對於node的安裝我們最好使用node的版本管理工具nvm,但是通過官方文檔里的連接下載時出現了錯誤,后來用了git clone的方法安裝成功:
git clone http://github.com/creationix/nvm.git .nvm
安裝完成后輸入
nvm -v
可以在窗口中打印出
Node Version Manager
Note: <version> refers to any version-like string nvm understands. This includes:
- full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
- default (built-in) aliases: node, stable, unstable, iojs, system
- custom aliases you define with `nvm alias foo`
Any options that produce colorized output should respect the `--no-colors` option.
Usage:
nvm --help Show this message
nvm --version Print out the latest released version of nvm
nvm install [-s] <version> Download and install a <version>, [-s] from source. Uses .nvmrc if available
--reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>
--lts When installing, only select from LTS (long-term support) versions
--lts=<LTS name> When installing, only select from versions for a specific LTS line
nvm uninstall <version> Uninstall a version
nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.
nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.
nvm use [--silent] <version> Modify PATH to use <version>. Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm exec [--silent] <version> [<command>] Run <command> on <version>. Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm run [--silent] <version> [<args>] Run `node` on <version> with <args> as arguments. Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm current Display currently activated version
nvm ls List installed versions
nvm ls <version> List versions matching a given <version>
nvm ls-remote List remote versions available for install
--lts When listing, only show LTS (long-term support) versions
nvm ls-remote <version> List remote versions available for install, matching a given <version>
--lts When listing, only show LTS (long-term support) versions
--lts=<LTS name> When listing, only show versions for a specific LTS line
nvm version <version> Resolve the given description to a single local version
nvm version-remote <version> Resolve the given description to a single remote version
--lts When listing, only select from LTS (long-term support) versions
--lts=<LTS name> When listing, only select from versions for a specific LTS line
nvm deactivate Undo effects of `nvm` on current shell
nvm alias [<pattern>] Show all aliases beginning with <pattern>
nvm alias <name> <version> Set an alias named <name> pointing to <version>
nvm unalias <name> Deletes the alias named <name>
nvm reinstall-packages <version> Reinstall global `npm` packages contained in <version> to current version
nvm unload Unload `nvm` from shell
nvm which [<version>] Display path to installed node version. Uses .nvmrc if available
nvm cache dir Display path to the cache directory for nvm
nvm cache clear Empty cache directory for nvm
Example:
nvm install v0.10.32 Install a specific version number
nvm use 0.10 Use the latest available 0.10.x release
nvm run 0.10.32 app.js Run app.js using node v0.10.32
nvm exec 0.10.32 node app.js Run `node app.js` with the PATH pointing to node v0.10.32
nvm alias default 0.10.32 Set default node version on a shell
Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
表示安裝成功
接下來我們就可以使用npm直接安裝node:
nvm install stable
nvm會自動下載最新的穩定版node,完成后驗證node和附帶安裝的npm的版本號:
$ node -v v7.6.0$ npm -v 4.1.2
證明已經安裝成功。
安裝Hexo
但是在接下來使用npm安裝hexo時遇到了問題:
npm install -g hexo-cli
試了幾次一直提示node npm未獲得許可:
sh: 1: node: Permission denied > hexo-util@0.6.0 postinstall /root/.nvm/versions/node/v7.6.0/lib/node_modules/hexo-cli/node_modules/hexo-util > npm run build:highlight sh: 1: npm: Permission denied /root/.nvm/versions/node/v7.6.0/lib └── (empty) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/hexo-cli/node_modules/chokidar/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: dtrace-provider@0.8.0 (node_modules/hexo-cli/node_modules/dtrace-provider): npm WARN optional SKIPPING OPTIONAL DEPENDENCY: dtrace-provider@0.8.0 install: `node scripts/install.js` npm WARN optional SKIPPING OPTIONAL DEPENDENCY: spawn ENOENT npm ERR! Linux 3.13.0-86-generic npm ERR! argv "/root/.nvm/versions/node/v7.6.0/bin/node" "/root/.nvm/versions/node/v7.6.0/bin/npm" "install" "-g" "hexo-cli" npm ERR! node v7.6.0 npm ERR! npm v4.1.2 npm ERR! file sh npm ERR! code ELIFECYCLE npm ERR! errno ENOENT npm ERR! syscall spawn npm ERR! hexo-util@0.6.0 postinstall: `npm run build:highlight` npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the hexo-util@0.6.0 postinstall script 'npm run build:highlight'. npm ERR! Make sure you have the latest version of node.js and npm installed. npm ERR! If you do, this is most likely a problem with the hexo-util package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! npm run build:highlight npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs hexo-util npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm owner ls hexo-util npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! /root/software/node/npm-debug.log
在網上查了解決方案說權限問題的提示用 sudo npm install 試試,但是也不行,自己本來也是用root賬號操作的,最后在一篇博文里找到了解決方法,博文鏈接
npm config set user 0 npm config set unsafe-perm true
查看安裝結果:
root@iZ28zkldm3zZ:~/software/node# hexo -v hexo-cli: 1.0.2 os: Linux 3.13.0-86-generic linux x64 http_parser: 2.7.0 node: 7.6.0 v8: 5.5.372.40 uv: 1.11.0 zlib: 1.2.11 ares: 1.10.1-DEV modules: 51 openssl: 1.0.2k icu: 58.2 unicode: 9.0 cldr: 30.0.3 tz: 2016j
接下來可以進行一些初始化操作
安裝 Hexo 完成后,請執行下列命令,Hexo 將會在指定文件夾中新建所需要的文件。
(<folder>替換為需要在里面新建文件的文件夾路徑)
hexo init <folder> cd <folder> npm install
新建完成后,指定文件夾的目錄如下:

新建完成后使用命令啟動本地測試
hexo s
因為是阿里雲的雲服務器,所以在瀏覽器中輸入 http://服務器IP:4000/ 可進入Hello World 頁面

預覽完成后可按 Ctrl+C 退出,自己有遇到過預覽過程中 Putty 意外退出的情況,這時候可以查看占用4000端口的進程,記下 PID 之后殺掉進程就可以。
更換主題
hexo默認的是內置的 landscape 主題,我們可以在官方主題列表中找到想引用的主題樣式

我嘗試過 Next:https://github.com/iissnan/hexo-theme-next 和 Yilia:https://github.com/litten/hexo-theme-yilia ,最后選擇了yilia,github里有預覽博客地址,可以先看一下效果。
選擇好需要的主題后(接下來都用yilia為例),我們復制好主題的地址

隨后轉到hexo目錄下執行clone命令:
git clone https://github.com/litten/hexo-theme-yilia.git themes/yilia
后面的字段是主題的存放地址。等待克隆完成,再打開 hexo/_config.yml 找到如下字段:
# Extensions ## Plugins: https://hexo.io/plugins/ ## Themes: https://hexo.io/themes/ theme: yilia exclude_generator:
將theme修改為我們剛剛下載的主題名稱,保存退出,使用 hexo s 命令預覽效果。
主題個性化配置
修改為我們需要的主題之后,可以對主題的默認配置進行修改。
cd themes/yilia
轉到主題資源文件目錄,這個就是我們下載的主題資源包。打開_config.yml,在里面可以對主題進行個性化配置
# Header menu: 主頁: / 隨筆: /tags/隨筆/ # SubNav subnav: github: "#" weibo: "#" rss: "#" zhihu: "#" #qq: "#" #weixin: "#" #jianshu: "#" #douban: "#" #mail: "mailto:litten225@qq.com" #facebook: "#" #google: "#" #twitter: "#" #linkedin: "#" rss: /atom.xml # 是否需要修改 root 路徑 # 如果您的網站存放在子目錄中,例如 http://yoursite.com/blog, # 請將您的 url 設為 http://yoursite.com/blog 並把 root 設為 /blog/。 root: # Content # 文章太長,截斷按鈕文字 excerpt_link: more # 文章卡片右下角常駐鏈接,不需要請設置為false show_all_link: '展開全文' # 數學公式 mathjax: false # 是否在新窗口打開鏈接 open_in_new: false # 打賞 # 請在需要打賞的文章的md文件頭部,設置屬性reward: true # 打賞基礎設定:0-關閉打賞; 1-文章對應的md文件里有reward:true屬性,才有打賞; 2-所有文章均有打賞 reward_type: 2 # 打賞wording reward_wording: '謝謝你請我吃糖果' # 支付寶二維碼圖片地址,跟你設置頭像的方式一樣。比如:/assets/img/alipay.jpg alipay: # 微信二維碼圖片地址 weixin: # Miscellaneous baidu_analytics: '' google_analytics: '' favicon: /favicon.png #你的頭像url avatar: #是否開啟分享 share_jia: true #是否開啟多說評論,填寫你在多說申請的項目名稱 duoshuo: duoshuo-key #若使用disqus,請在博客config文件中填寫disqus_shortname,並關閉多說評論 duoshuo: false # 樣式定制 - 一般不需要修改,除非有很強的定制欲望… style: # 頭像上面的背景顏色 header: '#4d4d4d' # 右滑板塊背景 slider: 'linear-gradient(200deg,#a0cfe4,#e8c37e)' # slider的設置 slider: # 是否默認展開tags板塊 showTags: false # 智能菜單 # 如不需要,將該對應項置為false # 比如 #smart_menu: # friends: false smart_menu: innerArchive: '所有文章' friends: '友鏈' aboutme: '關於我' friends: 友情鏈接1: http://localhost:4000/ 友情鏈接2: http://localhost:4000/ 友情鏈接3: http://localhost:4000/ 友情鏈接4: http://localhost:4000/ 友情鏈接5: http://localhost:4000/ 友情鏈接6: http://localhost:4000/ aboutme: 很慚愧<br><br>只做了一點微小的工作<br>謝謝大家
作者對配置項基本都做了注釋,也很好理解,我們修改對應的條目就可以。隨后保存看看預覽效果。
Github Pages托管
Github Pages服務可以給我們提供一個靜態網頁的托管,以便遠程瀏覽我們的博客內容。
首先我們需要在本機生成一個公鑰以便跟github建立安全連接,使用命令:
$ ssh-keygen -t rsa -C "youremail@domain.com"
這里的郵箱就是我們安裝git時配置的郵箱,隨后復制公鑰內容:
vim ~/.ssh/id_rsa.pub
全選后按 P 鍵復制,登陸github,進入Settings->SSH and GPG keys->New SSH key->粘貼
隨后進入系統測試連接是否成功:
ssh -T git@github.com
根據提示輸入yes,如果成功建立連接就可以進行接下來的操作了。
接下來我們使用我們需要在github上創建一個倉庫repository,注意倉庫名稱必須為 【page名稱】.github.io ,后面的.github.io必須保留

新建好我們的倉庫之后就可以從本地用git將資源推送到github上。
因為博客是作為我個站的一部分,所以區分了 主頁和博客主頁 ,不需要可以刪除即可。
之后進入阿里雲申請一個域名,設置域名解析將映射IP填寫為我們創建的Github Pages的IP即可(這個IP我們可以通過 ping 命令獲得),之后我們就可以通過我們的域名訪問博客啦~
歡迎大家光臨我的博客哦^_^
