本文參考了四弦同學(xué)的文章:在Linux服務(wù)器上搭建Hexo:OS X、Windows與Linux本地環(huán)境粮坞,講解得非常詳細(xì)蚊荣,在遇到這篇文章之前,踩了太多坑還沒有頭緒莫杈,現(xiàn)在終于解決了部署問題互例,非常非常感謝~
那我們開始吧~
在本地安裝hexo
安裝nvm
參考官方文檔
- 安裝:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
或
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
- 使環(huán)境變量生效:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
- 測試nvm是否安裝生效:
command -v nvm
通過nvm安裝node.js
- 安裝指定版本的node.js:
nvm install 6.10.0
可以通過nvm ls-remote
查看遠(yuǎn)程所有版本以選擇需要的版本
安裝hexo
- 安裝:
npm install -g hexo-cli
注意:如果安裝過程中有報(bào)錯(cuò):
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! /npm-debug.log
請重新輸入一下命令再重新安裝即可得到解決:
npm config set user 0
npm config set unsafe-perm true
參考:http://www.cnblogs.com/lidonghao/p/3543747.html
https://segmentfault.com/q/1010000006038485
http://www.07net01.com/2015/04/825142.html
https://www.liquidweb.com/kb/how-to-install-nvm-node-version-manager-for-node-js-on-ubuntu-12-04-lts/
hexo建站
參考官網(wǎng):Hexo官方文檔
初始化
- 初始化主目錄:
hexo init <folder>
- 進(jìn)入hexo主目錄:
cd <folder>
- 安裝node_modules:
npm install
站點(diǎn)配置文件_config.yml的配置
對(duì)一個(gè)新站點(diǎn)來說,需要編輯的項(xiàng)目有:
title: Hexo #站點(diǎn)的標(biāo)題
subtitle: #站點(diǎn)的副標(biāo)題
description: #站點(diǎn)的描述姓迅,寫一段話來介紹你的博客吧:)敲霍,主要為SEO使用
author: John Doe #顯示的文章作者名字俊马,例如我配置的是fourstring
language: #語言丁存。簡體中文是zh-Hans
timezone: #時(shí)區(qū),可以不配置柴我,默認(rèn)以本地時(shí)區(qū)為準(zhǔn)
url: http://yoursite.com #你的站點(diǎn)地址解寝,如果是全站HTTPS記得寫成https://domain.com
root: / #如果您的網(wǎng)站存放在子目錄中,例如 http://yoursite.com/blog艘儒,則請將您的 url 設(shè)為 http://yoursite.com/blog 并把 root 設(shè)為 /blog/聋伦。(引用自官方文檔)
permalink: :year/:month/:day/:title/ #固定鏈接格式夫偶。這項(xiàng)配置的格式為:變量1/變量2/變量3...,其中合法的變量格式為“:變量名”(注意觉增,:是變量的組成部分1!)這樣生成的效果為/2016/08/10/文章標(biāo)題。默認(rèn)的固定鏈接格式存在一些問題逾礁,下文講解
per_page: 10 #設(shè)置每頁文章篇數(shù)说铃,設(shè)為0可以關(guān)閉分頁功能
theme: #使用的主題。下文講解
deploy: #部署配置嘹履,其值是一個(gè)雜湊表腻扇,注意縮進(jìn),下文詳細(xì)講解
限于篇幅砾嫉,省略部分無關(guān)注釋
# Site
title: Patrick's Blog
subtitle: Love Coding, Enjoy Life
description: PatrickPhang
author: Patrick Phang
language: zh-Hans
timezone: Asia/Shanghai
avatar: /avatar.png
# URL
url: http://your_server_ip # 此處修改為你的服務(wù)器IP
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
default_layout: post
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: false
tab_replace:
# Category & Tag
default_category: uncategorized
category_map:
tag_map:
# Date / Time format
date_format: YYYY-MM-DD
time_format: HH:mm:ss
# Pagination
per_page: 10
pagination_dir: page
# Extensions
theme: next
# Deployment
deploy:
type: git
repo: git@your_server_ip:/home/blog/hexo.git # 此處為你服務(wù)器的git目錄幼苛,先忽略,下面再解釋
branch: master
新建文章
hexo new post "標(biāo)題"
預(yù)覽
生成靜態(tài)文件焕刮,位于public目錄:hexo generate
本地開啟服務(wù)器舶沿,localhost:4000 訪問:hexo server
個(gè)性化你的主題
本文主要介紹怎么在服務(wù)器上部署你的博客,這部分省略济锄,若想了解請參考如下鏈接:
IIssNan's Notes
hexo+NEXT主題之博客優(yōu)化
hexo的私人訂制
使用 Hexo 搭建博客的深度優(yōu)化與定制
Hexo的NexT主題個(gè)性化:添加文章閱讀量
如何使用 Hexo 部署個(gè)人博客
為NexT主題添加文章閱讀量統(tǒng)計(jì)功能
Hexo 主題配置
Hexo-NexT搭建個(gè)人博客(二)
hexo-wordcount實(shí)現(xiàn)文章標(biāo)題欄顯示更多的文章信息(精)
動(dòng)動(dòng)手指暑椰,給你的Hexo站點(diǎn)添加最近訪客(多說篇)
Hexo中播放網(wǎng)易云音樂的實(shí)踐
一步步在GitHub上創(chuàng)建博客主頁(1)
Hexo-用自己的頁面做首頁
hexo搭建個(gè)人博客如何設(shè)置自定義的頁面為主頁
自動(dòng)隨機(jī)切換Hexo博客的banner圖片
Hexo 博客插入 B 站 html5 播放器
Hexo搭建博客的個(gè)性化設(shè)置
Hexo-設(shè)置閱讀全文
hexo-NexT主題添加炫酷的動(dòng)畫插件
Hexo主題Yilia
部署 hexo 靜態(tài)博客到自有服務(wù)器
遇到的問題
-bash: hexo: command not found解決辦法
hexo 部署至Git遇到的坑
fatal: Unable to create temporary file '/home/username/git/myrepo.git/./objects/pack/tmp_pack_XXXXXX': Permission denied
Why am I getting git error “remote: error: unable to create temporary file: No such file or directory” on push?
部署到服務(wù)器
Git版本控制系統(tǒng)
準(zhǔn)備(建立SSH信任關(guān)系)
參考:《ssh-copy-id幫你建立信任》–linux命令五分鐘系列之四十一
SSH如何通過公鑰連接云服務(wù)器
- 在本地生成公鑰和密鑰:
ssh-keygen -t rsa
- 將本機(jī)生成的公鑰發(fā)送到服務(wù)器上(建立信任關(guān)系):
ssh-copy-id -i C:/Users/UserName/.ssh/id_rsa.pub root@server_ip
- 測試ssh遠(yuǎn)程登錄是否成功:
ssh root@server_ip
服務(wù)端配置
安裝nginx(也可以通過編譯安裝):
apt-get install nginx
-
新建git用戶并添加權(quán)限:
adduser git
編輯配置文件,加入git到sudo用戶組:nano /etc/sudoers
# User privilege specification root ALL=(ALL:ALL) ALL git ALL=(ALL:ALL) ALL # 新增這一行
-
生成一對(duì)ssh認(rèn)證密鑰:
su git cd /home/git mkdir /blog/.ssh cd blog/.ssh ssh-keygen -t rsa
-
配置Git倉庫:
將網(wǎng)站目錄放到/home/git/mkdir -p /home/git/blog/hexo.git #Git倉庫荐绝,不存儲(chǔ)網(wǎng)站文件 mkdir /home/git/blog/hexo #實(shí)際存儲(chǔ)網(wǎng)站文件目錄
初始化空的Git倉庫:
git init --bare /home/git/blog/hexo.git
進(jìn)入該倉庫一汽,配置post-update hooks(有的可能是post-receive):
cd /home/git/blog/hexo.git/hooks
sudo nano /home/git/blog/hexo.git/hooks/post-update.sample
git --work-tree=/home/git/blog/hexo --git-dir=/home/git/blog/hexo.git checkout -f
賦予可執(zhí)行權(quán)限:
chmod +x post-update
-
nginx web server配置
修改配置文件:nano /etc/nginx/sites-enabled/default
將root改為你的hexo主目錄server { ...... # root /var/www/html; root /home/git/blog/hexo;
本地配置
-
本地SSH配置
打開Git Bash,建立配置文件mkdir ~/.ssh touch ~/.ssh/config
編輯config文件低滩,寫入如下配置:
Host hexo #SSH主機(jī)配置的識(shí)別名召夹,配置好后直接"ssh 識(shí)別名"即可快速連接 HostName your_server_ip #SSH主機(jī)的地址 Port 22 #SSH主機(jī)端口 User git #用戶,本例是Git IdentityFile ~/.ssh/id_rsa #私鑰文件的存放地址恕沫,建議復(fù)制到~/.ssh下統(tǒng)一管理
然后執(zhí)行:
chmod 0600 ~/.ssh/testkey #換成你自己的私鑰路徑 ssh hexo
如果可以正常連接监憎,說明我們的Git服務(wù)端已經(jīng)配置成功。
-
配置部署選項(xiàng)
編輯home/_config.yml文件婶溯,找到deploy項(xiàng)目鲸阔,修改如下:deploy: type: git #用戶名 repo: git@hexo:/home/git/blog/hexo.git #Git倉庫地址,:符號(hào)后為Git倉庫服務(wù)器路徑 branch: master #分支迄委,由于我們只用Git進(jìn)行發(fā)布褐筛,master即可。
保存叙身,進(jìn)入home目錄渔扎,執(zhí)行:
hexo clean hexo deploy -g
即可將靜態(tài)文件發(fā)布到服務(wù)端了。
至此信轿,一切(woc終于結(jié)束了)搭建步驟完成晃痴,快訪問你的域名看看效果吧~
rsync遠(yuǎn)程同步工具
暫未使用過残吩,過兩天填坑