hexo部署私人服务器

一、开始

配置本地 node 环境

  1. node下载 官网下载,可以的话推荐使用 nvm 安装

  2. 安装记得把目录改到其他盘,这里我具体是在 D:\programming\nodejs

打开cmd查看node安装情况

1
2
node -v
npm -v
  1. 配置全局环境

进入安装目录,创建文件夹node_globalnode_cache 并执行

1
2
npm config set prefix "D:\programming\nodejs\node_global"
npm config set cache "D:\programming\nodejs\node_cache"

环境配置:新增环境变量NODE_PATH和添加Path,两个值都为 D:\programming\nodejs\node_global

  1. 安装hexo-cli
1
2
npm i hexo-cli -g
hexo

如果显示下面情况,恭喜你成功全局模块调用

  1. 如果出现命令未找到,或不是可执行程序,别着急!先仔细重复 3-4 步,一般是能够给解决的,如果实在是不能解决,往后看

  2. 随便找个地方初始化文件,执行如下命令:

1
2
mkdir hexo-blog
cd hexo-blog && npm init -y

现在有 3 种解决方法,任选其一

  • 法 1:检查全局目录是否的hexo模块,执行如下命令:
1
D:\programming\nodejs\node_global\hexo
  • 法 2:如果觉得每次加前缀麻烦,使用link命令链接
1
npm link hexo

package.json 中新建脚本如下

1
npm run hexo
  • 法 3:你还可以直接在hexo-blog中下载
1
2
3
npm i hexo-cli
# 还是要在package.json中新建脚本
npm run hexo

初始化hexo项目

  1. 如果是按照上一节步骤 4 过来的,则在hexo-blog文件下初始化
1
2
hexo init myblog && cd myblog
npm i
  1. 下载主题
1
git clone https://github.com/iissnan/hexo-theme-next themes/next

本地配置文件中设置theme属性

  1. 本地执行hexo项目,添加start脚本
1
npm start

打开 http://localhost:4000 验证效果吧

git环境搭建

  1. git安装:官网下载

  2. 生成ssh认证,执行如下命令

1
2
3
4
5
git config --global user.name "yourname"
git config --global user.email "youremail@example.com"
ssh-keygen -t rsa -C "youremail@example.com"
# 禁用自动转换,这个不设置后面上传时会出现警告
git config --global core.autocrlf false

最后获取到的ssh认证在C:\Users\yourname\.ssh


三、服务器配置

搭建远程Git私库

  1. 登录到远程服务器,推荐使用Xshell 5

  2. 安装 git

CentOS

1
2
3
4
# 如无,则安装
git --version
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
yum install -y git

Ubuntu

1
2
3
4
# 如无,则安装
git --version
sudo apt-get update
apt install git
  1. 创建用户并配置其仓库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
useradd git
# 设置密码
passwd git
# 这步很重要,不切换用户后面会很麻烦
su git
cd /home/git/
# 项目存在的真实目录
mkdir -p projects/blog
mkdir repos && cd repos
# 创建一个裸露的仓库
git init --bare blog.git
cd blog.git/hooks
# 创建 hook 钩子函数,输入了内容如下
vi post-receive
1
2
#!/bin/sh
git --work-tree=/home/git/projects/blog --git-dir=/home/git/repos/blog.git checkout -f

添加完毕后修改权限

1
2
3
4
5
chmod +x post-receive
# 退出到 root 登录
exit
# 添加权限
chown -R git:git /home/git/repos/blog.git
  1. 测试git仓库是否可用,另找空白文件夹
1
git clone git@{server_ip}:/home/git/repos/blog.git

如果能把空仓库拉下来,就说明 git 仓库搭建成功了

  1. 建立ssh信任关系,在本地电脑
1
2
3
ssh-copy-id -i C:/Users/{yourname}/.ssh/id_rsa.pub git@{server_ip}
# 测试能否登录
ssh git@{server_ip}

:此时的 ssh 登录 git 用户不需要密码!否则就有错,请仔细重复步骤 3-5

  1. 为了安全起见禁用 git 用户的 shell 登录权限,从而只能用 git clone,git push 等登录
1
2
3
4
5
6
# 查看 git-shell 是否在登录方式里面
cat /etc/shells
# 查看是否安装
which git-shell
# 添加上2步显示出来的路劲,通常在 /usr/bin/git-shell
vi /etc/shells

修改/etc/passwd中的权限

1
2
3
4
5
# 将原来的
git:x:1001:1001::/home/git:/bin/bash

# 修改为
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

搭建nginx服务器

  1. 下载并安装nginx
1
2
3
4
cd /usr/local/src
wget http://nginx.org/download/nginx-1.15.2.tar.gz
tar xzvf nginx-1.15.2.tar.gz
cd nginx-1.15.2

Ubuntu系统需要的依赖库

1
2
3
4
5
6
7
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libtool
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl
sudo apt-get install libssl-dev
1
2
3
4
5
6
# 如果后面还想要配置 SSL 协议,就执行后面一句!
./configure
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
make && make install
# 为 nginx 取别名,后面可直接用。用户登出失效,可以修改新增到 `~/.bashrc` 文件
alias nginx='/usr/local/nginx/sbin/nginx'
1
alias nginx='/usr/local/nginx/sbin/nginx'

执行文件

1
source ~/.bashrc
  1. 配置nginx文件

先启动是否安装成功

1
2
# 直接来!浏览器查看 server_ip,默认是 80 端口
nginx

修改配置文件

1
2
3
4
5
6
nginx -s stop // 先停止nginx
cd /usr/local/nginx/conf
vi nginx.conf
# 修改 root 解析路径,如下图
# 同时将 user 改为 root 如下图,不然nginx无法访问 /home/git/projects/blog
nginx -s reload

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
116
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 localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /home/git/projects/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 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;
# }
#}

}

https配置

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
119
120
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 aliatry.com;

rewrite ^/(.*) https://$host$1 permanent;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
#root /home/git/projects/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 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 aliatry.com;

ssl_certificate cert.pem;
ssl_certificate_key cert.key;

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

ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

location / {
root /home/git/projects/blog;
index index.html index.htm;
}
}

}

四、发布

至此我们就把本地和服务器的环境全部搭建完成,现在利用 hexo 配置文件进行链接

配置_config.yml文件

  1. 编辑 _config.yml 的 deploy 属性
1
2
3
4
5
deploy:
type: git
branch: master
repository:
server: git@server_ip:/home/git/repos/blog.git
  1. 在 package.json 中添加 npm 脚本
1
2
3
4
"scripts": {
"deploy": "hexo clean && hexo g -d",
"start": "hexo clean && hexo g && hexo s"
},
  1. 链接!这下在本地调试就用npm start,调试好了就上传到服务器,美滋滋~快通过你的服务器 ip 访问吧
1
npm run deploy