部署Hexo博客到阿里云centOS7

Posted by Chenyawei on 2019-11-23
Words 1.9k and Reading Time 8 Minutes
Viewed Times

1、博客的架构

先搞明白Hexo博客从搭建到自动发布的架构,才能更好的理解我们每一步进行的操作。
不然只跟着步骤过了一遍,却不知道为什么这么做。

首先看这张架构图:

2、整个搭建流程

第一部分: 服务器环境搭建,包括安装 Git 、Nginx配置 、创建 git 用户 。

第二部分: 本地Hexo初始化, 包括安装 NodeJS 、hexo-cli, 生成本地静态网站

第三部分: 使用Git自动化部署发布博客

3、服务器环境搭建

3-1.安装Git和NodeJS (CentOS 环境)

1
2
3
yum install git
#安装NodeJS
curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -

3-2. 创建git普通用户

1
2
adduser git      
sudo passwd git #设置git用户的密码

在本地电脑执行:

1
ssh-copy-id -i ~/.ssh/id_rsa.pub git@SERVER

如果你之前没有生成过公钥,则可能就没有 id_rsa.pub 文件,具体的生成方法,可以
参考这里

切换至git用户, 可以看到~/.ssh/authorized_keys 文件里已经有了本地电脑的公钥拷贝,这样就建立了ssh信任

1
2
3
su git
mkdir ~/.ssh
cat ~/.ssh/authorized_keys

然后在本地就可以执行ssh 命令测试是否可以免密登录
1
ssh -v git@SERVER

为了安全起见禁用git用户的 shell 登录权限,从而只能用git clone,git push等登录,执行如下命令
1
2
3
4
5
6
cat /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.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
28
server {

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
4
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)

执行可解决:
/usr/sbin/nginx -c /etc/nginx/nginx.conf #使用指定nginx.conf文件的方式重启nginx

启动: 输入:nginx
1
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
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
28
29
30
31
32
33
34
35
36
37
38
39

centOS7访问nginx失败解决-.0:80 failed (98: Address already in use)解决

以防万一,先安装好iptables服务(不管你装没装,先执行,免得后面添乱)
[root@localhost ~]# yum install iptables-services
[root@localhost ~]# systemctl mask firewalld.service
[root@localhost ~]# systemctl enable iptables.service
[root@localhost ~]# systemctl enable ip6tables.service
进入iptables配置80端口,因为nginx默认是由80端口访问
[root@localhost ~]# vim /etc/sysconfig/iptables
这是配置信息:
# Generated by iptables-save v1.4.21 on Fri May 12 21:28:29 2017
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [6:696]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT(我给vsftpd配置的端口)
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT(给nginx配置的端口,原样输入就行了)
-A INPUT -p tcp -m state --state NEW -m tcp --dport 30000:30999 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Fri May 12 21:28:29 2017
然后:wq,保存退出就行了
重启iptables,配置才生效
[root@localhost ~]# systemctl restart iptables.service
开启防火墙,不管你开没有,再开一遍:
[root@localhost ~]# systemctl start firewalld
开启http访问
[root@localhost ~]# firewall-cmd --permanent --add-service=http
加入80端口
[root@localhost ~]# firewall-cmd --permanent --zone=trusted --add-port=80/tcp
启动nginx!!!!(重点来了!!!)
centOS7的nginx启动与其他的有区别!!!注意:我是装的nginx1.80,在centOS6.X系列中,是通过
cd /usr/local/nginx/sbin/

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
3
cd blog
npm install hexo-deployer-git --save
npm install hero -server

4-2. 生成自己的第一篇文章 hello world !

使用 hexo new <文章名称> 来新建文章,该命令会成成一个 .md文件放置在 sources/_posts文件夹。

1
2
hexo new "hello Hexo"
vim sources/_posts/hello-hexo.md

编辑完毕以后, 使用hexo g将 .md文件渲染成静态文件,然后启动hexo s:
1
2
hexo g
hexo server

打开浏览器访问 http://localhost:4000 来查看我们的博客了!

5. 自动化部署

5-1:服务器上建立git裸库

创建一个裸仓库,裸仓库就是只保存git信息的Repository, 首先切换到gituser用户确保gituser用户拥有仓库所有权
一定要加 —bare,这样才是一个裸库。

1
2
3
4
5
su 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
4
vim ./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
3
chmod +x ./blog.git/hooks/post-receive
cd ~
chown -R git:git /home/git/repos/blog.git/ #添加权限

5-3. 配置_config.yml,完成自动化部署

然后打开 _config.yml, 找到 deploy

1
2
3
4
5
deploy:
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
2
hexo clean
hexo generate --deploy

访问服务器地址,就可以看到我们写的文章”Hello hexo”,以后写文章只需要:
1
2
3
hexo new "Blog article name"
···写文章
hexo clean && hexo generate --deploy

参考文档:

  1. https://blog.csdn.net/moyanxiaoq/article/details/85221262
  2. https://segmentfault.com/a/1190000005723321
  3. http://www.swiftyper.com/2016/04/17/deploy-hexo-with-git-hook/
  4. https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
  5. https://www.bilibili.com/video/av50025574?from=search&seid=11458733547652119175

notice

欢迎访问 chenyawei 的博客, 若有问题或者有好的建议欢迎留言,笔者看到之后会及时回复。 评论点赞需要github账号登录,如果没有账号的话请点击 github 注册, 谢谢 !

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 !