0. 介绍

Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。

如果你希望自己的博客自定义程度更高,并且愿意折腾的话,Hexo是不二选择。

1. 导航

先介绍一下需要的一些前置条件

  • 本机能访问github
  • 需要一个服务器(其实也可以不要,hexo的静态网页完全可以交给github或者vercel保管
  • 安装node.js和git
  • 似乎没有什么限制呢

1.0 安装node.js 和git

1
2
3
4
如果已经安装,请跳过本节。
Hexo自带中文文档,有能力可以直接按照官方文档操作。
[hexo官方网站](https://hexo.io/zh-cn/)
[GitHub](https://github.com/hexojs/hexo}

1.1 安装HEXO(已经安装可以跳过)

1.2 换源

1
2
npm install -g cnpm --registry=https://registry.npmmirror.com
npm install -g cnpm --registry=https://registry.npm.taobao.org

1.3 创建自己喜欢的目录保存blog位置

1
2
3
hexo init <folder> // folder为你的博客目录名字,任取
cd <folder> // 进入该目录
npm install // 安装依赖

创建一个博客目录。
至此本地博客的基本搭建就完成了!

2. 本地博客部署到云服务器

hexo作为静态网页不是非得自己有个服务器,github也行,vercel也能代理。如果想直接部署在github请查看

推荐的服务器?

  • 腾讯云
    马上年底了,腾讯云也推出了一些优惠活动,这个基本上新老用户都有比较合适的,而且腾讯云肯定在这方面做的比较好的了,而且你后面的ICP备案,腾讯这边是有幕布的
  • 百度智能云
    百度智能云虽然是后来才有的,但是无论是服务态度上还是稳定性上都挺不错的
  • 阿文云计算-超低价云服务器! (vpsaw.cn)
    这一家也是可以的,最最最重要的是这家服务器不用备案,相关手续和证件比较全面,跑路的可能较低,价格方面也是可以的。我选择了这家,还没有过别的服务器呢,不知道备案会不会很麻烦

其实如果你有公网ip的话似乎能本地部署?移动的不知道能不能拿到,电信的话好像能拿到ipv6的动态地址。不过也挺麻烦就是了

2.0 确保你已经完成了以下内容!

  • 本地搭建完成的博客
  • 一台windows/mac电脑(如果你的电脑是linux应该不用看本篇教程吧
  • 一台云服务器
  • 本地电脑安装一个ssh连接工具(或者bash终端)
  • PS:可能会多次失败也不怕重来的决心

2.1 安装配置git

1
2
3
4
yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel \\安装依赖工具包
yum install -y gcc perl-ExtUtils-MakeMaker package \\安装编译工具
yum remove git -y \\删除旧版本git
cd ~ \\进入家目录

PS: yum命令是linux中centos中才有的,如果系统是ubantu的话换成apt install +package就好了不过我用ubantu的时候好像不用进行这步欸,下个命令再演示吧

2.2 下载git到家目录

1
2
3
4
5
6
7
8
wget https://www.kernel.org/pub/software/scm/git/git-2.34.0.tar.gz --no-check-certificate \\鉴于下载地址的时效性,附上官网地址,后续git更新请去官网下载安装包。
yum install wget -y \\如果报错wget没有的话
tar -zxf git-2.34.0.tar.gz \\解压文件
cd git-2.34.0 \\进入到git目录下
make all prefix=/usr/local/git \\编译
make install prefix=/usr/local/git \\安装giecho '
echo 'export echo PATH=$PATH:/usr/local/git/bin' >> /etc/bashrct \将git加入PATH目录中
source /etc/bashrc \\使配置生效

PS:这个版本已经过期了,如果网址太长会影响shell命令的执行,换行前请在行末加
wget https://www.kernel.org/pub/software/scm/git/git-2.34.0.tar.gz
–no-check-certificate

ubantu的用这个就没问题了

1
sudo apt install git

如果安装不了就先把git先卸载了然后重新安装?狗头军师

1
sudo apt remove git

PS:如果说不是sudoer的话

查看git版本,如果能查看到git的版本号,说明安装成功。

1
git --version

3. 创建用户并配置SSH免密登录

3.1 创建用户并设置密码

1
2
adduser [username]
passwd [username]

PS: adduser 命令和useradd命令的效果可是不一样的哦

3.2 为用户分配权限

1
2
chmod 740 /etc/sudoers   # 设置权限  
vim /etc/sudoers # 编辑/etc/sudoers

image-20221219102042402
然后把自己刚刚创建的用户按照这个样子加在root下边就好了

3.2 安装Nginx

  1. 想要了解更多Nginx详细解答
1
sudo apt-get install nginx \\安装nginx
  1. 相关指令
  • 启动nginx服务
1
systemctl start nginx.service
  • 关闭nginx服务
1
systemctl stop nginx.service
  • 重新启动nginx服务
1
systemctl restart nginx.service

3.3 编辑Nginx文件

1
vim /etc/nginx/nginx.confl

image-20221219103606818
PS:这个sever{}应当在http里面哦!如果你在里面完全找不到这个位置,我建议你把文件清空然后把别人的直接套上去。

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#user  nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
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 html;
}

# 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;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

从别人那里抄来的,前面有一堆行号,我一行行删除的……
不知道ipv6有没有普及呢,如果用的是ipv6的话就把上边的server替换成这个

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server{  
listen 80;
server_name ceshi123.top;
rewrite ^(.*)$ https://$host$1 permanent; # http转https
}
server{
listen 443 ssl;
root /home/hexo;
server_name ceshi123.top; # 请替换为你的域名
client_max_body_size 40m; # 请求体上限
# ssl的一些配置
ssl_certificate "/home/ssl/hexo_ssl/ceshi123.top.crt"; # 请务必替换成你的ssl证书路径
ssl_certificate_key "/home/ssl/hexo_ssl/ceshi123.top.key"; # 请务必替换成你的ssl证书路径
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}

保存退出后,重启服务器,
修改完配置以后,需要重启一下Nginx服务。

1
systemctl reload nginx.service

4.0 建立git仓库

需要注意,//后面的别复制

4.1 创建 blog.git(之后clone是在这里)

1
2
3
4
5
6
su root   //如果现在就是root用户下可以不用这一句  
cd /home/hyh //hyh应该用你自己的代替哈
git init --bare blog.git //创建Git仓库
chown hyh:hyh -R blog.git //授予Git仓库权限
cd /home/hexo/blog.git/hooks/
vim post-receive //这一句话千万别错了哦

把下面的内容拷贝进去并wq!保存。 !强制保存,写错了就q!不保存就好了。

1
2
#!/bin/sh  
git --work-tree=/home/hexo --git-dir=/home/hyh/blog.git checkout -f
1
2
3
\\开放80端口,如果https需要开放443端口
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload

ps: 如果firewall没安装就先安装

1
sudo apt insatll firewall

5.0 把本地内容上传服务器

5.1 登录

1
2
3
su hyh  //登录你用来上传博客的用户  
mkdir ~/.ssh //创建存放密钥的文件夹
vim ~/.ssh/authorized_keys //写入密钥

image-20221219104916864

5.2 测试物理机与服务器能否跑通

1
ssh -v hyh@121.41.11.145 //服务器ip

5.3 修改一点代码

ctrl+f搜索deploy找到以下代码!

1
2
3
4
deploy:  
- type: git
repository: hyh@121.41.11.145:/home/hyh/blog.git
branch: master

image-20221219135835553

5.3 三连操作提交到服务器

1
hexo clean && hexo g && hexo -d
  • hexo clean 能简写成hexo cl 目的是清楚静态网页的缓存
  • hexo generate 简写成hexo g;目的是重新生成缓存
  • hexo deploy 简写成hexo d;目的是部署

注意:如果网页加载不出来但是已经显示deploy down时,请使用无痕浏览的方式打开网页直接搜索主机的IP地址,或者把浏览器 的cookies清除一下也是可以的