以前博客是託管在GitHub Page,訪問速度不太樂觀,後來買了臺阿里雲ECS,把博客遷了過來,做爲一個程序員,過多的手動操做簡直是對鍵盤的侮辱,下面介紹如何將博客直接推送到阿里雲ECS(CentOS系統),實現自動部署。javascript
在阿里雲服務器上搭建git倉庫,本地博客目錄下運行hexo g -d
生成靜態文件,並提交到git倉庫,從而觸發git hook,最後再執行bash命令將文件拷貝到博客網站目錄。java
在阿里雲服務器上建立git倉庫,注意不要漏掉--bare
參數。git
mkdir blog.git && cd blog.git git init --bare
修改本地博客目錄下的_config.yml
配置,其中xx.xxx.xx.xxx
是你的服務器ip地址,/www/blog.git
是你上一步建立的git倉庫路徑,master
是分支。程序員
deploy: type: git message: update repo: root@xx.xxx.xx.xxx:/www/blog.git,master
此插件的做用是執行deploy時,將hexo生成的靜態文件提交到_config.yml
配置中的deploy.repo
地址,即 root@xx.xxx.xx.xxx:/www/blog.git,master
。npm
npm install hexo-deployer-git --save
本地的deploy命令只是把靜態文件提交到git倉庫,既然有git hooks,那麼咱們能夠在有文件提交上來時,再將文件拷貝到博客網站目錄。
進入到git倉庫hooks目錄,並建立鉤子post-receive
。vim
cd /www/blog.git/hooks touch post-receive vim post-receive
而後輸入下面腳本:bash
#!/bin/bash -l GIT_REPO=/www/blog.git TMP_GIT_CLONE=/www/tmp/blog PUBLIC_WWW=/www/blog rm -rf ${TMP_GIT_CLONE} git clone $GIT_REPO $TMP_GIT_CLONE rm -rf ${PUBLIC_WWW}/* cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}
其中/www/blog.git
爲倉庫路徑,/www/blog
爲你的博客網站路徑,/www/tmp/blog
是臨時目錄,git會先將文件拉到臨時目錄,而後再將全部文件拷貝到博客網站目錄/www/blog
。服務器
更改目錄權限:hexo
chmod +x post-receive chmod 777 -R /www/blog
完成上述步驟以後,就能夠測試一下了,在本地博客目錄下運行hexo g -d
,此時可能還須要輸入服務器密碼,最後輸出如下結果說明部署成功:post
... INFO Deploy done: git
原文連接: http://www.tammeny.com