本篇前置條件:
1 個人服務器已安裝git,則可創建git倉庫,參考https://blog.csdn.net/xw505501936/article/details/101544049
2 本地PC機已安裝node,hexo,參考對應官方網站手冊即可
以下爲具體步驟:
第一步:服務端創建對應git倉庫:hexo-blog.git
cd /home/git
su - git (因我的git賬戶名爲git,需切換權限)
mkdir hexo-blog.git
cd hexo-blog.git
git --bare init
則此時git地址爲:[email protected]:/home/git/hexo-blog.git
git 爲您的賬號
127.1.1.1 爲您的服務器ip
/home/git/hexo-blog.git 爲倉庫目錄
可以在本地PC驗證服務端git倉庫有效性:
git clone [email protected]:/home/git/hexo-blog.git
第二步:本地PC端 安裝hexo-deployer-git,並配置hexo項目的 _config.yml 文件
hexo項目中安裝hexo-deployer-git:
npm install hexo-deployer-git –save
配置_config.yml 文件:
deploy:
type: git
message: update files
repo: [email protected]:/home/git/hexo-blog.git
branch: master
注意,此處有坑!
注意,此處有坑!
注意,此處有坑!
重要的事情說三遍!!!
因本地PC機用的window電腦,故冒號後面需帶有空格,否則hexo d 不生效
即:“type: git”
第三步:提交部署hexo到服務端git倉庫
hexo g
hexo d (輸入您的git賬戶密碼即可)
第四步:用腳本將倉庫的資源拉取到nginx代理的對應目錄下
讓Git倉庫每次檢測到push行爲後,將最新的資源文件Git clone在你要訪問的文件夾下,這樣達到自動化部署blog項目
1 進入git倉庫下的Hooks目錄,配置回調post-receive,執行
cd /home/git/hexo-blog.git/hooks
touch post-receive
vim post-receive
2 寫入以下腳本:
#!/bin/sh
GIT_REPO=/home/git/hexo-blog.git
TMP_GIT_CLONE=/home/git/tmp/hexo-blog
PUBLIC_WWW=/home/git/www/hexo
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}
cp -rf ${TMP_GIT_CLONE} ${PUBLIC_WWW}
其中public_www就是用於最終存放nginx映射的靜態資源文件目錄
3 修改目錄權限修
chmod +x post-receive
chmod 777 -R /home/git/www/hexo
chmod 777 -R /home/git/tmp/hexo-blog
對應的目錄,務必保證有權限,否則不能正確執行腳本,不能拷貝對應目錄文件
第五步:nginx配置
server {
listen 80 ;
root /home/git/www/hexo;
server_name hexo.ixiewei.com;
location / {
root /home/git/www/hexo;
if (-f $request_filename) {
rewrite ^/(.*)$ /$1 break;
}
}
}
此處保證nginx運行賬戶,擁有/home/git/www/hexo各級目錄權限,否則可能出現403
效果如下:
備註:親身實踐細節,記錄每個點滴