Git部署hexo 到Ubuntu服务器上

服务器Git建仓

1
2
3
4
5
6
sudo mkdir /var/repo/
sudo chown -R $USER:$USER /var/repo/
sudo chmod -R 755 /var/repo/

cd /var/repo/
git init --bare hexo_static.git

创建静态文件夹

1
2
3
4
sudo mkdir -p /var/www/hexo

sudo chown -R $USER:$USER /var/www/hexo
sudo chmod -R 755 /var/www/hexo

配置nginx

/etc/nginx/sites-available创建文件blog.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
listen 80;
listen [::]:80;

server_name ihls.xyz;

root /var/www/myblog;
index index.html;
error_page 500 502 503 504 404 /404.html;
# 承接上面的location
location = /404.html {
# 放错误页面的目录路径。
root /var/www/myblog;
}
location / {
try_files $uri $uri/ =404;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 1d;
}
location ~ \.(js|css) {
access_log off;
expires 1d;
}
}

链接启动配置

1
sudo ln -s /etc/nginx/sites-available/blog.conf /etc/nginx/sites-enabled/

并重启nginx

创建Git钩子

创建运行钩子文件

1
vim /var/repo/hexo_static.git/hooks/post-receive

写入以下内容

1
2
3
#!/bin/bash

git --work-tree=/var/www/hexo --git-dir=/var/repo/hexo_static.git checkout -f

并给文件权限

1
sudo chmod +x /var/repo/hexo_static.git/hooks/post-receive

结束后返回本地配置

本地配置

配置本地文件_config.yml

1
2
3
4
deploy:
type: git
repo: ubuntu@server_ip:/var/repo/hexo_static.git
branch: master

结束后运行

1
2
3
hexo clean
hexo g
hexo d