背景

最近服务器被挖矿程序攻击了,阿里云疯狂给我发消息,说再不处理要把我的服务器封了,比起找出挖矿程序然后清理掉,我宁愿选择把服务器重制这样更简单粗暴的方法,于是有了这篇文章:从零开始搭建hexo博客部署到阿里云服务器上。

原理

  1. 首先在本地hexo生成博客的静态文件

  2. 将生成的静态文件部署到云服务器

  3. 云服务器通过git-hook同步到网站根目录

搭建流程

本地环境搭建

  • hexo

  • git

  • nodejs

服务器环境搭建

  • nginx

  • git

服务器环境搭建步骤

  1. 安装git

    1
    2
    yum install git  
    git --version #查看git是否安装成功
  2. 创建git用户

    1
    2
    3
    4
    5
    adduser git
    #修改权限
    chmod 740 /etc/sudoers
    #编辑
    vim /etc/sudoers

    找到如下内容:

    1
    2
    ## Allow root to run any commands anywhere
    root ALL=(ALL) ALL

    在下面添加代码:

    1
    git    ALL=(ALL)    ALL

    保存退出后改回权限

    1
    chmod 400 /etc/suders
  3. git服务器开启RSA认证

    1
    vim /etc/ssh/ssd_config

    在’sshd_config’中设置以下几项:

    1
    2
    3
    RSAAuthentication yes
    PubkeyAuthentication yes
    AuthorizedKeysFile .ssh/authorized_keys

    设置git用户密码:

    1
    sudo passwd git
  4. 配置ssh

    这里是为了免密登陆,我设置没问题,但是还是需要密码(汗

    本地客户端生成公私钥

    1
    ssh-keygen

    上面这个命令会在用户.ssh文件夹下创建公私钥

    1
    2
    cd ~/.ssh
    ls

    可以看到以下两个文件:

    1
    2
    id_rsa(私钥)
    id_rsa.pub(公钥)

    上传公钥到服务器

    1
    2
    3
    ssh-copy-id -i ~/.ssh/id_rsa.pub root@139.224.82.218
    ssh-copy-id -i ~/.ssh/id_rsa.pub git@139.224.82.218
    #这里我两个用户都上传了

    测试免密登陆

    1
    ssh git@139.224.82.218

    这里我知道为什么我设置了免密登陆却还是需要输入密码了,在我的电脑上,必须进入root模式,才能使用hexo语句,而我在没有进入root模式的情况下,也就是一般用户模式下,去设置了如上的ssh免密登陆,所以说我根本就没有给我的root用户授权免密登陆,所以正确的做法是先进入root模式,再进行上面的操作。正确步骤如下:

    1
    2
    3
    4
    5
    6
    #1.先进入hexo文件根目录,如果先进入root模式的话当前路径就变成了/var/root了
    cd blog
    #2.进入root模式
    sudo su
    #3.获得ssh公私钥
    ssh-keygen

    回到云服务器

    更改文件夹权限

    1
    2
    chmod 600 ~/.ssh/authorized_keys
    chmod 700 ~/.ssh
  5. 创建git仓库

    1
    2
    3
    4
    5
    6
    7
    8
    mkdir /home/blogRepo

    chmod 777 /home/blogRepo

    cd /home/blogRepo

    #默认创建在 /home/git/目录下
    git init --bare hexo.git

    使用bare参数,git就会创建一个裸仓库,裸仓库没有工作区,我们不会在裸仓库上进行操作,它只为共享而存在

    设置钩子

    blog.git/hooks目录下新建一个post-receive文件,并编辑这个文件

    1
    vim /home/blogRepo/hexo.git/hooks/post-receive

    在文件中写入如下内容:

    1
    2
    #!/bin/bash
    git --work-tree=/home/website/MyBlog --git-dir=/home/blogRepo/hexo.git checkout -f

    设置这个文件的可执行权限

    chmod +x /home/blogRepo/hexo.git/hooks/post-receive
    
  6. 创建post-receive文件中的路径

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 如果之前的操作一直都是在root用户下,那git仓库的所有者就是root,这一步需要把git仓库的所有者给git用户
    sudo chown -R git:git hexo.git

    su root

    cd /home

    mkdir website/MyBlog

    chmod 777 /home/website

    chmod 777 /home/website/MyBlog
  7. 下载nginx

    我是用yum下载的,所以我nginx的配置目录在/etc/nginx/nginx.conf

    1
    2
    3
    yum install nginx

    vim /etc/nginx/nginx.conf

    找到配置文件中的段落:

    1
    2
    3
    4
    5
    server {
    ......
    # 站点根目录
    root /home/website/MyBlog;
    }

    重启nginx服务

    1
    nginx -s reload
  8. 修改本地hexo配置文件

    一定要先进入hexo创建的文件夹中再使用hexo语句,不然会报错

    1
    vim _config.yml

    修改配置文件如下

    1
    2
    3
    4
    5
    6
    # Deployment
    ## Docs: https://hexo.io/docs/one-command-deployment
    deploy:
    type: git
    repo: git@soincredible777.com.cn:/home/blogRepo/hexo.git
    branch: master
  9. hexo 部署到云服务器

    1
    2
    hexo clean
    hexo g -d

    大功告成!

2023年3月13日更新

因为目前使用了Butterfly的博客主题,想要给每篇文章配置一个封面,故使用Niginx配置一个简单的图床,我们要做的就是让Nginx可以访问两个路径,一个是博客的路径,另外一个是图片存放的路径,打开Nginx的配置文件/etc/nginx/nginx.conf,在其中添加一个server

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
server {
listen 80;
listen [::]:80;
server_name _;
root /home/website/MyBlog/;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# 上面是博客的服务
# 下面是图床的服务
# 注意把端口的防火墙权限打开
server {
listen 90;
listen [::]:90;
server_name _;
root /home/website/ImageHost/;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

添加完成后重启以下Nginx nginx -s reload,现在将图片上传到配置中的图片存放路径中,就可以使用域名:端口号/图片名称访问到图片了。

之后会不会有时间做一个WebApp来可视化地管理图片呢?