博客搭建

本文最后更新于:4 years ago

1.安装Nodejs
我用的是ubuntu,所以直接apt

1
$ sudo apt install nodejs

1.2安装npm
npm是nodejs的包管理工具

1
$ sudo apt install npm

2.安装hexo
这里建站是使用hexo快速生成的

1
$ sudo npm install --unsafe-perm --verbose -g hexo

2.1然后初始化

1
2
3
4
$ hexo init blog
$ cd blog
$ hexo g
$ hexo s

3.安装pm2
我这边是部署在服务器上,不用git托管,所以要用pm2来使hexo后台运行

1
$ sudo npm install -g pm2

然后在blog目录下新建一个run.js文件
里面添加

1
2
3
4
5
6
7
8
9
10
//run
const { exec } = require('child_process')
exec('hexo server',(error, stdout, stderr) => {
if(error){
console.log('exec error: ${error}')
return
}
console.log('stdout: ${stdout}');
console.log('stderr: ${stderr}');
})

然后用命令运行

1
$ pm2 start run.js

要注意,安全组要开放你用的端口,然后防火墙也要开放,不然是访问不了的。
防火墙开放:

1
2
$ sudo ufw allow 4000
$ sudo ufw reload

然后如果要去掉端口号,即直接通过域名访问,可以使用nginx反向代理。
在nginx配置文件nginx.conf里添加以下的代码

1
2
3
4
server_name: blog.i-ajls.com; #填需要转发的链接
location / {
proxy_pass http://127.0.0.1:4000 #转发到的地址
}