1、博客的架构
先搞明白Hexo博客从搭建到自动发布的架构,才能更好的理解我们每一步进行的操作。
不然只跟着步骤过了一遍,却不知道为什么这么做。
首先看这张架构图:
2、整个搭建流程
第一部分: 服务器环境搭建,包括安装 Git 、Nginx配置 、创建 git 用户 。
第二部分: 本地Hexo初始化, 包括安装 NodeJS 、hexo-cli, 生成本地静态网站
第三部分: 使用Git自动化部署发布博客
3、服务器环境搭建
3-1.安装Git和NodeJS (CentOS 环境)
1 | yum install git |
3-2. 创建git普通用户
1 | adduser git |
在本地电脑执行:1
ssh-copy-id -i ~/.ssh/id_rsa.pub git@SERVER
如果你之前没有生成过公钥,则可能就没有 id_rsa.pub 文件,具体的生成方法,可以
参考这里
切换至git用户, 可以看到~/.ssh/authorized_keys 文件里已经有了本地电脑的公钥拷贝,这样就建立了ssh信任1
2
3su git
mkdir ~/.ssh
cat ~/.ssh/authorized_keys
然后在本地就可以执行ssh 命令测试是否可以免密登录1
ssh -v git@SERVER
为了安全起见禁用git用户的 shell 登录权限,从而只能用git clone,git push等登录,执行如下命令1
2
3
4
5
6cat /etc/shells #查看`git-shell`是否在登录方式里面,有则跳过
which git-shell #查看是否安装
vi /etc/shells #添加上显示出来的路劲,通常在/usr/bin/git-shell
vi /etc/passwd
把 git:x:1000:1000::/home/git:/bin/bash
修改为 git:x:1000:1000::/home/git:/usr/bin/git-shell
至此,gituser用户添加完成
3-3. nginx安装和配置
注意:新买到阿里云centOS7是没有开通80端口的,添加端口:参考这里
安装1
yum install -y git nginx
配置 找到nginx的配置文件,修改配置如下:vim /etc/nginx/nginx.conf1
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
28server {
listen 80;
#listen [::]:80;
server_name localhost;
index index.html index.htm index.php default.html default.htm default.php;
#这里要改成网站的根目录
root /home/git/projects/repos/blog/;
#include other.conf;
#error_page 404 /404.html;
location ~ .*\.(ico|gif|jpg|jpeg|png|bmp|swf)$
{
access_log off;
expires 1d;
}
location ~ .*\.(js|css|txt|xml)?$
{
access_log off;
expires 12h;
}
location / {
try_files $uri $uri/ =404;
}
}
nginx -s reload //刷新配置
如果报如下错:1
2
3
4nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
执行可解决:
/usr/sbin/nginx -c /etc/nginx/nginx.conf #使用指定nginx.conf文件的方式重启nginx
启动: 输入:nginx1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20如果发现如下报错:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
说明80端口被占用,杀掉这个进程:
killall -9 nginx
再次启动nginx
查看是否启动:
ps aux|grep nginx
root 2484 0.0 0.2 120832 2104 ? Ss 18:04 0:00 nginx: master process nginx
root 2485 0.0 0.3 121228 3128 ? S 18:04 0:00 nginx: worker process
root 2494 0.0 0.0 112676 980 pts/7 R+ 18:08 0:00 grep --color=auto nginx
启动成功。
1 |
|
4. 本地Hexo程序
4-1:初始化Hexo博客
首先要安装 hexo-cli,安装hexo-cli 需要 root 权限,使用 sudo 运行1
sudo npm install -g hexo-cli
然后初始化Hexo程序1
hexo init blog
等执行成功以后安装两个插件, hexo-deployer-git 和 hexo-server ,这俩插件的作用分别是使用Git自动部署,和本地简单的服务器。
hexo-deployer-git帮助文档
hexo-server帮助文档1
2
3cd blog
npm install hexo-deployer-git --save
npm install hero -server
4-2. 生成自己的第一篇文章 hello world !
使用 hexo new <文章名称> 来新建文章,该命令会成成一个 .md文件放置在 sources/_posts文件夹。1
2hexo new "hello Hexo"
vim sources/_posts/hello-hexo.md
编辑完毕以后, 使用hexo g将 .md文件渲染成静态文件,然后启动hexo s:1
2hexo g
hexo server
打开浏览器访问 http://localhost:4000 来查看我们的博客了!
5. 自动化部署
5-1:服务器上建立git裸库
创建一个裸仓库,裸仓库就是只保存git信息的Repository, 首先切换到gituser用户确保gituser用户拥有仓库所有权
一定要加 —bare,这样才是一个裸库。1
2
3
4
5su git
cd ~
mkdir -p projects/blog
mkdir repos && cd repos
git init --bare blog.git
5-2. 使用 git-hooks 同步网站根目录
在这里我们使用的是 post-receive这个钩子,当git有收发的时候就会调用这个钩子。 在 ~/blog.git 裸库的 hooks文件夹中,
新建post-receive文件。1
2
3
4vim ./blog.git/hooks/post-receive
#!/bin/sh
git --work-tree=/home/git/projects/blog --git-dir=/home/git/repos/blog.git checkout -f
保存后,要赋予这个文件可执行权限1
2
3chmod +x ./blog.git/hooks/post-receive
cd ~
chown -R git:git /home/git/repos/blog.git/ #添加权限
5-3. 配置_config.yml,完成自动化部署
然后打开 _config.yml, 找到 deploy1
2
3
4
5deploy:
type: git
repo: git@SERVER:/home/git/blog.git //<repository url>
branch: master //这里填写分支 [branch]
message: 提交的信息 //自定义提交信息 (默认为 Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }})
保存后,尝试将我们刚才写的”hello hexo”部署到服务器1
2hexo clean
hexo generate --deploy
访问服务器地址,就可以看到我们写的文章”Hello hexo”,以后写文章只需要:1
2
3hexo new "Blog article name"
···写文章
hexo clean && hexo generate --deploy
参考文档:
- https://blog.csdn.net/moyanxiaoq/article/details/85221262
- https://segmentfault.com/a/1190000005723321
- http://www.swiftyper.com/2016/04/17/deploy-hexo-with-git-hook/
- https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
- https://www.bilibili.com/video/av50025574?from=search&seid=11458733547652119175
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !