Hexo搭建轻量化个人博客 篇三:将Hexo部署至VPS

写在前面的话

通过这篇文章,你将学会以下知识点:

  • CentOS安装nginx
  • 将本地建好的Hexo部署至VPS,通过自己注册的域名访问博客

本文目录

  • VPS环境介绍
  • CentOS安装Nginx
  • 配置Nginx
  • VPS安装Nodejs
  • VPS安装Git以及相关配置
  • 将本地Hexo部署至CentOS

1. VPS环境介绍

1
2
$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

2. CentOS安装Nginx

2.1 安装Nginx

输入以下代码进行安装。

1
$ yum install nginx -y
2.2 启动Nginx并设置开机自启。
1
2
$ systemctl start nginx #启动nginx
$ systemctl enable nginx #开机启动

3. 配置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
40
41
42
43
44
45
46
$ vi /etc/nginx/nginx.conf
server {
listen 80;
server_name tubu.ml; #这里写上自己的域名

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /var/www/blog; #这里修改为博客根目录
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/blog; #这里也要修改成博客根目录
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

4. VPS安装Nodejs

1
$ yum install nodejs

用以下命令确认是否安装成功:

1
$ node -v

5. VPS安装Git以及相关配置

5.1 Git安装
1
$ yum install git
5.2 创建git用户以及设置密码
1
2
$ adduser git
$ passwd git
5.3 修改git用户组
1
2
3
4
$ vi /etc/sudoers
root ALL=(ALL) ALL #找到这行
git ALL=(ALL) ALL #添加这行
:wq! 强制保存退出
5.4 添加SSH Key文件

新建~/.ssh/authorized_keys文件,把篇二生成的公钥复制粘贴到authorized_keys里面,:wq保存退出。

1
2
3
$ su git
$ mkdir ~/.ssh
$ vim ~/.ssh/authorized_keys

设置文件权限

1
2
$ chmod 600 ~/.ssh/authorized_keys
$ chmod 700 ~/.ssh

本地运行以下命令测试是否能够成功连接。

1
$ ssh -v git@Serverip #修改Serverip为你的VPS IP
5.5 新建blog文件夹

服务器运行以下命令新建一个blog文件夹,用于Gig上传文件

1
2
$ sudo mkdir -p /var/www/blog
$ sudo chown -R git:git /var/www/blog

5.6 初始化git裸库

切换到git用户,然后切换到git用户目录,接着初始化裸库,执行以下命令:

1
2
3
$ su git
$ cd /home/git
$ git init --bare blog.git

新建post-receive文件

1
2
3
4
$ vi /home/git/blog.git/hooks/post-receive
添加以下内容:wq退出
#!/bin/sh
git --work-tree=/var/www/blog --git-dir=/home/git/blog.git checkout -f

修改执行权限

1
chmod +x ~/blog.git/hooks/post-receive

6. 将本地Hexo部署至CentOS

回到本地,修改_config.yml文件(ssh默认端口不是22的看这里)

1
2
3
4
5
deploy:
type: git
repo: git@Serverip:/home/git/blog.git #修改Serverip为你的VPS IP
branch: master
message:

执行以下命令

1
$ hexo clean && hexo g && hexo d

这时访问 http://serverip 是不是就可以看到 Hexo 网站了。

结语

至此,Hexo搭建轻量化个人博客系列就接近尾声了,感谢各位看官捧场。

0%