最近更換了服務器,須要把本身的Hexo Next從新部署到新服務器上,本文記錄一下在vps上搭建hexo博客的過程。html
在vps上搭建hexo博客須要下面這些工具:nginx
本文服務器環境爲CentOS 7.6git
總體流程爲:shell
想要完成Git推送,首先得設置SSH登陸。過程以下:vim
# 添加hexo用戶
adduser hexo
# 切換到hexo用戶目錄
cd /home/hexo
# 建立.ssh文件夾
mkdir .ssh
# 建立authorized_keys文件並編輯
vim .ssh/authorized_keys
# 若是你尚未生成公鑰,那麼首先在本地電腦中執行 cat ~/.ssh/id_rsa.pub | pbcopy生成公鑰
# 再將公鑰複製粘貼到authorized_keys
# 保存關閉authorized_keys後,修改相應權限
chmod 600 .ssh/authorzied_keys
chmod 700 .ssh
複製代碼
測試是否設置成功:bash
ssh -v hexo@服務器ip
複製代碼
yum install git
複製代碼
Git的鉤子腳本位於版本庫.git/hooks目錄下,當Git執行特定操做時會調用特定的鉤子腳本。當版本庫經過git init或者git clone建立時,會在.git/hooks目錄下建立示例腳本,用戶能夠參照示例腳本的寫法開發適合的鉤子腳本。服務器
鉤子腳本要設置爲可運行,並使用特定的名稱。Git提供的示例腳本都帶有.sample擴展名,是爲了防止被意外運行。若是須要啓用相應的鉤子腳本,須要對其重命名(去掉.sample擴展名)。hexo
post-update 該鉤子腳本由遠程版本庫的git receive-pack命令調用。當從本地版本庫完成一個推送以後,即當全部引用都更新完畢後,在遠程服務器上該鉤子腳本被觸發執行。ssh
所以咱們須要配置post-update鉤子以即可以及時更新咱們在VPS上存放Hexo 靜態文件的目錄。工具
# 回到hexo目錄
cd /home/hexo
# 變成hexo用戶
su hexo
# 使用hexo用戶建立git裸倉庫,以blog.git爲例
git init --bare blog.git
# 進入鉤子文件夾hooks
cd blog.git/hooks/
# 啓用post-update
mv post-update.sample post-update
# 添加執行權限
chmod +x post-update
# 配置post-update
vim post-update
複製代碼
exec git update-server-info
複製代碼
git --work-tree="靜態文件VPS存放目錄" --git-dir="剛纔新建的VPS git地址" checkout -f
例:
git --work-tree=/home/hexo/blog --git-dir=/home/hexo/blog.git checkout -f
複製代碼
例:
yum install nginx
複製代碼
使用nginx -v
查看,顯示版本號則安裝成功。
server {
# 默認80端口
listen 80 default_server;
listen [::]:80 default_server;
# 修改server_name爲本身以前註冊好的域名,沒有就不用更改
server_name morethink.cn;
# 修改網站根目錄,在這裏存放你的Hexo靜態文件,請自行選擇或建立目錄
root /home/hexo/blog;
# 其餘保持不變
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
複製代碼
找到本地Hexo博客的站點配置文件_config.yml
,找到如下內容並修改:
deploy:
type: git
repo: hexo@你的服務器IP:/home/git/blog.git
branch: master
複製代碼
而後在根目錄執行如下命令:
hexo clean
hexo g -d
複製代碼
nginx root 403
問題: 在我配置nginx碰到一個403問題,改了文件權限仍是403,後來發現是nginx.conf中 user默認設置錯了,把 user nginx
改爲user root
就行了。參考文檔: