前言

前段时间之前那个服务器到期了,一直没管,博客也是,一直没更,说得倒是挺忙的,但其实也没做什么事。每天这样无意义的忙碌,过了好长一段时间。也想记录一些东西的,坐在电脑前却也不知道该干嘛了~~

后面,团队的事就交给师弟师妹了,我也该滚去做些自己的事。

前置知识

个人博客=个人服务器+git+nginx+hexo

首先要有一台服务器(我的是ubuntu),我的本地PC是ubuntu(用子系统和虚拟机也是一样的,用于本地环境的搭建)

hexo环境搭建在本地。

选用hexo,因为他是静态的,可以避免很多安全问题,其次就是比较成熟,且有很多开源的主题,可以DIY

nginx服务器配置在服务器。

用自己的服务器作为博客环境而不是github,主要是我考虑github有时在国内比较玄学的延迟,其次也是为了把上次搭博客没有记录的记录并复现一下

git本地和服务器都要有,不过服务器上是git服务器

本地配置

hexo的安装

hexo是安装在本地的,他是node.js写的,所以要先有node.js的环境。

1
2
3
4
sudo apt update
sudo apt install nodejs npm
#npm是node.js的包管理工具
#当然也可以选择源码安装,自行百度

环境好了,安装hexo

1
npm install -g hexo-cli

选择一个空文件夹,比如blog

1
2
3
4
5
cd ~/
mkdir blog
cd blog
hexo init
#完成初始化

简单使用

1
2
3
4
5
#在blog的路径内

hexo g -d 生成并推送
hexo s本地打开测试
#更多指令自行搜索

开始hexo的推送配置

在blog目录下_config.yml中,增加以下内容

1
2
3
4
deploy:
type: git
repo: git@服务器公网IP:/home/git/blog.git
branch: master

需要安装git插件,用于git推送

npm install hexo-deployer-git

生成ssh密钥

RSA密钥生成

1
ssh-keygen -C "your@mail.com" #一路确认就行

然后在~/.ssh目录下有id_rsa.pubid_rsa两个文件

一个是公钥一个是私钥

准备将公钥里的内容复制到服务器~/.ssh/authorized_keys文件中,就可以实现免密码ssh登录

服务器配置

nginx的安装与配置

我这边是源码安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#进入预备安装的路径
cd /usr/local/src/
#下载源码
sudo wget http://nginx.org/download/nginx-1.15.2.tar.gz
#解压
sudo tar -xvf nginx-1.15.2.tar.gz
#改个短点的名字
sudo mv nginx-1.15.2 nginx
#进入文件路径
cd nginx

#确认环境,如果不需要ssl也可以直接./configure
sudo ./configure --with-http_stub_status_module --with-http_ssl_module
#编译并安装
sudo make
make install

#进入nginx二进制文件的安装目录,运行
sudo ./nginx

找到配置文件/usr/local/nginx/conf/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
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
116
117
118
user  root;#这里可能需要改一下
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 ××××××××;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /home/git/hexo; #将nginx的根目录改为博客目录,就可以展示了
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
# HTTPS server

server {
listen 443 ssl;
server_name blog.blackt3a.com;
#证书
ssl_certificate /usr/local/nginx/conf/Nginx/××××××;
ssl_certificate_key /usr/local/nginx/conf/Nginx/××××××;

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

}

测试并重启

1
2
3
/usr/local/nginx/sbin/nginx -t #测试配置
#没问题就重启配置
/usr/local/nginx/sbin/nginx -s reload

git安装

本地和服务器都可以通过sudo apt-get install git 安装。

但是,远程服务器环境需要额外配置git服务

创建一个git用户

1
2
3
4
5
6
7
8
9
10
11
groupadd git
useradd git -g git

cd /home/git/
mkdir .ssh
chmod 755 .ssh
touch .ssh/authorized_keys #由于存放登录用户的公钥
chmod 644 .ssh/authorized_keys


mkdir -p /home/git/blog

git用户的权限设置

1
2
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

在blog.git的hooks文件夹下新建文件post-receive,添加内容并执行chmod +x post-receive

当本地git推送到服务器后,会自动执行hooks文件夹里面的脚本

1
2
3
#!/bin/sh
git --work-tree=/home/git/hexo --git-dir=/home/git/blog.git checkout -f

修改/etc/passwd

1
git:x:1004:1004::/home/git:/usr/bin/git-shell

安全防护,限定git只能使用git操作

添加CA证书属于进阶内容

腾讯云证书

https://www.cnblogs.com/XT-xutao/p/14833541.html

https://cloud.tencent.com/document/product/400/35244

参考:

https://www.cnblogs.com/FraserYu/p/15762299.html

https://cloud.tencent.com/developer/article/1763272

2022-10-31

⬆︎TOP