上半年来用云服务器搭了一些小应用,不知不觉发现可以比较完整地做一些东西了。顺手用 NginX 和 Hexo 配置了这个博客,版本控制放在 Github 上。
NginX 的配置
Hexo 似乎是需要每次写完重新生成静态文件再部署,感觉稍微有些麻烦。如果用 hexo server 命令则可以动态看到博客内容的更新,所以为了省事把 NginX 连到 Hexo 的端口,直接在后台运行 hexo s 就可以看到博客了。
1 2 3 4 5 6 7 8 9 10
   | server {     listen 80;     server_name blog.amomorning.com;     location / {         proxy_pass http:     }     location /webhook {         proxy_pass http:     } }
  | 
 
pm2 监控博客服务器进程
为了服务器能稳定运行,用 pm2 守护进程监控服务,在服务挂掉的时候能自动重启。直接挂在后台倒也没有占用多少CPU内存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   |  var spawn = require('child_process').spawn;
  free = spawn('hexo', ['server', '-p 4000']);
  free.stdout.on('data', function (data) {     console.log('standard output:\n' + data); });
  free.stderr.on('data', function (data) {     console.log('standard error output:\n' + data); });
  free.on('exit', function (code, signal) {     console.log('child process eixt ,exit:' + code); });
 
 
  | 
 
用pm2运行该代码:
Webhook 侦听 git push
将博客内容放在 GitHub 的代码仓库之后,就可以在不同设备上写博客啦(希望真的会有更新哈哈哈
因为懒于每次写完博客要连接服务器,就在 GitHub 设置里的 Webhook 功能,服务器收到代码仓库更新后能自动 git pull,更新博客内容。
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
   |  var http = require('http') var createHandler = require('github-webhook-handler') var handler = createHandler({path: '/webhook', secret: 'your webhook secret'}) const {exec} = require('child_process') 
  http.createServer(function(req, res) { 	handler(req, res, function(err) { 		res.statusCode = 404 		res.end('no such location') 	}) }).listen('40067')
  handler.on('error', function(err) { 	console.error('Error:', err.message) })
  handler.on('push', function(event) { 	console.log('Received a push event for %s to %s', 		event.payload.repository.name, 		event.payload.ref) 	exec('cd /your/blog/dir && git pull', (err, stdout, stderr) => { 		if (err) { 			console.log(err) 		} 		else { 			console.log('updated success') 		} 	}) })
 
 
  | 
 
同样用pm2运行该代码:
做得很方便用的样子,就是不知道博客里放点什么好呢 >w<