-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.json
More file actions
1 lines (1 loc) · 246 KB
/
Copy pathdb.json
File metadata and controls
1 lines (1 loc) · 246 KB
1
{"meta":{"version":1,"warehouse":"2.2.0"},"models":{"Asset":[{"_id":"themes/apollo/source/sw.js","path":"sw.js","modified":0,"renderable":1},{"_id":"themes/apollo/source/css/apollo.css","path":"css/apollo.css","modified":0,"renderable":1},{"_id":"themes/apollo/source/img/docker.png","path":"img/docker.png","modified":0,"renderable":1},{"_id":"themes/apollo/source/img/favicon.png","path":"img/favicon.png","modified":0,"renderable":1},{"_id":"themes/apollo/source/img/ppxu.jpg","path":"img/ppxu.jpg","modified":0,"renderable":1},{"_id":"themes/apollo/source/img/hook.png","path":"img/hook.png","modified":0,"renderable":1},{"_id":"themes/apollo/source/img/http2.png","path":"img/http2.png","modified":0,"renderable":1},{"_id":"themes/apollo/source/img/spdy.png","path":"img/spdy.png","modified":0,"renderable":1},{"_id":"themes/apollo/source/js/snow.js","path":"js/snow.js","modified":0,"renderable":1},{"_id":"themes/apollo/source/img/sync.png","path":"img/sync.png","modified":0,"renderable":1},{"_id":"themes/apollo/source/scss/apollo.scss","path":"scss/apollo.scss","modified":0,"renderable":1},{"_id":"themes/apollo/source/img/deploy.png","path":"img/deploy.png","modified":0,"renderable":1},{"_id":"themes/apollo/source/img/ssl.png","path":"img/ssl.png","modified":0,"renderable":1}],"Cache":[{"_id":"source/.DS_Store","hash":"b48c4f7d61a5928be717d4bd654481ff1eab36ee","modified":1482720962000},{"_id":"themes/apollo/.DS_Store","hash":"9457f542cf2c3f2a50b9ecd64858fc6f50b0d0c4","modified":1482720962000},{"_id":"themes/apollo/.babelrc","hash":"dbfe351b10bbbfdee559f6f521f07326cdf89f55","modified":1450495410000},{"_id":"themes/apollo/.editorconfig","hash":"14355e40eef847e03b4e2404f5d458baad4e3c4a","modified":1450495410000},{"_id":"themes/apollo/LICENSE","hash":"6e31ac9076bfc8f09ae47977419eee4edfb63e5b","modified":1450495410000},{"_id":"themes/apollo/README.md","hash":"fdaa691c63bba80c75b17e9426c06f148dfb9dc4","modified":1450495410000},{"_id":"themes/apollo/_config.yml","hash":"812a602fdf23a7dc320f436db00d577c93d0eb35","modified":1487660780000},{"_id":"themes/apollo/gulpfile.babel.js","hash":"ae6ba496e2672ed07050295954ca3da8b0ef423c","modified":1450495410000},{"_id":"themes/apollo/package.json","hash":"a872d0158d522612ccc0b300bdf27d0228de8428","modified":1450495410000},{"_id":"source/_posts/auto-deploy-hexo-with-github-webhooks.md","hash":"28dd064329aa9a5790ec3d6f374a164e04ecaca2","modified":1487660923000},{"_id":"source/_posts/config-spdy-in-nginx.md","hash":"e03fa18814364b6a98eb6c054b0119fe324d4dca","modified":1487660941000},{"_id":"source/_posts/config-ssl-in-nginx.md","hash":"46866bf055e1618d8e2460fbac74be727b220865","modified":1487660963000},{"_id":"source/_posts/enable-emoji-in-hexo.md","hash":"b0fe37c7746eb7af790ea04fa3709a2f29b8a25f","modified":1451138839000},{"_id":"source/_posts/hello-world.md","hash":"774b507901d9b17991ace2a70263b6dd6d11999a","modified":1450449077000},{"_id":"source/_posts/install-nodejs-and-hexo-in-aliyun-centos.md","hash":"39c312d849fbdcf325692787b508fd88c8918305","modified":1451138093000},{"_id":"source/_posts/install-shadowsocks-service.md","hash":"6b4d019bceee9dc8e33560a2f03b2f489562ce97","modified":1459258847000},{"_id":"source/_posts/introduce-to-electron.md","hash":"45253da3a7c2202e41c4446a50b0f7fb74150eca","modified":1454339934000},{"_id":"source/_posts/nginx-smooth-upgrade-and-config-http2.md","hash":"d3a75a7d4a0047937cafff3c29baca5f1171ff72","modified":1487660996000},{"_id":"source/_posts/setup-nginx-nodejs-with-docker-in-mac.md","hash":"ff70d0a1ee77d04d21c3d07d4daca1c362863d88","modified":1487661004000},{"_id":"themes/apollo/doc/custom-blocks.md","hash":"78e9400714d0ff7c9b272d3ccc80fb18c3bf208f","modified":1450495410000},{"_id":"themes/apollo/layout/index.jade","hash":"58c451042cad5beeb5a76852bba609c651ff3428","modified":1450495410000},{"_id":"themes/apollo/layout/post.jade","hash":"6de2c8cb24d70035008155ff5ba37d6bdeb94e8d","modified":1450772053000},{"_id":"themes/apollo/source/.DS_Store","hash":"e840c81470558c43d93f68a01fca72f98c807c0c","modified":1487661335000},{"_id":"themes/apollo/source/sw.js","hash":"54b241d2d0c59bf72d04aa3549a48ef178228267","modified":1487830299000},{"_id":"themes/apollo/layout/mixins/paginator.jade","hash":"510ee0ba37b4522cca6c6204d641809454ba3ac6","modified":1450495410000},{"_id":"themes/apollo/layout/mixins/post.jade","hash":"db05e11065f6f37324218accadd4c7630c80c663","modified":1450772110000},{"_id":"themes/apollo/layout/partial/comment.jade","hash":"ff0a2c269c2434da2ac5529872f1d6184a71f96d","modified":1450495410000},{"_id":"themes/apollo/layout/partial/foot.jade","hash":"22a02bf0359dee6126a37b06bf79a80120aae217","modified":1451137501000},{"_id":"themes/apollo/layout/partial/head.jade","hash":"2e490108c1f000b39606cf610fcf77c30b19d590","modified":1450772460000},{"_id":"themes/apollo/layout/partial/layout.jade","hash":"7aae8a09d88ce269f1cfb3394d3afcf4b1962b51","modified":1450717689000},{"_id":"themes/apollo/layout/partial/nav.jade","hash":"c35d3061da4b053b73150d9741c542d660798270","modified":1450495410000},{"_id":"themes/apollo/layout/partial/scripts.jade","hash":"44b5f488444bf2fa8bd64b7b73f55a3d3690d621","modified":1486439484000},{"_id":"themes/apollo/source/css/apollo.css","hash":"1deaf6c233e1e3edc623de49d7b8463ced148538","modified":1450945899000},{"_id":"themes/apollo/source/img/.DS_Store","hash":"460b63828842f78e002070f77464ef44d7d9bd11","modified":1487660718000},{"_id":"themes/apollo/source/img/docker.png","hash":"83f5f5d57ace19c6263025eaa4a6b5cea8f71c2d","modified":1487632810000},{"_id":"themes/apollo/source/img/favicon.png","hash":"f066c242e1e35e7c3b4b5bf362a1c47ac647552f","modified":1451026189000},{"_id":"themes/apollo/source/img/ppxu.jpg","hash":"216af1c8674d02ec4821fe789d6153ae5fc214b6","modified":1376048026000},{"_id":"themes/apollo/source/img/hook.png","hash":"d79090fb8d916b5925107e9c05205d4b1eb9f1b1","modified":1487632810000},{"_id":"themes/apollo/source/img/http2.png","hash":"6ea7e0152709bebfcebe153568f54892cf657852","modified":1487632810000},{"_id":"themes/apollo/source/img/spdy.png","hash":"a263ed4ff5778032a6da0efcb2acf3dc075b656e","modified":1487632810000},{"_id":"themes/apollo/source/js/snow.js","hash":"4fc0908422111ce18d6503c6e6aae23012fa7c3f","modified":1483979688000},{"_id":"themes/apollo/source/img/sync.png","hash":"dd95cada69424d7f75740df828860326e61bd10b","modified":1487632810000},{"_id":"themes/apollo/source/scss/apollo.scss","hash":"f8b40223ea647b7c5627c344ead5c1a493bdd576","modified":1450495410000},{"_id":"themes/apollo/source/img/deploy.png","hash":"47e18c8f5c5ebcfec08a4768fc19902fa5123b7b","modified":1487632810000},{"_id":"themes/apollo/source/img/ssl.png","hash":"187b9b52a726608ed6ead03c9229bd74df38a84b","modified":1487632810000},{"_id":"themes/apollo/source/scss/_partial/base.scss","hash":"88b361e68475caddbab763feed5e1db788ac2cd7","modified":1450495410000},{"_id":"themes/apollo/source/scss/_partial/footer.scss","hash":"cf0ad16d999537cefe8441893b4edcb54eafe37c","modified":1450772597000},{"_id":"themes/apollo/source/scss/_partial/header.scss","hash":"d24cc6520f3faa7bb80610b858a92639eadcc289","modified":1450495410000},{"_id":"themes/apollo/source/scss/_partial/mq.scss","hash":"0b9c7097136ac8e4a07d9702fc4dbe0345ac7596","modified":1450495410000},{"_id":"themes/apollo/source/scss/_partial/normalize.scss","hash":"fd0b27bed6f103ea95b08f698ea663ff576dbcf1","modified":1450495410000},{"_id":"themes/apollo/source/scss/_partial/post.scss","hash":"90945db06b2972334ade6e8052eb8c9110814e85","modified":1450945932000},{"_id":"themes/apollo/source/scss/_partial/posts.scss","hash":"92858015b8f3dcb4eb91b6dc41563b7aaa91b376","modified":1450495410000},{"_id":"public/atom.xml","hash":"e9aae05a01be6ac677f8ad13b23d1a942f8d73ab","modified":1487662417778},{"_id":"public/sitemap.xml","hash":"c28a5460ea4f1fe17aa5ef838a8b1f498c09944f","modified":1487662417789},{"_id":"public/2016/09/06/setup-nginx-nodejs-with-docker-in-mac/index.html","hash":"b1491e9dd4817dd1edf3de9054735e3ca56641d6","modified":1487662417800},{"_id":"public/2016/03/29/install-shadowsocks-service/index.html","hash":"bd8975c2413f757f0f7ce155c59cc22e15d912dd","modified":1487662417800},{"_id":"public/2015/12/24/enable-emoji-in-hexo/index.html","hash":"562fc18cbb78187c01d8d81c41fd68fe32af76da","modified":1487662417800},{"_id":"public/2015/12/23/nginx-smooth-upgrade-and-config-http2/index.html","hash":"c21f06daabb63c640d60538efcf197933dc32107","modified":1487662417800},{"_id":"public/2015/12/22/config-spdy-in-nginx/index.html","hash":"10541a8703b54120eec8e84237ccdc5ee616dba4","modified":1487662417800},{"_id":"public/2015/12/21/auto-deploy-hexo-with-github-webhooks/index.html","hash":"d01fc484a37fc42314923c59fa654ab11857d7d9","modified":1487662417800},{"_id":"public/2015/12/20/config-ssl-in-nginx/index.html","hash":"718a8b55ec82cc390918bce9aae8f9ac073173cf","modified":1487662417800},{"_id":"public/2015/12/19/install-nodejs-and-hexo-in-aliyun-centos/index.html","hash":"78ea3fe8bcdfff4f3f332523c6768799436c9967","modified":1487662417800},{"_id":"public/2015/12/18/hello-world/index.html","hash":"90cc2c0b7d34c30aad3398207a8968e9ac01c0c3","modified":1487662417800},{"_id":"public/archives/index.html","hash":"bb5f98b6af653907538049655e2e9bb324878fee","modified":1487662417800},{"_id":"public/archives/2015/12/index.html","hash":"5b2179b7c687a55997ab0d068406e0a47d7fe0f6","modified":1487662417800},{"_id":"public/archives/2015/index.html","hash":"5b2179b7c687a55997ab0d068406e0a47d7fe0f6","modified":1487662417800},{"_id":"public/archives/2016/index.html","hash":"093e7e37d5c7d7ddc2af774a8d266380f3e93f3b","modified":1487662417801},{"_id":"public/archives/2016/01/index.html","hash":"cd558c4e43b6e79561e5789feecb56c45ddd38bc","modified":1487662417801},{"_id":"public/archives/2016/03/index.html","hash":"b5a4b7e1313f3e57fa5a4a73d958bd7a0cd78ec1","modified":1487662417801},{"_id":"public/archives/2016/09/index.html","hash":"88d3fbeb15f31c0c524c40a3f1f661a33a2ae479","modified":1487662417801},{"_id":"public/categories/blog/index.html","hash":"985f7b170d22cbfcd65d751f05dac2407d6c5cfd","modified":1487662417801},{"_id":"public/categories/nodejs/index.html","hash":"cd558c4e43b6e79561e5789feecb56c45ddd38bc","modified":1487662417801},{"_id":"public/index.html","hash":"1c54ce92aead79fbeb01570d48ba5e9c09d96e8b","modified":1487662417801},{"_id":"public/tags/webhooks/index.html","hash":"13ba9d3adb7bee7e462979faf28c20b5734ed97c","modified":1487662417801},{"_id":"public/tags/github/index.html","hash":"13ba9d3adb7bee7e462979faf28c20b5734ed97c","modified":1487662417801},{"_id":"public/tags/hexo/index.html","hash":"e4164e44ef2cf9e77ee240f9c62c2efffb36402c","modified":1487662417801},{"_id":"public/tags/deploy/index.html","hash":"13ba9d3adb7bee7e462979faf28c20b5734ed97c","modified":1487662417801},{"_id":"public/tags/shell/index.html","hash":"13ba9d3adb7bee7e462979faf28c20b5734ed97c","modified":1487662417801},{"_id":"public/tags/aliyun/index.html","hash":"6b808849ff805d792655395976852f7d95624b0b","modified":1487662417801},{"_id":"public/tags/ecs/index.html","hash":"6b808849ff805d792655395976852f7d95624b0b","modified":1487662417801},{"_id":"public/tags/centos/index.html","hash":"18f8ba2a4dba9a2785e52f9ef20d7cde55dfc242","modified":1487662417801},{"_id":"public/tags/nginx/index.html","hash":"8e5c784456e8655130e1c65d2972baca206519e8","modified":1487662417801},{"_id":"public/tags/spdy/index.html","hash":"a332da169475da4c0309e3148ad001866317e4fa","modified":1487662417801},{"_id":"public/tags/ssl/index.html","hash":"9ac38d5cfef16b96b961cb859013028e2014ea91","modified":1487662417801},{"_id":"public/tags/https/index.html","hash":"9ac38d5cfef16b96b961cb859013028e2014ea91","modified":1487662417801},{"_id":"public/tags/markdown/index.html","hash":"bf9cda3d49088365dbe04fa58bb3f14b51c9cb12","modified":1487662417801},{"_id":"public/tags/emoji/index.html","hash":"bf9cda3d49088365dbe04fa58bb3f14b51c9cb12","modified":1487662417801},{"_id":"public/tags/nodejs/index.html","hash":"ddfb92dfe35c6f0bf3c0a558495493acba51f00a","modified":1487662417801},{"_id":"public/tags/blog/index.html","hash":"ee3dedeb8dedad5335f23b1e454bc970439bb024","modified":1487662417802},{"_id":"public/tags/shadowsocks/index.html","hash":"b5a4b7e1313f3e57fa5a4a73d958bd7a0cd78ec1","modified":1487662417802},{"_id":"public/tags/electron/index.html","hash":"cd558c4e43b6e79561e5789feecb56c45ddd38bc","modified":1487662417802},{"_id":"public/tags/atom/index.html","hash":"cd558c4e43b6e79561e5789feecb56c45ddd38bc","modified":1487662417802},{"_id":"public/tags/chromium/index.html","hash":"cd558c4e43b6e79561e5789feecb56c45ddd38bc","modified":1487662417802},{"_id":"public/tags/app/index.html","hash":"cd558c4e43b6e79561e5789feecb56c45ddd38bc","modified":1487662417802},{"_id":"public/tags/http2/index.html","hash":"1a536c6df0d98d5753544e9f757f8f25c1b1d8eb","modified":1487662417802},{"_id":"public/tags/mac/index.html","hash":"88d3fbeb15f31c0c524c40a3f1f661a33a2ae479","modified":1487662417802},{"_id":"public/tags/docker/index.html","hash":"88d3fbeb15f31c0c524c40a3f1f661a33a2ae479","modified":1487662417802},{"_id":"public/2016/01/31/introduce-to-electron/index.html","hash":"a279b285c8eefa20cb56f01ca119b938967a8ca6","modified":1487662417802},{"_id":"public/img/docker.png","hash":"83f5f5d57ace19c6263025eaa4a6b5cea8f71c2d","modified":1487662417806},{"_id":"public/img/favicon.png","hash":"f066c242e1e35e7c3b4b5bf362a1c47ac647552f","modified":1487662417806},{"_id":"public/img/ppxu.jpg","hash":"216af1c8674d02ec4821fe789d6153ae5fc214b6","modified":1487662417806},{"_id":"public/img/hook.png","hash":"d79090fb8d916b5925107e9c05205d4b1eb9f1b1","modified":1487662417806},{"_id":"public/img/http2.png","hash":"6ea7e0152709bebfcebe153568f54892cf657852","modified":1487662417806},{"_id":"public/img/spdy.png","hash":"a263ed4ff5778032a6da0efcb2acf3dc075b656e","modified":1487662417806},{"_id":"public/scss/apollo.scss","hash":"f8b40223ea647b7c5627c344ead5c1a493bdd576","modified":1487662417806},{"_id":"public/img/sync.png","hash":"dd95cada69424d7f75740df828860326e61bd10b","modified":1487662417806},{"_id":"public/img/deploy.png","hash":"47e18c8f5c5ebcfec08a4768fc19902fa5123b7b","modified":1487662417810},{"_id":"public/img/ssl.png","hash":"187b9b52a726608ed6ead03c9229bd74df38a84b","modified":1487662417810},{"_id":"public/sw.js","hash":"54b241d2d0c59bf72d04aa3549a48ef178228267","modified":1487831040950},{"_id":"public/css/apollo.css","hash":"1deaf6c233e1e3edc623de49d7b8463ced148538","modified":1487662417813},{"_id":"public/js/snow.js","hash":"4fc0908422111ce18d6503c6e6aae23012fa7c3f","modified":1487662417813},{"_id":"themes/apollo/layout/offline.jade","hash":"7b87aa292fadc6a6cd2e3ed4fb69b2cde75f7b3b","modified":1487662616000}],"Category":[{"name":"blog","_id":"cizf7sinx0002a3xxa3qhpj7h"},{"name":"nodejs","_id":"cizf7siop000ja3xxic35r5ap"}],"Data":[],"Page":[],"Post":[{"title":"使用Github的Webhooks实现Hexo的自动部署","date":"2015-12-21T09:29:24.000Z","_content":"博客之前的更新方式是先在本地写好文章,push到Github,然后ssh连到服务器上,再pull下来,如果每次都要这样操作一遍实在麻烦,今天就试着用Github的Webhooks功能实现了Hexo博客的自动部署,过程记录如下。\n\n<!--more-->\n\n整个过程主要有两个环节:\n\n### 本地代码自动部署到Github\n\nHexo本身就有deploy功能,只要在`_config.yml`里面做一下[配置](https://hexo.io/zh-cn/docs/deployment.html),就可以部署到Github、Heroku等平台上,如果博客是托管在Github Pages上的话使用这种方式可以很方便的实现自动部署,不过通过这种方式发送到Github上的只有`public`静态文件目录,我这里希望托管整个应用的代码,就不能使用这种方式了,反正只要可以push就行了,我们搬出shell大法好。\n\n创建文件`deploy.sh`\n\n``` bash\n#!/bin/bash\n\necho -e \"\\033[32m [AUTO DEPLOY] deploy hexo start \\033[0m\"\necho -e \"\\033[32m [AUTO DEPLOY] hexo generate... \\033[0m\"\nhexo g\necho -e \"\\033[32m [AUTO DEPLOY] git commit... \\033[0m\"\nd=`date +%x-%T`\ngit add .\ngit commit -m \"auto deploy at \"${d}\necho -e \"\\033[32m [AUTO DEPLOY] git push... \\033[0m\"\ngit push origin master\necho -e \"\\033[32m [AUTO DEPLOY] deploy hexo finish \\033[0m\"\n```\n\n然后增加权限\n\n``` bash\n$ chmod +x ./deploy.sh\n```\n\n这样完成本地开发后,只要执行命令\n\n``` bash\n$ ./deploy.sh\n```\n\n就可以让Hexo生成静态文件并push到Github上。\n\n### Github自动同步到服务器\n\n为了让服务器可以自动同步Github上面的更新,我们需要用到Github的Webhooks。\n\n首先创建文件`sync.sh`\n\n``` bash\n#!/bin/bash\n\necho -e \"\\033[32m [AUTO SYNC] sync hexo start \\033[0m\"\ncd /ppxu/blog\necho -e \"\\033[32m [AUTO SYNC] git pull... \\033[0m\"\ngit pull origin master\necho -e \"\\033[32m [AUTO SYNC] sync hexo finish \\033[0m\"\n```\n\n目标是每当Github有push的时候就自动调用这个脚本。\n\n然后找到Github仓库的Settings页\n\n\n\n添加一条Webhook,填写请求地址`http://xx.xx.xx.xx:7777/webhook`,这样每当Github收到push或者其他事件时就会自动向这个地址发送一条POST请求。\n\n下面在服务器上补充这个请求地址,我们用Node搭一个简单的http服务,这里用到了[github-webhook-handler](https://github.com/rvagg/github-webhook-handler)处理hook消息,创建文件`server.js`\n\n``` javascript\nvar http = require('http')\nvar exec = require('child_process').exec;\nvar createHandler = require('github-webhook-handler')\nvar handler = createHandler({ path: '/webhook', secret: '********' });\n\nhttp.createServer(function (req, res) {\n handler(req, res, function (err) {\n res.statusCode = 404;\n res.end('no such location');\n });\n}).listen(7777);\n\nhandler.on('error', function (err) {\n console.error('Error:', err.message);\n});\n\nhandler.on('push', function (event) {\n console.log('Received a push event for %s to %s',\n event.payload.repository.name,\n event.payload.ref);\n exec('/ppxu/blog/sync.sh', function(err, stdout, stderr){\n if(err) {\n console.log('sync server err: ' + stderr);\n } else {\n console.log(stdout);\n }\n });\n});\n```\n\n这里的`secret`要和在Github上新建hook时设置的一样,请求时校验用的。\n\n然后启动服务\n\n``` bash\n$ node server.js &\n```\n\n这里也可以用forever之类的工具防止进程挂掉。\n\n这样一套自动部署系统就建立好了,在本机和服务器的实际效果如下:\n\n\n\n\n\n感觉生活一下子美好起来了呢:)\n\n#### 参考资料\n\n* [https://hexo.io/zh-cn/docs/deployment.html](https://hexo.io/zh-cn/docs/deployment.html)\n\n* [https://developer.github.com/webhooks/](https://developer.github.com/webhooks/)\n\n* [http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html](http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html)\n","source":"_posts/auto-deploy-hexo-with-github-webhooks.md","raw":"title: 使用Github的Webhooks实现Hexo的自动部署\ndate: 2015-12-21 17:29:24\ncategories: blog\ntags: [github, webhooks, hexo, deploy, shell]\n---\n博客之前的更新方式是先在本地写好文章,push到Github,然后ssh连到服务器上,再pull下来,如果每次都要这样操作一遍实在麻烦,今天就试着用Github的Webhooks功能实现了Hexo博客的自动部署,过程记录如下。\n\n<!--more-->\n\n整个过程主要有两个环节:\n\n### 本地代码自动部署到Github\n\nHexo本身就有deploy功能,只要在`_config.yml`里面做一下[配置](https://hexo.io/zh-cn/docs/deployment.html),就可以部署到Github、Heroku等平台上,如果博客是托管在Github Pages上的话使用这种方式可以很方便的实现自动部署,不过通过这种方式发送到Github上的只有`public`静态文件目录,我这里希望托管整个应用的代码,就不能使用这种方式了,反正只要可以push就行了,我们搬出shell大法好。\n\n创建文件`deploy.sh`\n\n``` bash\n#!/bin/bash\n\necho -e \"\\033[32m [AUTO DEPLOY] deploy hexo start \\033[0m\"\necho -e \"\\033[32m [AUTO DEPLOY] hexo generate... \\033[0m\"\nhexo g\necho -e \"\\033[32m [AUTO DEPLOY] git commit... \\033[0m\"\nd=`date +%x-%T`\ngit add .\ngit commit -m \"auto deploy at \"${d}\necho -e \"\\033[32m [AUTO DEPLOY] git push... \\033[0m\"\ngit push origin master\necho -e \"\\033[32m [AUTO DEPLOY] deploy hexo finish \\033[0m\"\n```\n\n然后增加权限\n\n``` bash\n$ chmod +x ./deploy.sh\n```\n\n这样完成本地开发后,只要执行命令\n\n``` bash\n$ ./deploy.sh\n```\n\n就可以让Hexo生成静态文件并push到Github上。\n\n### Github自动同步到服务器\n\n为了让服务器可以自动同步Github上面的更新,我们需要用到Github的Webhooks。\n\n首先创建文件`sync.sh`\n\n``` bash\n#!/bin/bash\n\necho -e \"\\033[32m [AUTO SYNC] sync hexo start \\033[0m\"\ncd /ppxu/blog\necho -e \"\\033[32m [AUTO SYNC] git pull... \\033[0m\"\ngit pull origin master\necho -e \"\\033[32m [AUTO SYNC] sync hexo finish \\033[0m\"\n```\n\n目标是每当Github有push的时候就自动调用这个脚本。\n\n然后找到Github仓库的Settings页\n\n\n\n添加一条Webhook,填写请求地址`http://xx.xx.xx.xx:7777/webhook`,这样每当Github收到push或者其他事件时就会自动向这个地址发送一条POST请求。\n\n下面在服务器上补充这个请求地址,我们用Node搭一个简单的http服务,这里用到了[github-webhook-handler](https://github.com/rvagg/github-webhook-handler)处理hook消息,创建文件`server.js`\n\n``` javascript\nvar http = require('http')\nvar exec = require('child_process').exec;\nvar createHandler = require('github-webhook-handler')\nvar handler = createHandler({ path: '/webhook', secret: '********' });\n\nhttp.createServer(function (req, res) {\n handler(req, res, function (err) {\n res.statusCode = 404;\n res.end('no such location');\n });\n}).listen(7777);\n\nhandler.on('error', function (err) {\n console.error('Error:', err.message);\n});\n\nhandler.on('push', function (event) {\n console.log('Received a push event for %s to %s',\n event.payload.repository.name,\n event.payload.ref);\n exec('/ppxu/blog/sync.sh', function(err, stdout, stderr){\n if(err) {\n console.log('sync server err: ' + stderr);\n } else {\n console.log(stdout);\n }\n });\n});\n```\n\n这里的`secret`要和在Github上新建hook时设置的一样,请求时校验用的。\n\n然后启动服务\n\n``` bash\n$ node server.js &\n```\n\n这里也可以用forever之类的工具防止进程挂掉。\n\n这样一套自动部署系统就建立好了,在本机和服务器的实际效果如下:\n\n\n\n\n\n感觉生活一下子美好起来了呢:)\n\n#### 参考资料\n\n* [https://hexo.io/zh-cn/docs/deployment.html](https://hexo.io/zh-cn/docs/deployment.html)\n\n* [https://developer.github.com/webhooks/](https://developer.github.com/webhooks/)\n\n* [http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html](http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html)\n","slug":"auto-deploy-hexo-with-github-webhooks","published":1,"updated":"2017-02-21T07:08:43.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7sino0000a3xxxmxo77d3","content":"<p>博客之前的更新方式是先在本地写好文章,push到Github,然后ssh连到服务器上,再pull下来,如果每次都要这样操作一遍实在麻烦,今天就试着用Github的Webhooks功能实现了Hexo博客的自动部署,过程记录如下。</p>\n<a id=\"more\"></a>\n<p>整个过程主要有两个环节:</p>\n<h3 id=\"本地代码自动部署到github\"><a class=\"header-anchor\" href=\"#本地代码自动部署到github\">¶</a>本地代码自动部署到Github</h3>\n<p>Hexo本身就有deploy功能,只要在<code>_config.yml</code>里面做一下<a href=\"https://hexo.io/zh-cn/docs/deployment.html\" target=\"_blank\" rel=\"external\">配置</a>,就可以部署到Github、Heroku等平台上,如果博客是托管在Github Pages上的话使用这种方式可以很方便的实现自动部署,不过通过这种方式发送到Github上的只有<code>public</code>静态文件目录,我这里希望托管整个应用的代码,就不能使用这种方式了,反正只要可以push就行了,我们搬出shell大法好。</p>\n<p>创建文件<code>deploy.sh</code></p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"meta\">#!/bin/bash</span></div><div class=\"line\"></div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] deploy hexo start \\033[0m\"</span></div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] hexo generate... \\033[0m\"</span></div><div class=\"line\">hexo g</div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] git commit... \\033[0m\"</span></div><div class=\"line\">d=`date +%x-%T`</div><div class=\"line\">git add .</div><div class=\"line\">git commit -m <span class=\"string\">\"auto deploy at \"</span><span class=\"variable\">${d}</span></div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] git push... \\033[0m\"</span></div><div class=\"line\">git push origin master</div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] deploy hexo finish \\033[0m\"</span></div></pre></td></tr></table></figure>\n<p>然后增加权限</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ chmod +x ./deploy.sh</div></pre></td></tr></table></figure>\n<p>这样完成本地开发后,只要执行命令</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ./deploy.sh</div></pre></td></tr></table></figure>\n<p>就可以让Hexo生成静态文件并push到Github上。</p>\n<h3 id=\"github自动同步到服务器\"><a class=\"header-anchor\" href=\"#github自动同步到服务器\">¶</a>Github自动同步到服务器</h3>\n<p>为了让服务器可以自动同步Github上面的更新,我们需要用到Github的Webhooks。</p>\n<p>首先创建文件<code>sync.sh</code></p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"meta\">#!/bin/bash</span></div><div class=\"line\"></div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO SYNC] sync hexo start \\033[0m\"</span></div><div class=\"line\"><span class=\"built_in\">cd</span> /ppxu/blog</div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO SYNC] git pull... \\033[0m\"</span></div><div class=\"line\">git pull origin master</div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO SYNC] sync hexo finish \\033[0m\"</span></div></pre></td></tr></table></figure>\n<p>目标是每当Github有push的时候就自动调用这个脚本。</p>\n<p>然后找到Github仓库的Settings页</p>\n<p><img src=\"/img/hook.png\" alt=\"/img/hook.png\"></p>\n<p>添加一条Webhook,填写请求地址<code>http://xx.xx.xx.xx:7777/webhook</code>,这样每当Github收到push或者其他事件时就会自动向这个地址发送一条POST请求。</p>\n<p>下面在服务器上补充这个请求地址,我们用Node搭一个简单的http服务,这里用到了<a href=\"https://github.com/rvagg/github-webhook-handler\" target=\"_blank\" rel=\"external\">github-webhook-handler</a>处理hook消息,创建文件<code>server.js</code></p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div><div class=\"line\">15</div><div class=\"line\">16</div><div class=\"line\">17</div><div class=\"line\">18</div><div class=\"line\">19</div><div class=\"line\">20</div><div class=\"line\">21</div><div class=\"line\">22</div><div class=\"line\">23</div><div class=\"line\">24</div><div class=\"line\">25</div><div class=\"line\">26</div><div class=\"line\">27</div><div class=\"line\">28</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"keyword\">var</span> http = <span class=\"built_in\">require</span>(<span class=\"string\">'http'</span>)</div><div class=\"line\"><span class=\"keyword\">var</span> exec = <span class=\"built_in\">require</span>(<span class=\"string\">'child_process'</span>).exec;</div><div class=\"line\"><span class=\"keyword\">var</span> createHandler = <span class=\"built_in\">require</span>(<span class=\"string\">'github-webhook-handler'</span>)</div><div class=\"line\"><span class=\"keyword\">var</span> handler = createHandler({ <span class=\"attr\">path</span>: <span class=\"string\">'/webhook'</span>, <span class=\"attr\">secret</span>: <span class=\"string\">'********'</span> });</div><div class=\"line\"></div><div class=\"line\">http.createServer(<span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">req, res</span>) </span>{</div><div class=\"line\"> handler(req, res, <span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">err</span>) </span>{</div><div class=\"line\"> res.statusCode = <span class=\"number\">404</span>;</div><div class=\"line\"> res.end(<span class=\"string\">'no such location'</span>);</div><div class=\"line\"> });</div><div class=\"line\">}).listen(<span class=\"number\">7777</span>);</div><div class=\"line\"></div><div class=\"line\">handler.on(<span class=\"string\">'error'</span>, <span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">err</span>) </span>{</div><div class=\"line\"> <span class=\"built_in\">console</span>.error(<span class=\"string\">'Error:'</span>, err.message);</div><div class=\"line\">});</div><div class=\"line\"></div><div class=\"line\">handler.on(<span class=\"string\">'push'</span>, <span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">event</span>) </span>{</div><div class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">'Received a push event for %s to %s'</span>,</div><div class=\"line\"> event.payload.repository.name,</div><div class=\"line\"> event.payload.ref);</div><div class=\"line\"> exec(<span class=\"string\">'/ppxu/blog/sync.sh'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">err, stdout, stderr</span>)</span>{</div><div class=\"line\"> <span class=\"keyword\">if</span>(err) {</div><div class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">'sync server err: '</span> + stderr);</div><div class=\"line\"> } <span class=\"keyword\">else</span> {</div><div class=\"line\"> <span class=\"built_in\">console</span>.log(stdout);</div><div class=\"line\"> }</div><div class=\"line\"> });</div><div class=\"line\">});</div></pre></td></tr></table></figure>\n<p>这里的<code>secret</code>要和在Github上新建hook时设置的一样,请求时校验用的。</p>\n<p>然后启动服务</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ node server.js &</div></pre></td></tr></table></figure>\n<p>这里也可以用forever之类的工具防止进程挂掉。</p>\n<p>这样一套自动部署系统就建立好了,在本机和服务器的实际效果如下:</p>\n<p><img src=\"/img/deploy.png\" alt=\"/img/deploy.png\"></p>\n<p><img src=\"/img/sync.png\" alt=\"/img/sync.png\"></p>\n<p>感觉生活一下子美好起来了呢:)</p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li>\n<p><a href=\"https://hexo.io/zh-cn/docs/deployment.html\" target=\"_blank\" rel=\"external\">https://hexo.io/zh-cn/docs/deployment.html</a></p>\n</li>\n<li>\n<p><a href=\"https://developer.github.com/webhooks/\" target=\"_blank\" rel=\"external\">https://developer.github.com/webhooks/</a></p>\n</li>\n<li>\n<p><a href=\"http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html\" target=\"_blank\" rel=\"external\">http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html</a></p>\n</li>\n</ul>\n","excerpt":"<p>博客之前的更新方式是先在本地写好文章,push到Github,然后ssh连到服务器上,再pull下来,如果每次都要这样操作一遍实在麻烦,今天就试着用Github的Webhooks功能实现了Hexo博客的自动部署,过程记录如下。</p>","more":"<p>整个过程主要有两个环节:</p>\n<h3 id=\"本地代码自动部署到github\"><a class=\"header-anchor\" href=\"#本地代码自动部署到github\">¶</a>本地代码自动部署到Github</h3>\n<p>Hexo本身就有deploy功能,只要在<code>_config.yml</code>里面做一下<a href=\"https://hexo.io/zh-cn/docs/deployment.html\">配置</a>,就可以部署到Github、Heroku等平台上,如果博客是托管在Github Pages上的话使用这种方式可以很方便的实现自动部署,不过通过这种方式发送到Github上的只有<code>public</code>静态文件目录,我这里希望托管整个应用的代码,就不能使用这种方式了,反正只要可以push就行了,我们搬出shell大法好。</p>\n<p>创建文件<code>deploy.sh</code></p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"meta\">#!/bin/bash</div><div class=\"line\"></span></div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] deploy hexo start \\033[0m\"</span></div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] hexo generate... \\033[0m\"</span></div><div class=\"line\">hexo g</div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] git commit... \\033[0m\"</span></div><div class=\"line\">d=`date +%x-%T`</div><div class=\"line\">git add .</div><div class=\"line\">git commit -m <span class=\"string\">\"auto deploy at \"</span><span class=\"variable\">${d}</span></div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] git push... \\033[0m\"</span></div><div class=\"line\">git push origin master</div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO DEPLOY] deploy hexo finish \\033[0m\"</span></div></pre></td></tr></table></figure>\n<p>然后增加权限</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ chmod +x ./deploy.sh</div></pre></td></tr></table></figure>\n<p>这样完成本地开发后,只要执行命令</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ./deploy.sh</div></pre></td></tr></table></figure>\n<p>就可以让Hexo生成静态文件并push到Github上。</p>\n<h3 id=\"github自动同步到服务器\"><a class=\"header-anchor\" href=\"#github自动同步到服务器\">¶</a>Github自动同步到服务器</h3>\n<p>为了让服务器可以自动同步Github上面的更新,我们需要用到Github的Webhooks。</p>\n<p>首先创建文件<code>sync.sh</code></p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"meta\">#!/bin/bash</div><div class=\"line\"></span></div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO SYNC] sync hexo start \\033[0m\"</span></div><div class=\"line\"><span class=\"built_in\">cd</span> /ppxu/blog</div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO SYNC] git pull... \\033[0m\"</span></div><div class=\"line\">git pull origin master</div><div class=\"line\"><span class=\"built_in\">echo</span> <span class=\"_\">-e</span> <span class=\"string\">\"\\033[32m [AUTO SYNC] sync hexo finish \\033[0m\"</span></div></pre></td></tr></table></figure>\n<p>目标是每当Github有push的时候就自动调用这个脚本。</p>\n<p>然后找到Github仓库的Settings页</p>\n<p><img src=\"/img/hook.png\" alt=\"/img/hook.png\"></p>\n<p>添加一条Webhook,填写请求地址<code>http://xx.xx.xx.xx:7777/webhook</code>,这样每当Github收到push或者其他事件时就会自动向这个地址发送一条POST请求。</p>\n<p>下面在服务器上补充这个请求地址,我们用Node搭一个简单的http服务,这里用到了<a href=\"https://github.com/rvagg/github-webhook-handler\">github-webhook-handler</a>处理hook消息,创建文件<code>server.js</code></p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div><div class=\"line\">15</div><div class=\"line\">16</div><div class=\"line\">17</div><div class=\"line\">18</div><div class=\"line\">19</div><div class=\"line\">20</div><div class=\"line\">21</div><div class=\"line\">22</div><div class=\"line\">23</div><div class=\"line\">24</div><div class=\"line\">25</div><div class=\"line\">26</div><div class=\"line\">27</div><div class=\"line\">28</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"keyword\">var</span> http = <span class=\"built_in\">require</span>(<span class=\"string\">'http'</span>)</div><div class=\"line\"><span class=\"keyword\">var</span> exec = <span class=\"built_in\">require</span>(<span class=\"string\">'child_process'</span>).exec;</div><div class=\"line\"><span class=\"keyword\">var</span> createHandler = <span class=\"built_in\">require</span>(<span class=\"string\">'github-webhook-handler'</span>)</div><div class=\"line\"><span class=\"keyword\">var</span> handler = createHandler({ <span class=\"attr\">path</span>: <span class=\"string\">'/webhook'</span>, <span class=\"attr\">secret</span>: <span class=\"string\">'********'</span> });</div><div class=\"line\"></div><div class=\"line\">http.createServer(<span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">req, res</span>) </span>{</div><div class=\"line\"> handler(req, res, <span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">err</span>) </span>{</div><div class=\"line\"> res.statusCode = <span class=\"number\">404</span>;</div><div class=\"line\"> res.end(<span class=\"string\">'no such location'</span>);</div><div class=\"line\"> });</div><div class=\"line\">}).listen(<span class=\"number\">7777</span>);</div><div class=\"line\"></div><div class=\"line\">handler.on(<span class=\"string\">'error'</span>, <span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">err</span>) </span>{</div><div class=\"line\"> <span class=\"built_in\">console</span>.error(<span class=\"string\">'Error:'</span>, err.message);</div><div class=\"line\">});</div><div class=\"line\"></div><div class=\"line\">handler.on(<span class=\"string\">'push'</span>, <span class=\"function\"><span class=\"keyword\">function</span> (<span class=\"params\">event</span>) </span>{</div><div class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">'Received a push event for %s to %s'</span>,</div><div class=\"line\"> event.payload.repository.name,</div><div class=\"line\"> event.payload.ref);</div><div class=\"line\"> exec(<span class=\"string\">'/ppxu/blog/sync.sh'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">err, stdout, stderr</span>)</span>{</div><div class=\"line\"> <span class=\"keyword\">if</span>(err) {</div><div class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">'sync server err: '</span> + stderr);</div><div class=\"line\"> } <span class=\"keyword\">else</span> {</div><div class=\"line\"> <span class=\"built_in\">console</span>.log(stdout);</div><div class=\"line\"> }</div><div class=\"line\"> });</div><div class=\"line\">});</div></pre></td></tr></table></figure>\n<p>这里的<code>secret</code>要和在Github上新建hook时设置的一样,请求时校验用的。</p>\n<p>然后启动服务</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ node server.js &</div></pre></td></tr></table></figure>\n<p>这里也可以用forever之类的工具防止进程挂掉。</p>\n<p>这样一套自动部署系统就建立好了,在本机和服务器的实际效果如下:</p>\n<p><img src=\"/img/deploy.png\" alt=\"/img/deploy.png\"></p>\n<p><img src=\"/img/sync.png\" alt=\"/img/sync.png\"></p>\n<p>感觉生活一下子美好起来了呢:)</p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li>\n<p><a href=\"https://hexo.io/zh-cn/docs/deployment.html\">https://hexo.io/zh-cn/docs/deployment.html</a></p>\n</li>\n<li>\n<p><a href=\"https://developer.github.com/webhooks/\">https://developer.github.com/webhooks/</a></p>\n</li>\n<li>\n<p><a href=\"http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html\">http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html</a></p>\n</li>\n</ul>"},{"title":"Nginx配置SPDY","date":"2015-12-22T09:32:20.000Z","_content":"本来准备给服务器搞个HTTP/2上去,发现Nginx要到1.9.5才可以支持HTTP/2协议,现在服务器上的Nginx版本才1.8.0,想了想先试试SPDY吧,改天再来升级Nginx和HTTP/2。\n\n<!--more-->\n\n首先查看一下本地的Nginx是不是已经包含了SPDY\n\n``` bash\n$ nginx -V |grep spdy\n```\n\n如果看到有`–-with-http_spdy_module`,就说明已经支持了SPDY,如果没有的话需要重新下载和编译Nginx,在编译的时候加上`--with-http_spdy_module`选项。\n\n然后修改Nginx的配置文件\n\n```\nserver {\n listen 80;\n listen 443 ssl spdy;\n server_name ppxu.me *.ppxu.me;\n\n add_header Alternate-Protocol 443:npn-spdy/3.1;\n ...\n```\n\n重启Nginx,SPDY就配置完成了。\n\n访问一下网站,然后在chrome中打开`chrome://net-internals/#http2`,就可以看到站点已经支持了SPDY3.1\n\n\n\n还可以在这个[网站](https://spdycheck.org/)检查SPDY情况。\n\n不过毕竟SPDY协议已经废弃了,还是赶紧搞上HTTP/2才是正事。\n\n#### 参考资料\n\n* [http://nginx.org/](http://nginx.org/)\n\n* [http://www.stefanwille.com/2013/04/using-spdy-with-nginx-1-4/](http://www.stefanwille.com/2013/04/using-spdy-with-nginx-1-4/)\n\n* [http://www.linuxidc.com/Linux/2015-09/123251.htm](http://www.linuxidc.com/Linux/2015-09/123251.htm)\n\n* [http://www.linuxidc.com/Linux/2015-02/112979.htm](http://www.linuxidc.com/Linux/2015-02/112979.htm)\n\n* [http://www.jb51.net/article/59017.htm](http://www.jb51.net/article/59017.htm)\n\n* [http://www.tuicool.com/articles/2mi63q](http://www.tuicool.com/articles/2mi63q)\n","source":"_posts/config-spdy-in-nginx.md","raw":"title: Nginx配置SPDY\ndate: 2015-12-22 17:32:20\ncategories: blog\ntags: [aliyun, ecs, centos, nginx, spdy]\n---\n本来准备给服务器搞个HTTP/2上去,发现Nginx要到1.9.5才可以支持HTTP/2协议,现在服务器上的Nginx版本才1.8.0,想了想先试试SPDY吧,改天再来升级Nginx和HTTP/2。\n\n<!--more-->\n\n首先查看一下本地的Nginx是不是已经包含了SPDY\n\n``` bash\n$ nginx -V |grep spdy\n```\n\n如果看到有`–-with-http_spdy_module`,就说明已经支持了SPDY,如果没有的话需要重新下载和编译Nginx,在编译的时候加上`--with-http_spdy_module`选项。\n\n然后修改Nginx的配置文件\n\n```\nserver {\n listen 80;\n listen 443 ssl spdy;\n server_name ppxu.me *.ppxu.me;\n\n add_header Alternate-Protocol 443:npn-spdy/3.1;\n ...\n```\n\n重启Nginx,SPDY就配置完成了。\n\n访问一下网站,然后在chrome中打开`chrome://net-internals/#http2`,就可以看到站点已经支持了SPDY3.1\n\n\n\n还可以在这个[网站](https://spdycheck.org/)检查SPDY情况。\n\n不过毕竟SPDY协议已经废弃了,还是赶紧搞上HTTP/2才是正事。\n\n#### 参考资料\n\n* [http://nginx.org/](http://nginx.org/)\n\n* [http://www.stefanwille.com/2013/04/using-spdy-with-nginx-1-4/](http://www.stefanwille.com/2013/04/using-spdy-with-nginx-1-4/)\n\n* [http://www.linuxidc.com/Linux/2015-09/123251.htm](http://www.linuxidc.com/Linux/2015-09/123251.htm)\n\n* [http://www.linuxidc.com/Linux/2015-02/112979.htm](http://www.linuxidc.com/Linux/2015-02/112979.htm)\n\n* [http://www.jb51.net/article/59017.htm](http://www.jb51.net/article/59017.htm)\n\n* [http://www.tuicool.com/articles/2mi63q](http://www.tuicool.com/articles/2mi63q)\n","slug":"config-spdy-in-nginx","published":1,"updated":"2017-02-21T07:09:01.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7sint0001a3xxvura8kul","content":"<p>本来准备给服务器搞个HTTP/2上去,发现Nginx要到1.9.5才可以支持HTTP/2协议,现在服务器上的Nginx版本才1.8.0,想了想先试试SPDY吧,改天再来升级Nginx和HTTP/2。</p>\n<a id=\"more\"></a>\n<p>首先查看一下本地的Nginx是不是已经包含了SPDY</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ nginx -V |grep spdy</div></pre></td></tr></table></figure>\n<p>如果看到有<code>–-with-http_spdy_module</code>,就说明已经支持了SPDY,如果没有的话需要重新下载和编译Nginx,在编译的时候加上<code>--with-http_spdy_module</code>选项。</p>\n<p>然后修改Nginx的配置文件</p>\n<figure class=\"highlight elixir\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> listen <span class=\"number\">80</span>;</div><div class=\"line\"> listen <span class=\"number\">443</span> ssl spdy;</div><div class=\"line\"> server_name ppxu.me *.ppxu.me;</div><div class=\"line\"></div><div class=\"line\"> add_header Alternate-Protocol <span class=\"number\">443</span><span class=\"symbol\">:npn-spdy/</span><span class=\"number\">3.1</span>;</div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<p>重启Nginx,SPDY就配置完成了。</p>\n<p>访问一下网站,然后在chrome中打开<code>chrome://net-internals/#http2</code>,就可以看到站点已经支持了SPDY3.1</p>\n<p><img src=\"/img/spdy.png\" alt=\"/img/spdy.png\"></p>\n<p>还可以在这个<a href=\"https://spdycheck.org/\" target=\"_blank\" rel=\"external\">网站</a>检查SPDY情况。</p>\n<p>不过毕竟SPDY协议已经废弃了,还是赶紧搞上HTTP/2才是正事。</p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li>\n<p><a href=\"http://nginx.org/\" target=\"_blank\" rel=\"external\">http://nginx.org/</a></p>\n</li>\n<li>\n<p><a href=\"http://www.stefanwille.com/2013/04/using-spdy-with-nginx-1-4/\" target=\"_blank\" rel=\"external\">http://www.stefanwille.com/2013/04/using-spdy-with-nginx-1-4/</a></p>\n</li>\n<li>\n<p><a href=\"http://www.linuxidc.com/Linux/2015-09/123251.htm\" target=\"_blank\" rel=\"external\">http://www.linuxidc.com/Linux/2015-09/123251.htm</a></p>\n</li>\n<li>\n<p><a href=\"http://www.linuxidc.com/Linux/2015-02/112979.htm\" target=\"_blank\" rel=\"external\">http://www.linuxidc.com/Linux/2015-02/112979.htm</a></p>\n</li>\n<li>\n<p><a href=\"http://www.jb51.net/article/59017.htm\" target=\"_blank\" rel=\"external\">http://www.jb51.net/article/59017.htm</a></p>\n</li>\n<li>\n<p><a href=\"http://www.tuicool.com/articles/2mi63q\" target=\"_blank\" rel=\"external\">http://www.tuicool.com/articles/2mi63q</a></p>\n</li>\n</ul>\n","excerpt":"<p>本来准备给服务器搞个HTTP/2上去,发现Nginx要到1.9.5才可以支持HTTP/2协议,现在服务器上的Nginx版本才1.8.0,想了想先试试SPDY吧,改天再来升级Nginx和HTTP/2。</p>","more":"<p>首先查看一下本地的Nginx是不是已经包含了SPDY</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ nginx -V |grep spdy</div></pre></td></tr></table></figure>\n<p>如果看到有<code>–-with-http_spdy_module</code>,就说明已经支持了SPDY,如果没有的话需要重新下载和编译Nginx,在编译的时候加上<code>--with-http_spdy_module</code>选项。</p>\n<p>然后修改Nginx的配置文件</p>\n<figure class=\"highlight elixir\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> listen <span class=\"number\">80</span>;</div><div class=\"line\"> listen <span class=\"number\">443</span> ssl spdy;</div><div class=\"line\"> server_name ppxu.me *.ppxu.me;</div><div class=\"line\"></div><div class=\"line\"> add_header Alternate-Protocol <span class=\"number\">443</span><span class=\"symbol\">:npn-spdy/</span><span class=\"number\">3.1</span>;</div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<p>重启Nginx,SPDY就配置完成了。</p>\n<p>访问一下网站,然后在chrome中打开<code>chrome://net-internals/#http2</code>,就可以看到站点已经支持了SPDY3.1</p>\n<p><img src=\"/img/spdy.png\" alt=\"/img/spdy.png\"></p>\n<p>还可以在这个<a href=\"https://spdycheck.org/\">网站</a>检查SPDY情况。</p>\n<p>不过毕竟SPDY协议已经废弃了,还是赶紧搞上HTTP/2才是正事。</p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li>\n<p><a href=\"http://nginx.org/\">http://nginx.org/</a></p>\n</li>\n<li>\n<p><a href=\"http://www.stefanwille.com/2013/04/using-spdy-with-nginx-1-4/\">http://www.stefanwille.com/2013/04/using-spdy-with-nginx-1-4/</a></p>\n</li>\n<li>\n<p><a href=\"http://www.linuxidc.com/Linux/2015-09/123251.htm\">http://www.linuxidc.com/Linux/2015-09/123251.htm</a></p>\n</li>\n<li>\n<p><a href=\"http://www.linuxidc.com/Linux/2015-02/112979.htm\">http://www.linuxidc.com/Linux/2015-02/112979.htm</a></p>\n</li>\n<li>\n<p><a href=\"http://www.jb51.net/article/59017.htm\">http://www.jb51.net/article/59017.htm</a></p>\n</li>\n<li>\n<p><a href=\"http://www.tuicool.com/articles/2mi63q\">http://www.tuicool.com/articles/2mi63q</a></p>\n</li>\n</ul>"},{"title":"阿里云ECS配置Nginx和SSL","date":"2015-12-20T14:01:05.000Z","_content":"今天给服务器安装了Nginx,并配置了SSL,网址左边终于有了萌萌哒的小绿锁,这里还是做一下记录。\n\n<!--more-->\n\n先是安装Nginx\n\n``` bash\n$ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm\n$ rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm\n$ yum install nginx\n```\n\n启动Nginx\n\n``` bash\n$ systemctl start nginx\n```\n\n这样Nginx服务就已经启动完成了,输入服务器ip地址就可以看到Nginx主页,然后我们需要把Hexo服务的4000端口转发到Nginx的80端口,查看Nginx配置文件`/etc/nginx/nginx.conf`,看到需要修改`conf.d`目录下的`/etc/nginx/conf.d/default.conf`文件\n\n```\nserver {\n listen 80;\n server_name ppxu.me *.ppxu.me;\n\n location / {\n proxy_pass http://127.0.0.1:4000/;\n proxy_redirect off;\n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n }\n\n ...\n```\n\n重启Nginx\n\n``` bash\n$ systemctl restart nginx\n```\n\n现在访问网站,在响应头里就可以看到`Server:nginx/1.8.0`,说明Nginx环境已经正常了,下面准备搞上HTTPS。\n\n这里就不详述HTTPS的原理了,对我们这个小博客而言,搞个免费的SSL证书就可以了,这里推荐[https://startssl.com/](https://startssl.com/)或者[https://www.wosign.com/](https://www.wosign.com/),申请成功后把下载下来的对应版本的证书文件上传到服务器上,包含公钥`xx.crt`和私钥`xx.key`。\n\n然后编辑`/etc/nginx/conf.d/default.conf`\n\n```\nserver {\n listen 80;\n listen 443 ssl;\n server_name ppxu.me *.ppxu.me;\n\n ssl on;\n ssl_certificate /etc/nginx/conf.d/ppxu.crt;\n ssl_certificate_key /etc/nginx/conf.d/ppxu.key;\n ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\n ssl_ciphers 'AES128+EECDH:AES128+EDH:!aNULL';\n ssl_prefer_server_ciphers on;\n ssl_session_cache shared:SSL:10m;\n ssl_session_timeout 10m;\n ssl_stapling on;\n ssl_stapling_verify on;\n\n location / {\n proxy_pass http://127.0.0.1:4000/;\n proxy_redirect off;\n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n }\n\n ...\n```\n\n重启Nginx,如果一切顺利,现在就可以通过`https://ppxu.me`访问到本网站了,但是如果直接输入`ppxu.me`的话会报400错误,显示\n\n``` html\nThe plain HTTP request was sent to HTTPS port\n```\n\n所以我们需要将http的请求强制使用https访问,最方便的办法是启用HSTS,在nginx的配置中添加\n\n```\nadd_header Strict-Transport-Security \"max-age=31536000; includeSubdomains\";\n```\n\n这样只要是支持HSTS的浏览器,就可以自动完成http到https的替换,而且是直接在浏览器本地完成的,对于不支持的浏览器,只能在服务器做重定向了,继续在Nginx配置中添加一条规则\n\n```\nerror_page 497 https://$host$uri;\n```\n\n这样我们的网站就已经完全支持了HTTPS访问,可以在这个网站[https://www.ssllabs.com/ssltest/analyze.html](https://www.ssllabs.com/ssltest/analyze.html)对网页进行安全评测,如果评分不够高的话可以再看看如何[加强nginx的SSL安全](http://www.oschina.net/translate/strong_ssl_security_on_nginx)。\n\n\n\n#### 参考资料\n\n* [http://codybonney.com/installing-nginx-on-centos-6-4/](http://codybonney.com/installing-nginx-on-centos-6-4/)\n* [http://www.tuijiankan.com/2015/05/04/阿里云Centos6安装配置Nodejs、Nginx、Hexo操作记录/](http://www.tuijiankan.com/2015/05/04/%E9%98%BF%E9%87%8C%E4%BA%91Centos6%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AENodejs%E3%80%81Nginx%E3%80%81Hexo%E6%93%8D%E4%BD%9C%E8%AE%B0%E5%BD%95/)\n* [http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/](http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/)\n* [https://imququ.com/post/switch-to-https.html](https://imququ.com/post/switch-to-https.html)\n* [https://imququ.com/post/sth-about-switch-to-https.html](https://imququ.com/post/sth-about-switch-to-https.html)\n* [http://www.ha97.com/5194.html](http://www.ha97.com/5194.html)\n* [http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html](http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html)\n* [http://www.codeceo.com/article/nginx-ssl-nodejs.html](http://www.codeceo.com/article/nginx-ssl-nodejs.html)\n* [http://www.ttlsa.com/nginx/nginx-node-js/](http://www.ttlsa.com/nginx/nginx-node-js/)\n* [http://blog.csdn.net/wzy_1988/article/details/8549290](http://blog.csdn.net/wzy_1988/article/details/8549290)\n* [http://www.tutugreen.com/wordpress/upgrade-ssl/](http://www.tutugreen.com/wordpress/upgrade-ssl/)\n* [http://www.oschina.net/translate/strong_ssl_security_on_nginx](http://www.oschina.net/translate/strong_ssl_security_on_nginx)\n* [http://blog.jobbole.com/44844/](http://blog.jobbole.com/44844/)\n* [http://blog.jobbole.com/80591/](http://blog.jobbole.com/80591/)\n* [https://imququ.com/post/my-nginx-conf-for-wpo.html](https://imququ.com/post/my-nginx-conf-for-wpo.html)\n","source":"_posts/config-ssl-in-nginx.md","raw":"title: 阿里云ECS配置Nginx和SSL\ndate: 2015-12-20 22:01:05\ncategories: blog\ntags: [aliyun, ecs, centos, nginx, ssl, https]\n---\n今天给服务器安装了Nginx,并配置了SSL,网址左边终于有了萌萌哒的小绿锁,这里还是做一下记录。\n\n<!--more-->\n\n先是安装Nginx\n\n``` bash\n$ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm\n$ rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm\n$ yum install nginx\n```\n\n启动Nginx\n\n``` bash\n$ systemctl start nginx\n```\n\n这样Nginx服务就已经启动完成了,输入服务器ip地址就可以看到Nginx主页,然后我们需要把Hexo服务的4000端口转发到Nginx的80端口,查看Nginx配置文件`/etc/nginx/nginx.conf`,看到需要修改`conf.d`目录下的`/etc/nginx/conf.d/default.conf`文件\n\n```\nserver {\n listen 80;\n server_name ppxu.me *.ppxu.me;\n\n location / {\n proxy_pass http://127.0.0.1:4000/;\n proxy_redirect off;\n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n }\n\n ...\n```\n\n重启Nginx\n\n``` bash\n$ systemctl restart nginx\n```\n\n现在访问网站,在响应头里就可以看到`Server:nginx/1.8.0`,说明Nginx环境已经正常了,下面准备搞上HTTPS。\n\n这里就不详述HTTPS的原理了,对我们这个小博客而言,搞个免费的SSL证书就可以了,这里推荐[https://startssl.com/](https://startssl.com/)或者[https://www.wosign.com/](https://www.wosign.com/),申请成功后把下载下来的对应版本的证书文件上传到服务器上,包含公钥`xx.crt`和私钥`xx.key`。\n\n然后编辑`/etc/nginx/conf.d/default.conf`\n\n```\nserver {\n listen 80;\n listen 443 ssl;\n server_name ppxu.me *.ppxu.me;\n\n ssl on;\n ssl_certificate /etc/nginx/conf.d/ppxu.crt;\n ssl_certificate_key /etc/nginx/conf.d/ppxu.key;\n ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\n ssl_ciphers 'AES128+EECDH:AES128+EDH:!aNULL';\n ssl_prefer_server_ciphers on;\n ssl_session_cache shared:SSL:10m;\n ssl_session_timeout 10m;\n ssl_stapling on;\n ssl_stapling_verify on;\n\n location / {\n proxy_pass http://127.0.0.1:4000/;\n proxy_redirect off;\n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n }\n\n ...\n```\n\n重启Nginx,如果一切顺利,现在就可以通过`https://ppxu.me`访问到本网站了,但是如果直接输入`ppxu.me`的话会报400错误,显示\n\n``` html\nThe plain HTTP request was sent to HTTPS port\n```\n\n所以我们需要将http的请求强制使用https访问,最方便的办法是启用HSTS,在nginx的配置中添加\n\n```\nadd_header Strict-Transport-Security \"max-age=31536000; includeSubdomains\";\n```\n\n这样只要是支持HSTS的浏览器,就可以自动完成http到https的替换,而且是直接在浏览器本地完成的,对于不支持的浏览器,只能在服务器做重定向了,继续在Nginx配置中添加一条规则\n\n```\nerror_page 497 https://$host$uri;\n```\n\n这样我们的网站就已经完全支持了HTTPS访问,可以在这个网站[https://www.ssllabs.com/ssltest/analyze.html](https://www.ssllabs.com/ssltest/analyze.html)对网页进行安全评测,如果评分不够高的话可以再看看如何[加强nginx的SSL安全](http://www.oschina.net/translate/strong_ssl_security_on_nginx)。\n\n\n\n#### 参考资料\n\n* [http://codybonney.com/installing-nginx-on-centos-6-4/](http://codybonney.com/installing-nginx-on-centos-6-4/)\n* [http://www.tuijiankan.com/2015/05/04/阿里云Centos6安装配置Nodejs、Nginx、Hexo操作记录/](http://www.tuijiankan.com/2015/05/04/%E9%98%BF%E9%87%8C%E4%BA%91Centos6%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AENodejs%E3%80%81Nginx%E3%80%81Hexo%E6%93%8D%E4%BD%9C%E8%AE%B0%E5%BD%95/)\n* [http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/](http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/)\n* [https://imququ.com/post/switch-to-https.html](https://imququ.com/post/switch-to-https.html)\n* [https://imququ.com/post/sth-about-switch-to-https.html](https://imququ.com/post/sth-about-switch-to-https.html)\n* [http://www.ha97.com/5194.html](http://www.ha97.com/5194.html)\n* [http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html](http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html)\n* [http://www.codeceo.com/article/nginx-ssl-nodejs.html](http://www.codeceo.com/article/nginx-ssl-nodejs.html)\n* [http://www.ttlsa.com/nginx/nginx-node-js/](http://www.ttlsa.com/nginx/nginx-node-js/)\n* [http://blog.csdn.net/wzy_1988/article/details/8549290](http://blog.csdn.net/wzy_1988/article/details/8549290)\n* [http://www.tutugreen.com/wordpress/upgrade-ssl/](http://www.tutugreen.com/wordpress/upgrade-ssl/)\n* [http://www.oschina.net/translate/strong_ssl_security_on_nginx](http://www.oschina.net/translate/strong_ssl_security_on_nginx)\n* [http://blog.jobbole.com/44844/](http://blog.jobbole.com/44844/)\n* [http://blog.jobbole.com/80591/](http://blog.jobbole.com/80591/)\n* [https://imququ.com/post/my-nginx-conf-for-wpo.html](https://imququ.com/post/my-nginx-conf-for-wpo.html)\n","slug":"config-ssl-in-nginx","published":1,"updated":"2017-02-21T07:09:23.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7sio40004a3xxrlp2zxu9","content":"<p>今天给服务器安装了Nginx,并配置了SSL,网址左边终于有了萌萌哒的小绿锁,这里还是做一下记录。</p>\n<a id=\"more\"></a>\n<p>先是安装Nginx</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm</div><div class=\"line\">$ rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm</div><div class=\"line\">$ yum install nginx</div></pre></td></tr></table></figure>\n<p>启动Nginx</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ systemctl start nginx</div></pre></td></tr></table></figure>\n<p>这样Nginx服务就已经启动完成了,输入服务器ip地址就可以看到Nginx主页,然后我们需要把Hexo服务的4000端口转发到Nginx的80端口,查看Nginx配置文件<code>/etc/nginx/nginx.conf</code>,看到需要修改<code>conf.d</code>目录下的<code>/etc/nginx/conf.d/default.conf</code>文件</p>\n<figure class=\"highlight tp\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> listen <span class=\"number\">80</span>;</div><div class=\"line\"> server_name ppxu.me *.ppxu.me;</div><div class=\"line\"></div><div class=\"line\"> location / {</div><div class=\"line\"> proxy_pass http:<span class=\"comment\">//127.0.0.1:4000/;</span></div><div class=\"line\"> proxy_redirect off;</div><div class=\"line\"> proxy_set_header <span class=\"keyword\">X</span>-Real-IP <span class=\"variable\">$remote_addr</span>;</div><div class=\"line\"> proxy_set_header <span class=\"keyword\">X</span>-Forwarded-For <span class=\"variable\">$proxy_add_x_forwarded_for</span>;</div><div class=\"line\"> }</div><div class=\"line\"></div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<p>重启Nginx</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ systemctl restart nginx</div></pre></td></tr></table></figure>\n<p>现在访问网站,在响应头里就可以看到<code>Server:nginx/1.8.0</code>,说明Nginx环境已经正常了,下面准备搞上HTTPS。</p>\n<p>这里就不详述HTTPS的原理了,对我们这个小博客而言,搞个免费的SSL证书就可以了,这里推荐<a href=\"https://startssl.com/\" target=\"_blank\" rel=\"external\">https://startssl.com/</a>或者<a href=\"https://www.wosign.com/\" target=\"_blank\" rel=\"external\">https://www.wosign.com/</a>,申请成功后把下载下来的对应版本的证书文件上传到服务器上,包含公钥<code>xx.crt</code>和私钥<code>xx.key</code>。</p>\n<p>然后编辑<code>/etc/nginx/conf.d/default.conf</code></p>\n<figure class=\"highlight stata\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div><div class=\"line\">15</div><div class=\"line\">16</div><div class=\"line\">17</div><div class=\"line\">18</div><div class=\"line\">19</div><div class=\"line\">20</div><div class=\"line\">21</div><div class=\"line\">22</div><div class=\"line\">23</div><div class=\"line\">24</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> listen 80;</div><div class=\"line\"> listen 443 ssl;</div><div class=\"line\"> server_name ppxu.me *.ppxu.me;</div><div class=\"line\"></div><div class=\"line\"> ssl <span class=\"keyword\">on</span>;</div><div class=\"line\"> ssl_certificate /etc/nginx/<span class=\"keyword\">conf</span>.<span class=\"keyword\">d</span>/ppxu.crt;</div><div class=\"line\"> ssl_certificate_key /etc/nginx/<span class=\"keyword\">conf</span>.<span class=\"keyword\">d</span>/ppxu.key;</div><div class=\"line\"> ssl_protocols TLSv1 TLSv1.1 TLSv1.2;</div><div class=\"line\"> ssl_ciphers 'AES128+EECDH:AES128+EDH:!aNULL';</div><div class=\"line\"> ssl_prefer_server_ciphers <span class=\"keyword\">on</span>;</div><div class=\"line\"> ssl_session_cache shared:SSL:10m;</div><div class=\"line\"> ssl_session_timeout 10m;</div><div class=\"line\"> ssl_stapling <span class=\"keyword\">on</span>;</div><div class=\"line\"> ssl_stapling_verify <span class=\"keyword\">on</span>;</div><div class=\"line\"></div><div class=\"line\"> location / {</div><div class=\"line\"> proxy_pass http:<span class=\"comment\">//127.0.0.1:4000/;</span></div><div class=\"line\"> proxy_redirect off;</div><div class=\"line\"> proxy_set_header X-Real-IP <span class=\"variable\">$remote_addr</span>;</div><div class=\"line\"> proxy_set_header X-Forwarded-<span class=\"keyword\">For</span> <span class=\"variable\">$proxy_add_x_forwarded_for</span>;</div><div class=\"line\"> }</div><div class=\"line\"></div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<p>重启Nginx,如果一切顺利,现在就可以通过<code>https://ppxu.me</code>访问到本网站了,但是如果直接输入<code>ppxu.me</code>的话会报400错误,显示</p>\n<figure class=\"highlight html\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">The plain HTTP request was sent to HTTPS port</div></pre></td></tr></table></figure>\n<p>所以我们需要将http的请求强制使用https访问,最方便的办法是启用HSTS,在nginx的配置中添加</p>\n<figure class=\"highlight armasm\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"keyword\">add_header </span><span class=\"keyword\">Strict-Transport-Security </span><span class=\"string\">\"max-age=31536000; includeSubdomains\"</span><span class=\"comment\">;</span></div></pre></td></tr></table></figure>\n<p>这样只要是支持HSTS的浏览器,就可以自动完成http到https的替换,而且是直接在浏览器本地完成的,对于不支持的浏览器,只能在服务器做重定向了,继续在Nginx配置中添加一条规则</p>\n<figure class=\"highlight nginx\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"attribute\">error_page</span> <span class=\"number\">497</span> https://<span class=\"variable\">$host</span><span class=\"variable\">$uri</span>;</div></pre></td></tr></table></figure>\n<p>这样我们的网站就已经完全支持了HTTPS访问,可以在这个网站<a href=\"https://www.ssllabs.com/ssltest/analyze.html\" target=\"_blank\" rel=\"external\">https://www.ssllabs.com/ssltest/analyze.html</a>对网页进行安全评测,如果评分不够高的话可以再看看如何<a href=\"http://www.oschina.net/translate/strong_ssl_security_on_nginx\" target=\"_blank\" rel=\"external\">加强nginx的SSL安全</a>。</p>\n<p><img src=\"/img/ssl.png\" alt=\"/img/ssl.png\"></p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li><a href=\"http://codybonney.com/installing-nginx-on-centos-6-4/\" target=\"_blank\" rel=\"external\">http://codybonney.com/installing-nginx-on-centos-6-4/</a></li>\n<li><a href=\"http://www.tuijiankan.com/2015/05/04/%E9%98%BF%E9%87%8C%E4%BA%91Centos6%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AENodejs%E3%80%81Nginx%E3%80%81Hexo%E6%93%8D%E4%BD%9C%E8%AE%B0%E5%BD%95/\" target=\"_blank\" rel=\"external\">http://www.tuijiankan.com/2015/05/04/阿里云Centos6安装配置Nodejs、Nginx、Hexo操作记录/</a></li>\n<li><a href=\"http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/\" target=\"_blank\" rel=\"external\">http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/</a></li>\n<li><a href=\"https://imququ.com/post/switch-to-https.html\" target=\"_blank\" rel=\"external\">https://imququ.com/post/switch-to-https.html</a></li>\n<li><a href=\"https://imququ.com/post/sth-about-switch-to-https.html\" target=\"_blank\" rel=\"external\">https://imququ.com/post/sth-about-switch-to-https.html</a></li>\n<li><a href=\"http://www.ha97.com/5194.html\" target=\"_blank\" rel=\"external\">http://www.ha97.com/5194.html</a></li>\n<li><a href=\"http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html\" target=\"_blank\" rel=\"external\">http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html</a></li>\n<li><a href=\"http://www.codeceo.com/article/nginx-ssl-nodejs.html\" target=\"_blank\" rel=\"external\">http://www.codeceo.com/article/nginx-ssl-nodejs.html</a></li>\n<li><a href=\"http://www.ttlsa.com/nginx/nginx-node-js/\" target=\"_blank\" rel=\"external\">http://www.ttlsa.com/nginx/nginx-node-js/</a></li>\n<li><a href=\"http://blog.csdn.net/wzy_1988/article/details/8549290\" target=\"_blank\" rel=\"external\">http://blog.csdn.net/wzy_1988/article/details/8549290</a></li>\n<li><a href=\"http://www.tutugreen.com/wordpress/upgrade-ssl/\" target=\"_blank\" rel=\"external\">http://www.tutugreen.com/wordpress/upgrade-ssl/</a></li>\n<li><a href=\"http://www.oschina.net/translate/strong_ssl_security_on_nginx\" target=\"_blank\" rel=\"external\">http://www.oschina.net/translate/strong_ssl_security_on_nginx</a></li>\n<li><a href=\"http://blog.jobbole.com/44844/\" target=\"_blank\" rel=\"external\">http://blog.jobbole.com/44844/</a></li>\n<li><a href=\"http://blog.jobbole.com/80591/\" target=\"_blank\" rel=\"external\">http://blog.jobbole.com/80591/</a></li>\n<li><a href=\"https://imququ.com/post/my-nginx-conf-for-wpo.html\" target=\"_blank\" rel=\"external\">https://imququ.com/post/my-nginx-conf-for-wpo.html</a></li>\n</ul>\n","excerpt":"<p>今天给服务器安装了Nginx,并配置了SSL,网址左边终于有了萌萌哒的小绿锁,这里还是做一下记录。</p>","more":"<p>先是安装Nginx</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm</div><div class=\"line\">$ rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm</div><div class=\"line\">$ yum install nginx</div></pre></td></tr></table></figure>\n<p>启动Nginx</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ systemctl start nginx</div></pre></td></tr></table></figure>\n<p>这样Nginx服务就已经启动完成了,输入服务器ip地址就可以看到Nginx主页,然后我们需要把Hexo服务的4000端口转发到Nginx的80端口,查看Nginx配置文件<code>/etc/nginx/nginx.conf</code>,看到需要修改<code>conf.d</code>目录下的<code>/etc/nginx/conf.d/default.conf</code>文件</p>\n<figure class=\"highlight tp\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> listen <span class=\"number\">80</span>;</div><div class=\"line\"> server_name ppxu.me *.ppxu.me;</div><div class=\"line\"></div><div class=\"line\"> location / {</div><div class=\"line\"> proxy_pass http:<span class=\"comment\">//127.0.0.1:4000/;</span></div><div class=\"line\"> proxy_redirect off;</div><div class=\"line\"> proxy_set_header <span class=\"keyword\">X</span>-Real-IP <span class=\"variable\">$remote_addr</span>;</div><div class=\"line\"> proxy_set_header <span class=\"keyword\">X</span>-Forwarded-For <span class=\"variable\">$proxy_add_x_forwarded_for</span>;</div><div class=\"line\"> }</div><div class=\"line\"></div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<p>重启Nginx</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ systemctl restart nginx</div></pre></td></tr></table></figure>\n<p>现在访问网站,在响应头里就可以看到<code>Server:nginx/1.8.0</code>,说明Nginx环境已经正常了,下面准备搞上HTTPS。</p>\n<p>这里就不详述HTTPS的原理了,对我们这个小博客而言,搞个免费的SSL证书就可以了,这里推荐<a href=\"https://startssl.com/\">https://startssl.com/</a>或者<a href=\"https://www.wosign.com/\">https://www.wosign.com/</a>,申请成功后把下载下来的对应版本的证书文件上传到服务器上,包含公钥<code>xx.crt</code>和私钥<code>xx.key</code>。</p>\n<p>然后编辑<code>/etc/nginx/conf.d/default.conf</code></p>\n<figure class=\"highlight stata\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div><div class=\"line\">15</div><div class=\"line\">16</div><div class=\"line\">17</div><div class=\"line\">18</div><div class=\"line\">19</div><div class=\"line\">20</div><div class=\"line\">21</div><div class=\"line\">22</div><div class=\"line\">23</div><div class=\"line\">24</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> listen 80;</div><div class=\"line\"> listen 443 ssl;</div><div class=\"line\"> server_name ppxu.me *.ppxu.me;</div><div class=\"line\"></div><div class=\"line\"> ssl <span class=\"keyword\">on</span>;</div><div class=\"line\"> ssl_certificate /etc/nginx/<span class=\"keyword\">conf</span>.<span class=\"keyword\">d</span>/ppxu.crt;</div><div class=\"line\"> ssl_certificate_key /etc/nginx/<span class=\"keyword\">conf</span>.<span class=\"keyword\">d</span>/ppxu.key;</div><div class=\"line\"> ssl_protocols TLSv1 TLSv1.1 TLSv1.2;</div><div class=\"line\"> ssl_ciphers 'AES128+EECDH:AES128+EDH:!aNULL';</div><div class=\"line\"> ssl_prefer_server_ciphers <span class=\"keyword\">on</span>;</div><div class=\"line\"> ssl_session_cache shared:SSL:10m;</div><div class=\"line\"> ssl_session_timeout 10m;</div><div class=\"line\"> ssl_stapling <span class=\"keyword\">on</span>;</div><div class=\"line\"> ssl_stapling_verify <span class=\"keyword\">on</span>;</div><div class=\"line\"></div><div class=\"line\"> location / {</div><div class=\"line\"> proxy_pass http:<span class=\"comment\">//127.0.0.1:4000/;</span></div><div class=\"line\"> proxy_redirect off;</div><div class=\"line\"> proxy_set_header X-Real-IP <span class=\"variable\">$remote_addr</span>;</div><div class=\"line\"> proxy_set_header X-Forwarded-<span class=\"keyword\">For</span> <span class=\"variable\">$proxy_add_x_forwarded_for</span>;</div><div class=\"line\"> }</div><div class=\"line\"></div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<p>重启Nginx,如果一切顺利,现在就可以通过<code>https://ppxu.me</code>访问到本网站了,但是如果直接输入<code>ppxu.me</code>的话会报400错误,显示</p>\n<figure class=\"highlight html\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">The plain HTTP request was sent to HTTPS port</div></pre></td></tr></table></figure>\n<p>所以我们需要将http的请求强制使用https访问,最方便的办法是启用HSTS,在nginx的配置中添加</p>\n<figure class=\"highlight armasm\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"keyword\">add_header </span><span class=\"keyword\">Strict-Transport-Security </span><span class=\"string\">\"max-age=31536000; includeSubdomains\"</span><span class=\"comment\">;</span></div></pre></td></tr></table></figure>\n<p>这样只要是支持HSTS的浏览器,就可以自动完成http到https的替换,而且是直接在浏览器本地完成的,对于不支持的浏览器,只能在服务器做重定向了,继续在Nginx配置中添加一条规则</p>\n<figure class=\"highlight nginx\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"attribute\">error_page</span> <span class=\"number\">497</span> https://<span class=\"variable\">$host</span><span class=\"variable\">$uri</span>;</div></pre></td></tr></table></figure>\n<p>这样我们的网站就已经完全支持了HTTPS访问,可以在这个网站<a href=\"https://www.ssllabs.com/ssltest/analyze.html\">https://www.ssllabs.com/ssltest/analyze.html</a>对网页进行安全评测,如果评分不够高的话可以再看看如何<a href=\"http://www.oschina.net/translate/strong_ssl_security_on_nginx\">加强nginx的SSL安全</a>。</p>\n<p><img src=\"/img/ssl.png\" alt=\"/img/ssl.png\"></p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li><a href=\"http://codybonney.com/installing-nginx-on-centos-6-4/\">http://codybonney.com/installing-nginx-on-centos-6-4/</a></li>\n<li><a href=\"http://www.tuijiankan.com/2015/05/04/%E9%98%BF%E9%87%8C%E4%BA%91Centos6%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AENodejs%E3%80%81Nginx%E3%80%81Hexo%E6%93%8D%E4%BD%9C%E8%AE%B0%E5%BD%95/\">http://www.tuijiankan.com/2015/05/04/阿里云Centos6安装配置Nodejs、Nginx、Hexo操作记录/</a></li>\n<li><a href=\"http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/\">http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/</a></li>\n<li><a href=\"https://imququ.com/post/switch-to-https.html\">https://imququ.com/post/switch-to-https.html</a></li>\n<li><a href=\"https://imququ.com/post/sth-about-switch-to-https.html\">https://imququ.com/post/sth-about-switch-to-https.html</a></li>\n<li><a href=\"http://www.ha97.com/5194.html\">http://www.ha97.com/5194.html</a></li>\n<li><a href=\"http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html\">http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html</a></li>\n<li><a href=\"http://www.codeceo.com/article/nginx-ssl-nodejs.html\">http://www.codeceo.com/article/nginx-ssl-nodejs.html</a></li>\n<li><a href=\"http://www.ttlsa.com/nginx/nginx-node-js/\">http://www.ttlsa.com/nginx/nginx-node-js/</a></li>\n<li><a href=\"http://blog.csdn.net/wzy_1988/article/details/8549290\">http://blog.csdn.net/wzy_1988/article/details/8549290</a></li>\n<li><a href=\"http://www.tutugreen.com/wordpress/upgrade-ssl/\">http://www.tutugreen.com/wordpress/upgrade-ssl/</a></li>\n<li><a href=\"http://www.oschina.net/translate/strong_ssl_security_on_nginx\">http://www.oschina.net/translate/strong_ssl_security_on_nginx</a></li>\n<li><a href=\"http://blog.jobbole.com/44844/\">http://blog.jobbole.com/44844/</a></li>\n<li><a href=\"http://blog.jobbole.com/80591/\">http://blog.jobbole.com/80591/</a></li>\n<li><a href=\"https://imququ.com/post/my-nginx-conf-for-wpo.html\">https://imququ.com/post/my-nginx-conf-for-wpo.html</a></li>\n</ul>"},{"title":"让Hexo支持emoji表情","date":"2015-12-24T13:23:43.000Z","_content":"Hexo的文章内容默认是不支持emoji表情的,作为一个有逼格的码农,怎么少得了丰富的表情语言,今天就研究了下怎么支持emoji。\n\n<!--more-->\n\nHexo默认的markdown编译插件是[hexo-renderer-marked](https://github.com/hexojs/hexo-renderer-marked),看了一下相关文档,好像没办法支持emoji,还好在Hexo的[plugins](https://hexo.io/plugins/)页,我们找到了另外一个markdown插件[hexo-renderer-markdown-it](https://github.com/celsomiranda/hexo-renderer-markdown-it),而且号称速度比默认的还要快,最主要的是,在[markdown-it](https://github.com/markdown-it/markdown-it)的文档里面,我们发现它可以通过plugins的方式支持[emoji](https://github.com/markdown-it/markdown-it-emoji)。\n\n下面我们就来替换markdown插件\n\n``` bash\n$ cd /ppxu/blog/\n$ npm un hexo-renderer-marked --save\n$ npm i hexo-renderer-markdown-it --save\n```\n\n不过此时的hexo-renderer-markdown-it还是用不了emoji的,我们需要加上emoji的plugin\n\n``` bash\n$ cd node_modules/hexo-renderer-markdown-it/\n$ npm install markdown-it-emoji --save\n```\n\n然后编辑Hexo的配置文件`_config.yml`\n\n```\nmarkdown:\n render:\n html: true\n xhtmlOut: false\n breaks: false\n linkify: true\n typographer: true\n quotes: '“”‘’'\n plugins:\n - markdown-it-footnote\n - markdown-it-sup\n - markdown-it-sub\n - markdown-it-abbr\n - markdown-it-emoji\n anchors:\n level: 2\n collisionSuffix: 'v'\n permalink: true\n permalinkClass: header-anchor\n permalinkSymbol: ¶\n```\n\n关键就是在plugins里加上`- markdown-it-emoji`,其他的配置说明可以参见[wiki](https://github.com/celsomiranda/hexo-renderer-markdown-it/wiki/Advanced-Configuration)。\n\n重启Hexo服务,即可生效,这里输入`:smile: :smirk: :relieved:`,效果 :smile: :smirk: :relieved:\n\n当然直接输入表情也是可以的 😂\n","source":"_posts/enable-emoji-in-hexo.md","raw":"title: 让Hexo支持emoji表情\ndate: 2015-12-24 21:23:43\ncategories: blog\ntags: [hexo, markdown, emoji]\n---\nHexo的文章内容默认是不支持emoji表情的,作为一个有逼格的码农,怎么少得了丰富的表情语言,今天就研究了下怎么支持emoji。\n\n<!--more-->\n\nHexo默认的markdown编译插件是[hexo-renderer-marked](https://github.com/hexojs/hexo-renderer-marked),看了一下相关文档,好像没办法支持emoji,还好在Hexo的[plugins](https://hexo.io/plugins/)页,我们找到了另外一个markdown插件[hexo-renderer-markdown-it](https://github.com/celsomiranda/hexo-renderer-markdown-it),而且号称速度比默认的还要快,最主要的是,在[markdown-it](https://github.com/markdown-it/markdown-it)的文档里面,我们发现它可以通过plugins的方式支持[emoji](https://github.com/markdown-it/markdown-it-emoji)。\n\n下面我们就来替换markdown插件\n\n``` bash\n$ cd /ppxu/blog/\n$ npm un hexo-renderer-marked --save\n$ npm i hexo-renderer-markdown-it --save\n```\n\n不过此时的hexo-renderer-markdown-it还是用不了emoji的,我们需要加上emoji的plugin\n\n``` bash\n$ cd node_modules/hexo-renderer-markdown-it/\n$ npm install markdown-it-emoji --save\n```\n\n然后编辑Hexo的配置文件`_config.yml`\n\n```\nmarkdown:\n render:\n html: true\n xhtmlOut: false\n breaks: false\n linkify: true\n typographer: true\n quotes: '“”‘’'\n plugins:\n - markdown-it-footnote\n - markdown-it-sup\n - markdown-it-sub\n - markdown-it-abbr\n - markdown-it-emoji\n anchors:\n level: 2\n collisionSuffix: 'v'\n permalink: true\n permalinkClass: header-anchor\n permalinkSymbol: ¶\n```\n\n关键就是在plugins里加上`- markdown-it-emoji`,其他的配置说明可以参见[wiki](https://github.com/celsomiranda/hexo-renderer-markdown-it/wiki/Advanced-Configuration)。\n\n重启Hexo服务,即可生效,这里输入`:smile: :smirk: :relieved:`,效果 :smile: :smirk: :relieved:\n\n当然直接输入表情也是可以的 😂\n","slug":"enable-emoji-in-hexo","published":1,"updated":"2015-12-26T14:07:19.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7sio60005a3xxp6xvf9ok","content":"<p>Hexo的文章内容默认是不支持emoji表情的,作为一个有逼格的码农,怎么少得了丰富的表情语言,今天就研究了下怎么支持emoji。</p>\n<a id=\"more\"></a>\n<p>Hexo默认的markdown编译插件是<a href=\"https://github.com/hexojs/hexo-renderer-marked\" target=\"_blank\" rel=\"external\">hexo-renderer-marked</a>,看了一下相关文档,好像没办法支持emoji,还好在Hexo的<a href=\"https://hexo.io/plugins/\" target=\"_blank\" rel=\"external\">plugins</a>页,我们找到了另外一个markdown插件<a href=\"https://github.com/celsomiranda/hexo-renderer-markdown-it\" target=\"_blank\" rel=\"external\">hexo-renderer-markdown-it</a>,而且号称速度比默认的还要快,最主要的是,在<a href=\"https://github.com/markdown-it/markdown-it\" target=\"_blank\" rel=\"external\">markdown-it</a>的文档里面,我们发现它可以通过plugins的方式支持<a href=\"https://github.com/markdown-it/markdown-it-emoji\" target=\"_blank\" rel=\"external\">emoji</a>。</p>\n<p>下面我们就来替换markdown插件</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /ppxu/blog/</div><div class=\"line\">$ npm un hexo-renderer-marked --save</div><div class=\"line\">$ npm i hexo-renderer-markdown-it --save</div></pre></td></tr></table></figure>\n<p>不过此时的hexo-renderer-markdown-it还是用不了emoji的,我们需要加上emoji的plugin</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> node_modules/hexo-renderer-markdown-it/</div><div class=\"line\">$ npm install markdown-it-emoji --save</div></pre></td></tr></table></figure>\n<p>然后编辑Hexo的配置文件<code>_config.yml</code></p>\n<figure class=\"highlight yaml\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div><div class=\"line\">15</div><div class=\"line\">16</div><div class=\"line\">17</div><div class=\"line\">18</div><div class=\"line\">19</div><div class=\"line\">20</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"attr\">markdown:</span></div><div class=\"line\"><span class=\"attr\"> render:</span></div><div class=\"line\"><span class=\"attr\"> html:</span> <span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\"> xhtmlOut:</span> <span class=\"literal\">false</span></div><div class=\"line\"><span class=\"attr\"> breaks:</span> <span class=\"literal\">false</span></div><div class=\"line\"><span class=\"attr\"> linkify:</span> <span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\"> typographer:</span> <span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\"> quotes:</span> <span class=\"string\">'“”‘’'</span></div><div class=\"line\"><span class=\"attr\"> plugins:</span></div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-footnote</div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-sup</div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-sub</div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-abbr</div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-emoji</div><div class=\"line\"><span class=\"attr\"> anchors:</span></div><div class=\"line\"><span class=\"attr\"> level:</span> <span class=\"number\">2</span></div><div class=\"line\"><span class=\"attr\"> collisionSuffix:</span> <span class=\"string\">'v'</span></div><div class=\"line\"><span class=\"attr\"> permalink:</span> <span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\"> permalinkClass:</span> header-anchor</div><div class=\"line\"><span class=\"attr\"> permalinkSymbol:</span> ¶</div></pre></td></tr></table></figure>\n<p>关键就是在plugins里加上<code>- markdown-it-emoji</code>,其他的配置说明可以参见<a href=\"https://github.com/celsomiranda/hexo-renderer-markdown-it/wiki/Advanced-Configuration\" target=\"_blank\" rel=\"external\">wiki</a>。</p>\n<p>重启Hexo服务,即可生效,这里输入<code>:smile: :smirk: :relieved:</code>,效果 😄 😏 😌</p>\n<p>当然直接输入表情也是可以的 😂</p>\n","excerpt":"<p>Hexo的文章内容默认是不支持emoji表情的,作为一个有逼格的码农,怎么少得了丰富的表情语言,今天就研究了下怎么支持emoji。</p>","more":"<p>Hexo默认的markdown编译插件是<a href=\"https://github.com/hexojs/hexo-renderer-marked\">hexo-renderer-marked</a>,看了一下相关文档,好像没办法支持emoji,还好在Hexo的<a href=\"https://hexo.io/plugins/\">plugins</a>页,我们找到了另外一个markdown插件<a href=\"https://github.com/celsomiranda/hexo-renderer-markdown-it\">hexo-renderer-markdown-it</a>,而且号称速度比默认的还要快,最主要的是,在<a href=\"https://github.com/markdown-it/markdown-it\">markdown-it</a>的文档里面,我们发现它可以通过plugins的方式支持<a href=\"https://github.com/markdown-it/markdown-it-emoji\">emoji</a>。</p>\n<p>下面我们就来替换markdown插件</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /ppxu/blog/</div><div class=\"line\">$ npm un hexo-renderer-marked --save</div><div class=\"line\">$ npm i hexo-renderer-markdown-it --save</div></pre></td></tr></table></figure>\n<p>不过此时的hexo-renderer-markdown-it还是用不了emoji的,我们需要加上emoji的plugin</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> node_modules/hexo-renderer-markdown-it/</div><div class=\"line\">$ npm install markdown-it-emoji --save</div></pre></td></tr></table></figure>\n<p>然后编辑Hexo的配置文件<code>_config.yml</code></p>\n<figure class=\"highlight yaml\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div><div class=\"line\">15</div><div class=\"line\">16</div><div class=\"line\">17</div><div class=\"line\">18</div><div class=\"line\">19</div><div class=\"line\">20</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"attr\">markdown:</span></div><div class=\"line\"><span class=\"attr\"> render:</span></div><div class=\"line\"><span class=\"attr\"> html:</span> <span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\"> xhtmlOut:</span> <span class=\"literal\">false</span></div><div class=\"line\"><span class=\"attr\"> breaks:</span> <span class=\"literal\">false</span></div><div class=\"line\"><span class=\"attr\"> linkify:</span> <span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\"> typographer:</span> <span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\"> quotes:</span> <span class=\"string\">'“”‘’'</span></div><div class=\"line\"><span class=\"attr\"> plugins:</span></div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-footnote</div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-sup</div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-sub</div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-abbr</div><div class=\"line\"><span class=\"bullet\"> -</span> markdown-it-emoji</div><div class=\"line\"><span class=\"attr\"> anchors:</span></div><div class=\"line\"><span class=\"attr\"> level:</span> <span class=\"number\">2</span></div><div class=\"line\"><span class=\"attr\"> collisionSuffix:</span> <span class=\"string\">'v'</span></div><div class=\"line\"><span class=\"attr\"> permalink:</span> <span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\"> permalinkClass:</span> header-anchor</div><div class=\"line\"><span class=\"attr\"> permalinkSymbol:</span> ¶</div></pre></td></tr></table></figure>\n<p>关键就是在plugins里加上<code>- markdown-it-emoji</code>,其他的配置说明可以参见<a href=\"https://github.com/celsomiranda/hexo-renderer-markdown-it/wiki/Advanced-Configuration\">wiki</a>。</p>\n<p>重启Hexo服务,即可生效,这里输入<code>:smile: :smirk: :relieved:</code>,效果 😄 😏 😌</p>\n<p>当然直接输入表情也是可以的 😂</p>"},{"title":"Hello World","_content":"Welcome to [Hexo](http://hexo.io/)! This is your very first post. Check [documentation](http://hexo.io/docs/) for more info. If you get any problems when using Hexo, you can find the answer in [troubleshooting](http://hexo.io/docs/troubleshooting.html) or you can ask me on [GitHub](https://github.com/hexojs/hexo/issues).\n\n## Quick Start\n\n### Create a new post\n\n``` bash\n$ hexo new \"My New Post\"\n```\n\nMore info: [Writing](http://hexo.io/docs/writing.html)\n\n### Run server\n\n``` bash\n$ hexo server\n```\n\nMore info: [Server](http://hexo.io/docs/server.html)\n\n### Generate static files\n\n``` bash\n$ hexo generate\n```\n\nMore info: [Generating](http://hexo.io/docs/generating.html)\n\n### Deploy to remote sites\n\n``` bash\n$ hexo deploy\n```\n\nMore info: [Deployment](http://hexo.io/docs/deployment.html)\n","source":"_posts/hello-world.md","raw":"title: Hello World\n---\nWelcome to [Hexo](http://hexo.io/)! This is your very first post. Check [documentation](http://hexo.io/docs/) for more info. If you get any problems when using Hexo, you can find the answer in [troubleshooting](http://hexo.io/docs/troubleshooting.html) or you can ask me on [GitHub](https://github.com/hexojs/hexo/issues).\n\n## Quick Start\n\n### Create a new post\n\n``` bash\n$ hexo new \"My New Post\"\n```\n\nMore info: [Writing](http://hexo.io/docs/writing.html)\n\n### Run server\n\n``` bash\n$ hexo server\n```\n\nMore info: [Server](http://hexo.io/docs/server.html)\n\n### Generate static files\n\n``` bash\n$ hexo generate\n```\n\nMore info: [Generating](http://hexo.io/docs/generating.html)\n\n### Deploy to remote sites\n\n``` bash\n$ hexo deploy\n```\n\nMore info: [Deployment](http://hexo.io/docs/deployment.html)\n","slug":"hello-world","published":1,"date":"2015-12-18T14:31:17.000Z","updated":"2015-12-18T14:31:17.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7sio90006a3xxki6j0txi","content":"<p>Welcome to <a href=\"http://hexo.io/\" target=\"_blank\" rel=\"external\">Hexo</a>! This is your very first post. Check <a href=\"http://hexo.io/docs/\" target=\"_blank\" rel=\"external\">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href=\"http://hexo.io/docs/troubleshooting.html\" target=\"_blank\" rel=\"external\">troubleshooting</a> or you can ask me on <a href=\"https://github.com/hexojs/hexo/issues\" target=\"_blank\" rel=\"external\">GitHub</a>.</p>\n<h2 id=\"quick-start\"><a class=\"header-anchor\" href=\"#quick-start\">¶</a>Quick Start</h2>\n<h3 id=\"create-a-new-post\"><a class=\"header-anchor\" href=\"#create-a-new-post\">¶</a>Create a new post</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo new <span class=\"string\">\"My New Post\"</span></div></pre></td></tr></table></figure>\n<p>More info: <a href=\"http://hexo.io/docs/writing.html\" target=\"_blank\" rel=\"external\">Writing</a></p>\n<h3 id=\"run-server\"><a class=\"header-anchor\" href=\"#run-server\">¶</a>Run server</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo server</div></pre></td></tr></table></figure>\n<p>More info: <a href=\"http://hexo.io/docs/server.html\" target=\"_blank\" rel=\"external\">Server</a></p>\n<h3 id=\"generate-static-files\"><a class=\"header-anchor\" href=\"#generate-static-files\">¶</a>Generate static files</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo generate</div></pre></td></tr></table></figure>\n<p>More info: <a href=\"http://hexo.io/docs/generating.html\" target=\"_blank\" rel=\"external\">Generating</a></p>\n<h3 id=\"deploy-to-remote-sites\"><a class=\"header-anchor\" href=\"#deploy-to-remote-sites\">¶</a>Deploy to remote sites</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo deploy</div></pre></td></tr></table></figure>\n<p>More info: <a href=\"http://hexo.io/docs/deployment.html\" target=\"_blank\" rel=\"external\">Deployment</a></p>\n","excerpt":"","more":"<p>Welcome to <a href=\"http://hexo.io/\">Hexo</a>! This is your very first post. Check <a href=\"http://hexo.io/docs/\">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href=\"http://hexo.io/docs/troubleshooting.html\">troubleshooting</a> or you can ask me on <a href=\"https://github.com/hexojs/hexo/issues\">GitHub</a>.</p>\n<h2 id=\"quick-start\"><a class=\"header-anchor\" href=\"#quick-start\">¶</a>Quick Start</h2>\n<h3 id=\"create-a-new-post\"><a class=\"header-anchor\" href=\"#create-a-new-post\">¶</a>Create a new post</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo new <span class=\"string\">\"My New Post\"</span></div></pre></td></tr></table></figure>\n<p>More info: <a href=\"http://hexo.io/docs/writing.html\">Writing</a></p>\n<h3 id=\"run-server\"><a class=\"header-anchor\" href=\"#run-server\">¶</a>Run server</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo server</div></pre></td></tr></table></figure>\n<p>More info: <a href=\"http://hexo.io/docs/server.html\">Server</a></p>\n<h3 id=\"generate-static-files\"><a class=\"header-anchor\" href=\"#generate-static-files\">¶</a>Generate static files</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo generate</div></pre></td></tr></table></figure>\n<p>More info: <a href=\"http://hexo.io/docs/generating.html\">Generating</a></p>\n<h3 id=\"deploy-to-remote-sites\"><a class=\"header-anchor\" href=\"#deploy-to-remote-sites\">¶</a>Deploy to remote sites</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo deploy</div></pre></td></tr></table></figure>\n<p>More info: <a href=\"http://hexo.io/docs/deployment.html\">Deployment</a></p>\n"},{"title":"阿里云ECS搭建Hexo博客","date":"2015-12-19T02:31:27.000Z","_content":"最近刚撸了个阿里云ECS服务器来折腾,先搭个Hexo博客耍耍,这里记录一下操作步骤。\n\n<!--more-->\n\n撸主选的是最便宜的阿里云ECS,应付日常小撸妥妥的,具体配置如下:\n\n```\nCPU:1核\n内存:1024MB\n操作系统:CentOS 7.2 64位\n带宽:1Mbps\n```\n\n下面是具体的步骤:\n\n* 连接服务器\n\n``` bash\n$ ssh root@xx.xx.xx.xx\n```\n\n* 安装Nodejs环境\n\n * 更新软件源\n\n ``` bash\n $ yum -y update\n ```\n\n * 下载Node.js\n\n ``` bash\n $ cd /usr/local/src\n $ wget http://nodejs.org/dist/node-latest.tar.gz\n ```\n\n * 解压\n\n ``` bash\n $ tar zxf node-latest.tar.gz\n $ cd node-v*.*.*\n ```\n\n * 编译安装\n\n ``` bash\n $ ./configure\n $ make && make install\n ```\n\n * 确认安装成功\n\n ``` bash\n $ node -v\n $ npm -v\n ```\n\n* 安装Hexo\n\n``` bash\n$ npm install -g hexo-cli\n$ hexo init blog\n$ cd blog\n$ npm install\n```\n\n* 启动Hexo\n\n``` bash\n$ hexo server //普通启动\n$ hexo server & //静默启动\n```\n\n启动成功后就可以通过服务器的ip地址`xx.xx.xx.xx:4000`访问到页面了,然后把4000转到80上,通常做法是用Nginx做反向代理,这里先用iptables防火墙简单做一下转发处理。\n\n* 转到80端口\n\n编辑iptables文件\n\n``` bash\n$ vi /etc/sysconfig/iptables\n```\n\n加上下面这段\n\n```\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 4000 -j ACCEPT\n\n*nat\n-A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 4000\nCOMMIT\n```\n\n重启iptables服务\n\n``` bash\n$ service iptables restart\n```\n\n这时发现报错了\n\n```\nFailed to restart iptables.service: Unit iptables.service failed to load: No such file or directory.\n```\n\n查了一下原来是CentOS 7中的防火墙改成了firewalld,这里先换回iptables。\n\n``` bash\n$ systemctl stop firewalld\n$ systemctl mask firewalld\n$ yum install iptables-services\n$ systemctl enable iptables\n$ service start iptables\n```\n\n这样就可以通过ip地址`xx.xx.xx.xx`直接访问网站了。\n\n* 域名解析\n\n再撸个域名`ppxu.me`,把`@`和`www`都解析到服务器ip地址就可以了。\n\n* git管理\n\n再配置一下git环境,以后就可以通过git来管理内容了。\n\n#### 参考资料\n\n* [https://hexo.io/zh-cn/docs/index.html](https://hexo.io/zh-cn/docs/index.html)\n* [http://wsgzao.github.io/post/hexo-guide/](http://wsgzao.github.io/post/hexo-guide/)\n* [http://zipperary.com/categories/hexo/](http://zipperary.com/categories/hexo/)\n* [http://www.jianshu.com/p/73779eacb494](http://www.jianshu.com/p/73779eacb494)\n* [http://www.tuijiankan.com/2015/05/04/阿里云Centos6安装配置Nodejs、Nginx、Hexo操作记录/](http://www.tuijiankan.com/2015/05/04/%E9%98%BF%E9%87%8C%E4%BA%91Centos6%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AENodejs%E3%80%81Nginx%E3%80%81Hexo%E6%93%8D%E4%BD%9C%E8%AE%B0%E5%BD%95/)\n* [http://codybonney.com/installing-node-js-0-10-24-on-centos-6-4/](http://codybonney.com/installing-node-js-0-10-24-on-centos-6-4/)\n* [http://codybonney.com/redirect-port-80-to-another-port-using-iptables-on-centos/](http://codybonney.com/redirect-port-80-to-another-port-using-iptables-on-centos/)\n* [http://www.vkilo.com/rhel-7-iptables-service.html](http://www.vkilo.com/rhel-7-iptables-service.html)\n","source":"_posts/install-nodejs-and-hexo-in-aliyun-centos.md","raw":"title: 阿里云ECS搭建Hexo博客\ndate: 2015-12-19 10:31:27\ncategories: blog\ntags: [aliyun, ecs, centos, nodejs, hexo, blog]\n---\n最近刚撸了个阿里云ECS服务器来折腾,先搭个Hexo博客耍耍,这里记录一下操作步骤。\n\n<!--more-->\n\n撸主选的是最便宜的阿里云ECS,应付日常小撸妥妥的,具体配置如下:\n\n```\nCPU:1核\n内存:1024MB\n操作系统:CentOS 7.2 64位\n带宽:1Mbps\n```\n\n下面是具体的步骤:\n\n* 连接服务器\n\n``` bash\n$ ssh root@xx.xx.xx.xx\n```\n\n* 安装Nodejs环境\n\n * 更新软件源\n\n ``` bash\n $ yum -y update\n ```\n\n * 下载Node.js\n\n ``` bash\n $ cd /usr/local/src\n $ wget http://nodejs.org/dist/node-latest.tar.gz\n ```\n\n * 解压\n\n ``` bash\n $ tar zxf node-latest.tar.gz\n $ cd node-v*.*.*\n ```\n\n * 编译安装\n\n ``` bash\n $ ./configure\n $ make && make install\n ```\n\n * 确认安装成功\n\n ``` bash\n $ node -v\n $ npm -v\n ```\n\n* 安装Hexo\n\n``` bash\n$ npm install -g hexo-cli\n$ hexo init blog\n$ cd blog\n$ npm install\n```\n\n* 启动Hexo\n\n``` bash\n$ hexo server //普通启动\n$ hexo server & //静默启动\n```\n\n启动成功后就可以通过服务器的ip地址`xx.xx.xx.xx:4000`访问到页面了,然后把4000转到80上,通常做法是用Nginx做反向代理,这里先用iptables防火墙简单做一下转发处理。\n\n* 转到80端口\n\n编辑iptables文件\n\n``` bash\n$ vi /etc/sysconfig/iptables\n```\n\n加上下面这段\n\n```\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 4000 -j ACCEPT\n\n*nat\n-A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 4000\nCOMMIT\n```\n\n重启iptables服务\n\n``` bash\n$ service iptables restart\n```\n\n这时发现报错了\n\n```\nFailed to restart iptables.service: Unit iptables.service failed to load: No such file or directory.\n```\n\n查了一下原来是CentOS 7中的防火墙改成了firewalld,这里先换回iptables。\n\n``` bash\n$ systemctl stop firewalld\n$ systemctl mask firewalld\n$ yum install iptables-services\n$ systemctl enable iptables\n$ service start iptables\n```\n\n这样就可以通过ip地址`xx.xx.xx.xx`直接访问网站了。\n\n* 域名解析\n\n再撸个域名`ppxu.me`,把`@`和`www`都解析到服务器ip地址就可以了。\n\n* git管理\n\n再配置一下git环境,以后就可以通过git来管理内容了。\n\n#### 参考资料\n\n* [https://hexo.io/zh-cn/docs/index.html](https://hexo.io/zh-cn/docs/index.html)\n* [http://wsgzao.github.io/post/hexo-guide/](http://wsgzao.github.io/post/hexo-guide/)\n* [http://zipperary.com/categories/hexo/](http://zipperary.com/categories/hexo/)\n* [http://www.jianshu.com/p/73779eacb494](http://www.jianshu.com/p/73779eacb494)\n* [http://www.tuijiankan.com/2015/05/04/阿里云Centos6安装配置Nodejs、Nginx、Hexo操作记录/](http://www.tuijiankan.com/2015/05/04/%E9%98%BF%E9%87%8C%E4%BA%91Centos6%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AENodejs%E3%80%81Nginx%E3%80%81Hexo%E6%93%8D%E4%BD%9C%E8%AE%B0%E5%BD%95/)\n* [http://codybonney.com/installing-node-js-0-10-24-on-centos-6-4/](http://codybonney.com/installing-node-js-0-10-24-on-centos-6-4/)\n* [http://codybonney.com/redirect-port-80-to-another-port-using-iptables-on-centos/](http://codybonney.com/redirect-port-80-to-another-port-using-iptables-on-centos/)\n* [http://www.vkilo.com/rhel-7-iptables-service.html](http://www.vkilo.com/rhel-7-iptables-service.html)\n","slug":"install-nodejs-and-hexo-in-aliyun-centos","published":1,"updated":"2015-12-26T13:54:53.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7siod0009a3xx8rely9t2","content":"<p>最近刚撸了个阿里云ECS服务器来折腾,先搭个Hexo博客耍耍,这里记录一下操作步骤。</p>\n<a id=\"more\"></a>\n<p>撸主选的是最便宜的阿里云ECS,应付日常小撸妥妥的,具体配置如下:</p>\n<figure class=\"highlight lsl\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div></pre></td><td class=\"code\"><pre><div class=\"line\">CPU:<span class=\"number\">1</span>核</div><div class=\"line\">内存:<span class=\"number\">1024</span>MB</div><div class=\"line\">操作系统:CentOS <span class=\"number\">7.2</span> <span class=\"number\">64</span>位</div><div class=\"line\">带宽:<span class=\"number\">1</span>Mbps</div></pre></td></tr></table></figure>\n<p>下面是具体的步骤:</p>\n<ul>\n<li>连接服务器</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ssh root@xx.xx.xx.xx</div></pre></td></tr></table></figure>\n<ul>\n<li>\n<p>安装Nodejs环境</p>\n<ul>\n<li>更新软件源</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ yum -y update</div></pre></td></tr></table></figure>\n<ul>\n<li>下载Node.js</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /usr/<span class=\"built_in\">local</span>/src</div><div class=\"line\">$ wget http://nodejs.org/dist/node-latest.tar.gz</div></pre></td></tr></table></figure>\n<ul>\n<li>解压</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ tar zxf node-latest.tar.gz</div><div class=\"line\">$ <span class=\"built_in\">cd</span> node-v*.*.*</div></pre></td></tr></table></figure>\n<ul>\n<li>编译安装</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ./configure</div><div class=\"line\">$ make && make install</div></pre></td></tr></table></figure>\n<ul>\n<li>确认安装成功</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ node -v</div><div class=\"line\">$ npm -v</div></pre></td></tr></table></figure>\n</li>\n<li>\n<p>安装Hexo</p>\n</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ npm install -g hexo-cli</div><div class=\"line\">$ hexo init blog</div><div class=\"line\">$ <span class=\"built_in\">cd</span> blog</div><div class=\"line\">$ npm install</div></pre></td></tr></table></figure>\n<ul>\n<li>启动Hexo</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo server //普通启动</div><div class=\"line\">$ hexo server & //静默启动</div></pre></td></tr></table></figure>\n<p>启动成功后就可以通过服务器的ip地址<code>xx.xx.xx.xx:4000</code>访问到页面了,然后把4000转到80上,通常做法是用Nginx做反向代理,这里先用iptables防火墙简单做一下转发处理。</p>\n<ul>\n<li>转到80端口</li>\n</ul>\n<p>编辑iptables文件</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ vi /etc/sysconfig/iptables</div></pre></td></tr></table></figure>\n<p>加上下面这段</p>\n<figure class=\"highlight pf\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\">-A INPUT -m <span class=\"keyword\">state</span> --state NEW -m tcp -p tcp --dport <span class=\"number\">80</span> -j ACCEPT</div><div class=\"line\">-A INPUT -m <span class=\"keyword\">state</span> --state NEW -m tcp -p tcp --dport <span class=\"number\">4000</span> -j ACCEPT</div><div class=\"line\"></div><div class=\"line\">*nat</div><div class=\"line\">-A PREROUTING -p tcp --dport <span class=\"number\">80</span> -j REDIRECT --to-ports <span class=\"number\">4000</span></div><div class=\"line\">COMMIT</div></pre></td></tr></table></figure>\n<p>重启iptables服务</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ service iptables restart</div></pre></td></tr></table></figure>\n<p>这时发现报错了</p>\n<figure class=\"highlight livecodeserver\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">Failed <span class=\"built_in\">to</span> restart iptables.service: Unit iptables.service failed <span class=\"built_in\">to</span> <span class=\"built_in\">load</span>: No such <span class=\"built_in\">file</span> <span class=\"keyword\">or</span> <span class=\"built_in\">directory</span>.</div></pre></td></tr></table></figure>\n<p>查了一下原来是CentOS 7中的防火墙改成了firewalld,这里先换回iptables。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ systemctl stop firewalld</div><div class=\"line\">$ systemctl mask firewalld</div><div class=\"line\">$ yum install iptables-services</div><div class=\"line\">$ systemctl <span class=\"built_in\">enable</span> iptables</div><div class=\"line\">$ service start iptables</div></pre></td></tr></table></figure>\n<p>这样就可以通过ip地址<code>xx.xx.xx.xx</code>直接访问网站了。</p>\n<ul>\n<li>域名解析</li>\n</ul>\n<p>再撸个域名<code>ppxu.me</code>,把<code>@</code>和<code>www</code>都解析到服务器ip地址就可以了。</p>\n<ul>\n<li>git管理</li>\n</ul>\n<p>再配置一下git环境,以后就可以通过git来管理内容了。</p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li><a href=\"https://hexo.io/zh-cn/docs/index.html\" target=\"_blank\" rel=\"external\">https://hexo.io/zh-cn/docs/index.html</a></li>\n<li><a href=\"http://wsgzao.github.io/post/hexo-guide/\" target=\"_blank\" rel=\"external\">http://wsgzao.github.io/post/hexo-guide/</a></li>\n<li><a href=\"http://zipperary.com/categories/hexo/\" target=\"_blank\" rel=\"external\">http://zipperary.com/categories/hexo/</a></li>\n<li><a href=\"http://www.jianshu.com/p/73779eacb494\" target=\"_blank\" rel=\"external\">http://www.jianshu.com/p/73779eacb494</a></li>\n<li><a href=\"http://www.tuijiankan.com/2015/05/04/%E9%98%BF%E9%87%8C%E4%BA%91Centos6%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AENodejs%E3%80%81Nginx%E3%80%81Hexo%E6%93%8D%E4%BD%9C%E8%AE%B0%E5%BD%95/\" target=\"_blank\" rel=\"external\">http://www.tuijiankan.com/2015/05/04/阿里云Centos6安装配置Nodejs、Nginx、Hexo操作记录/</a></li>\n<li><a href=\"http://codybonney.com/installing-node-js-0-10-24-on-centos-6-4/\" target=\"_blank\" rel=\"external\">http://codybonney.com/installing-node-js-0-10-24-on-centos-6-4/</a></li>\n<li><a href=\"http://codybonney.com/redirect-port-80-to-another-port-using-iptables-on-centos/\" target=\"_blank\" rel=\"external\">http://codybonney.com/redirect-port-80-to-another-port-using-iptables-on-centos/</a></li>\n<li><a href=\"http://www.vkilo.com/rhel-7-iptables-service.html\" target=\"_blank\" rel=\"external\">http://www.vkilo.com/rhel-7-iptables-service.html</a></li>\n</ul>\n","excerpt":"<p>最近刚撸了个阿里云ECS服务器来折腾,先搭个Hexo博客耍耍,这里记录一下操作步骤。</p>","more":"<p>撸主选的是最便宜的阿里云ECS,应付日常小撸妥妥的,具体配置如下:</p>\n<figure class=\"highlight lsl\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div></pre></td><td class=\"code\"><pre><div class=\"line\">CPU:<span class=\"number\">1</span>核</div><div class=\"line\">内存:<span class=\"number\">1024</span>MB</div><div class=\"line\">操作系统:CentOS <span class=\"number\">7.2</span> <span class=\"number\">64</span>位</div><div class=\"line\">带宽:<span class=\"number\">1</span>Mbps</div></pre></td></tr></table></figure>\n<p>下面是具体的步骤:</p>\n<ul>\n<li>连接服务器</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ssh root@xx.xx.xx.xx</div></pre></td></tr></table></figure>\n<ul>\n<li>\n<p>安装Nodejs环境</p>\n<ul>\n<li>更新软件源</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ yum -y update</div></pre></td></tr></table></figure>\n<ul>\n<li>下载Node.js</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /usr/<span class=\"built_in\">local</span>/src</div><div class=\"line\">$ wget http://nodejs.org/dist/node-latest.tar.gz</div></pre></td></tr></table></figure>\n<ul>\n<li>解压</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ tar zxf node-latest.tar.gz</div><div class=\"line\">$ <span class=\"built_in\">cd</span> node-v*.*.*</div></pre></td></tr></table></figure>\n<ul>\n<li>编译安装</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ./configure</div><div class=\"line\">$ make && make install</div></pre></td></tr></table></figure>\n<ul>\n<li>确认安装成功</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ node -v</div><div class=\"line\">$ npm -v</div></pre></td></tr></table></figure>\n</li>\n<li>\n<p>安装Hexo</p>\n</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ npm install -g hexo-cli</div><div class=\"line\">$ hexo init blog</div><div class=\"line\">$ <span class=\"built_in\">cd</span> blog</div><div class=\"line\">$ npm install</div></pre></td></tr></table></figure>\n<ul>\n<li>启动Hexo</li>\n</ul>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ hexo server //普通启动</div><div class=\"line\">$ hexo server & //静默启动</div></pre></td></tr></table></figure>\n<p>启动成功后就可以通过服务器的ip地址<code>xx.xx.xx.xx:4000</code>访问到页面了,然后把4000转到80上,通常做法是用Nginx做反向代理,这里先用iptables防火墙简单做一下转发处理。</p>\n<ul>\n<li>转到80端口</li>\n</ul>\n<p>编辑iptables文件</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ vi /etc/sysconfig/iptables</div></pre></td></tr></table></figure>\n<p>加上下面这段</p>\n<figure class=\"highlight pf\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\">-A INPUT -m <span class=\"keyword\">state</span> --state NEW -m tcp -p tcp --dport <span class=\"number\">80</span> -j ACCEPT</div><div class=\"line\">-A INPUT -m <span class=\"keyword\">state</span> --state NEW -m tcp -p tcp --dport <span class=\"number\">4000</span> -j ACCEPT</div><div class=\"line\"></div><div class=\"line\">*nat</div><div class=\"line\">-A PREROUTING -p tcp --dport <span class=\"number\">80</span> -j REDIRECT --to-ports <span class=\"number\">4000</span></div><div class=\"line\">COMMIT</div></pre></td></tr></table></figure>\n<p>重启iptables服务</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ service iptables restart</div></pre></td></tr></table></figure>\n<p>这时发现报错了</p>\n<figure class=\"highlight livecodeserver\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">Failed <span class=\"built_in\">to</span> restart iptables.service: Unit iptables.service failed <span class=\"built_in\">to</span> <span class=\"built_in\">load</span>: No such <span class=\"built_in\">file</span> <span class=\"keyword\">or</span> <span class=\"built_in\">directory</span>.</div></pre></td></tr></table></figure>\n<p>查了一下原来是CentOS 7中的防火墙改成了firewalld,这里先换回iptables。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ systemctl stop firewalld</div><div class=\"line\">$ systemctl mask firewalld</div><div class=\"line\">$ yum install iptables-services</div><div class=\"line\">$ systemctl <span class=\"built_in\">enable</span> iptables</div><div class=\"line\">$ service start iptables</div></pre></td></tr></table></figure>\n<p>这样就可以通过ip地址<code>xx.xx.xx.xx</code>直接访问网站了。</p>\n<ul>\n<li>域名解析</li>\n</ul>\n<p>再撸个域名<code>ppxu.me</code>,把<code>@</code>和<code>www</code>都解析到服务器ip地址就可以了。</p>\n<ul>\n<li>git管理</li>\n</ul>\n<p>再配置一下git环境,以后就可以通过git来管理内容了。</p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li><a href=\"https://hexo.io/zh-cn/docs/index.html\">https://hexo.io/zh-cn/docs/index.html</a></li>\n<li><a href=\"http://wsgzao.github.io/post/hexo-guide/\">http://wsgzao.github.io/post/hexo-guide/</a></li>\n<li><a href=\"http://zipperary.com/categories/hexo/\">http://zipperary.com/categories/hexo/</a></li>\n<li><a href=\"http://www.jianshu.com/p/73779eacb494\">http://www.jianshu.com/p/73779eacb494</a></li>\n<li><a href=\"http://www.tuijiankan.com/2015/05/04/%E9%98%BF%E9%87%8C%E4%BA%91Centos6%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AENodejs%E3%80%81Nginx%E3%80%81Hexo%E6%93%8D%E4%BD%9C%E8%AE%B0%E5%BD%95/\">http://www.tuijiankan.com/2015/05/04/阿里云Centos6安装配置Nodejs、Nginx、Hexo操作记录/</a></li>\n<li><a href=\"http://codybonney.com/installing-node-js-0-10-24-on-centos-6-4/\">http://codybonney.com/installing-node-js-0-10-24-on-centos-6-4/</a></li>\n<li><a href=\"http://codybonney.com/redirect-port-80-to-another-port-using-iptables-on-centos/\">http://codybonney.com/redirect-port-80-to-another-port-using-iptables-on-centos/</a></li>\n<li><a href=\"http://www.vkilo.com/rhel-7-iptables-service.html\">http://www.vkilo.com/rhel-7-iptables-service.html</a></li>\n</ul>"},{"title":"在阿里云服务器上安装ShadowSocks服务","date":"2016-03-29T06:55:04.000Z","_content":"记录一下如何在服务器上安装科学上网工具ShadowSocks,因为没有国外的主机,只好拿阿里云试试手。\n\n<!--more-->\n\n### 安装shadowsocks\n\n``` bash\n$ yum install epel-release\n$ yum update\n$ yum install python-setuptools\n$ easy_install pip\n$ pip install shadowsocks\n```\n\n### 配置shadowsocks\n\n``` bash\n$ vi /etc/shadowsocks.json\n```\n\n输入以下内容\n\n``` json\n{\n \"server\": \"0.0.0.0\",\n \"server_port\": 端口号,\n \"local_address\": \"127.0.0.1\",\n \"local_port\": 1080,\n \"password\":\" 你的密码\",\n \"timeout\": 500,\n \"method\":\" aes-256-cfb\",\n \"fast_open\": true\n}\n```\n\n### 启动服务\n\n``` bash\n$ yum install python-setuptools supervisor\n$ easy_install pip\n$ pip install shadowsocks\n```\n\n加入自动启动\n\n``` bash\n$ vi /etc/supervisord.conf\n```\n\n在最后输入\n\n```\n[program:shadowsocks]\ncommand=ssserver -c /etc/shadowsocks.json\nautostart=true\nautorestart=true\nuser=root\n```\n\n刷新\n\n``` bash\n$ sudo chkconfig --add supervisord\n$ sudo chkconfig supervisord on\n$ service supervisord start\n$ supervisorctl reload\n```\n\n最后,重启一下服务器即可,更多的优化手段可以参考下面链接。\n\n#### 参考\n\n* [http://www.linexy.net/archives/digitalocean-build-shadowsocks-services-and-optimization-program/](http://www.linexy.net/archives/digitalocean-build-shadowsocks-services-and-optimization-program/)\n","source":"_posts/install-shadowsocks-service.md","raw":"title: 在阿里云服务器上安装ShadowSocks服务\ndate: 2016-03-29 14:55:04\ncategories: blog\ntags: [aliyun, ecs, centos, shadowsocks]\n---\n记录一下如何在服务器上安装科学上网工具ShadowSocks,因为没有国外的主机,只好拿阿里云试试手。\n\n<!--more-->\n\n### 安装shadowsocks\n\n``` bash\n$ yum install epel-release\n$ yum update\n$ yum install python-setuptools\n$ easy_install pip\n$ pip install shadowsocks\n```\n\n### 配置shadowsocks\n\n``` bash\n$ vi /etc/shadowsocks.json\n```\n\n输入以下内容\n\n``` json\n{\n \"server\": \"0.0.0.0\",\n \"server_port\": 端口号,\n \"local_address\": \"127.0.0.1\",\n \"local_port\": 1080,\n \"password\":\" 你的密码\",\n \"timeout\": 500,\n \"method\":\" aes-256-cfb\",\n \"fast_open\": true\n}\n```\n\n### 启动服务\n\n``` bash\n$ yum install python-setuptools supervisor\n$ easy_install pip\n$ pip install shadowsocks\n```\n\n加入自动启动\n\n``` bash\n$ vi /etc/supervisord.conf\n```\n\n在最后输入\n\n```\n[program:shadowsocks]\ncommand=ssserver -c /etc/shadowsocks.json\nautostart=true\nautorestart=true\nuser=root\n```\n\n刷新\n\n``` bash\n$ sudo chkconfig --add supervisord\n$ sudo chkconfig supervisord on\n$ service supervisord start\n$ supervisorctl reload\n```\n\n最后,重启一下服务器即可,更多的优化手段可以参考下面链接。\n\n#### 参考\n\n* [http://www.linexy.net/archives/digitalocean-build-shadowsocks-services-and-optimization-program/](http://www.linexy.net/archives/digitalocean-build-shadowsocks-services-and-optimization-program/)\n","slug":"install-shadowsocks-service","published":1,"updated":"2016-03-29T13:40:47.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7sioh000ba3xxnhokh5p0","content":"<p>记录一下如何在服务器上安装科学上网工具ShadowSocks,因为没有国外的主机,只好拿阿里云试试手。</p>\n<a id=\"more\"></a>\n<h3 id=\"安装shadowsocks\"><a class=\"header-anchor\" href=\"#安装shadowsocks\">¶</a>安装shadowsocks</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ yum install epel-release</div><div class=\"line\">$ yum update</div><div class=\"line\">$ yum install python-setuptools</div><div class=\"line\">$ easy_install pip</div><div class=\"line\">$ pip install shadowsocks</div></pre></td></tr></table></figure>\n<h3 id=\"配置shadowsocks\"><a class=\"header-anchor\" href=\"#配置shadowsocks\">¶</a>配置shadowsocks</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ vi /etc/shadowsocks.json</div></pre></td></tr></table></figure>\n<p>输入以下内容</p>\n<figure class=\"highlight json\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div></pre></td><td class=\"code\"><pre><div class=\"line\">{</div><div class=\"line\"> <span class=\"attr\">\"server\"</span>: <span class=\"string\">\"0.0.0.0\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"server_port\"</span>: 端口号,</div><div class=\"line\"> <span class=\"attr\">\"local_address\"</span>: <span class=\"string\">\"127.0.0.1\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"local_port\"</span>: <span class=\"number\">1080</span>,</div><div class=\"line\"> <span class=\"attr\">\"password\"</span>:<span class=\"string\">\" 你的密码\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"timeout\"</span>: <span class=\"number\">500</span>,</div><div class=\"line\"> <span class=\"attr\">\"method\"</span>:<span class=\"string\">\" aes-256-cfb\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"fast_open\"</span>: <span class=\"literal\">true</span></div><div class=\"line\">}</div></pre></td></tr></table></figure>\n<h3 id=\"启动服务\"><a class=\"header-anchor\" href=\"#启动服务\">¶</a>启动服务</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ yum install python-setuptools supervisor</div><div class=\"line\">$ easy_install pip</div><div class=\"line\">$ pip install shadowsocks</div></pre></td></tr></table></figure>\n<p>加入自动启动</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ vi /etc/supervisord.conf</div></pre></td></tr></table></figure>\n<p>在最后输入</p>\n<figure class=\"highlight ini\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"section\">[program:shadowsocks]</span></div><div class=\"line\"><span class=\"attr\">command</span>=ssserver -c /etc/shadowsocks.json</div><div class=\"line\"><span class=\"attr\">autostart</span>=<span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\">autorestart</span>=<span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\">user</span>=root</div></pre></td></tr></table></figure>\n<p>刷新</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ sudo chkconfig --add supervisord</div><div class=\"line\">$ sudo chkconfig supervisord on</div><div class=\"line\">$ service supervisord start</div><div class=\"line\">$ supervisorctl reload</div></pre></td></tr></table></figure>\n<p>最后,重启一下服务器即可,更多的优化手段可以参考下面链接。</p>\n<h4 id=\"参考\"><a class=\"header-anchor\" href=\"#参考\">¶</a>参考</h4>\n<ul>\n<li><a href=\"http://www.linexy.net/archives/digitalocean-build-shadowsocks-services-and-optimization-program/\" target=\"_blank\" rel=\"external\">http://www.linexy.net/archives/digitalocean-build-shadowsocks-services-and-optimization-program/</a></li>\n</ul>\n","excerpt":"<p>记录一下如何在服务器上安装科学上网工具ShadowSocks,因为没有国外的主机,只好拿阿里云试试手。</p>","more":"<h3 id=\"安装shadowsocks\"><a class=\"header-anchor\" href=\"#安装shadowsocks\">¶</a>安装shadowsocks</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ yum install epel-release</div><div class=\"line\">$ yum update</div><div class=\"line\">$ yum install python-setuptools</div><div class=\"line\">$ easy_install pip</div><div class=\"line\">$ pip install shadowsocks</div></pre></td></tr></table></figure>\n<h3 id=\"配置shadowsocks\"><a class=\"header-anchor\" href=\"#配置shadowsocks\">¶</a>配置shadowsocks</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ vi /etc/shadowsocks.json</div></pre></td></tr></table></figure>\n<p>输入以下内容</p>\n<figure class=\"highlight json\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div></pre></td><td class=\"code\"><pre><div class=\"line\">{</div><div class=\"line\"> <span class=\"attr\">\"server\"</span>: <span class=\"string\">\"0.0.0.0\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"server_port\"</span>: 端口号,</div><div class=\"line\"> <span class=\"attr\">\"local_address\"</span>: <span class=\"string\">\"127.0.0.1\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"local_port\"</span>: <span class=\"number\">1080</span>,</div><div class=\"line\"> <span class=\"attr\">\"password\"</span>:<span class=\"string\">\" 你的密码\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"timeout\"</span>: <span class=\"number\">500</span>,</div><div class=\"line\"> <span class=\"attr\">\"method\"</span>:<span class=\"string\">\" aes-256-cfb\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"fast_open\"</span>: <span class=\"literal\">true</span></div><div class=\"line\">}</div></pre></td></tr></table></figure>\n<h3 id=\"启动服务\"><a class=\"header-anchor\" href=\"#启动服务\">¶</a>启动服务</h3>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ yum install python-setuptools supervisor</div><div class=\"line\">$ easy_install pip</div><div class=\"line\">$ pip install shadowsocks</div></pre></td></tr></table></figure>\n<p>加入自动启动</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ vi /etc/supervisord.conf</div></pre></td></tr></table></figure>\n<p>在最后输入</p>\n<figure class=\"highlight ini\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"section\">[program:shadowsocks]</span></div><div class=\"line\"><span class=\"attr\">command</span>=ssserver -c /etc/shadowsocks.json</div><div class=\"line\"><span class=\"attr\">autostart</span>=<span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\">autorestart</span>=<span class=\"literal\">true</span></div><div class=\"line\"><span class=\"attr\">user</span>=root</div></pre></td></tr></table></figure>\n<p>刷新</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ sudo chkconfig --add supervisord</div><div class=\"line\">$ sudo chkconfig supervisord on</div><div class=\"line\">$ service supervisord start</div><div class=\"line\">$ supervisorctl reload</div></pre></td></tr></table></figure>\n<p>最后,重启一下服务器即可,更多的优化手段可以参考下面链接。</p>\n<h4 id=\"参考\"><a class=\"header-anchor\" href=\"#参考\">¶</a>参考</h4>\n<ul>\n<li><a href=\"http://www.linexy.net/archives/digitalocean-build-shadowsocks-services-and-optimization-program/\">http://www.linexy.net/archives/digitalocean-build-shadowsocks-services-and-optimization-program/</a></li>\n</ul>"},{"title":"Electron快速上手(译)","date":"2016-01-31T13:20:40.000Z","_content":"Electron可以让你使用纯JavaScript创建桌面应用程序,它提供了一个包含丰富的原生(操作系统)API的运行时。你可以把它看作是一个Node.js运行时的变体,只不过它是专注于桌面应用程序而不是web服务器。\n\n这并不是说Electron是一个操作图形用户界面(GUI)库的JavaScript工具,实际上,Electron使用网页作为它的GUI,因此你也可以把它看作是一个精简版的Chromium浏览器,通过JavaScript来控制。\n\n<!--more-->\n\n### 主进程\n\n在Electron中,运行`package.json`中的`main`脚本的进程叫作主进程(the main process)。主进程中运行的脚本可以通过创建网页来显示一个GUI界面。\n\n### 渲染进程\n\n由于Electron使用Chromium来显示网页,Chromium的多线程架构也用到了。Electron中的每一个网页都在一个独立的进程中运行,即渲染进程(the renderer process)。\n\n在普通的浏览器中,网页通常是在一个沙箱环境中运行,不允许访问本地资源。不过使用Electron就可以在网页中利用Node.js的API来实现一些初级的操作系统交互。\n\n### 主进程和渲染进程的区别\n\n主进程通过创建`BrowserWindow`实例来生成网页,每个`BrowserWindow`实例在它自己的渲染进程中运行网页。当一个`BrowserWindow`实例销毁时,对应的渲染进程也被终止。\n\n主进程管理所有的网页和它们相对应的渲染进程,每个渲染进程是独立的,只须关心在它上面运行的网页。\n\n在网页中调用原生的GUI相关API是不允许的,因为在网页中管理原生GUI资源是十分危险的,很容易泄露资源。如果你想在网页中操作GUI,该网页的渲染进程必须要和主进程通信,请求在主进程上进行这些操作。\n\n在Electron中,我们提供了[ipc](http://electron.atom.io/docs/v0.36.5/api/ipc-renderer)模块用来实现主进程和渲染进程之间的通信。另外还有一个[remote](http://electron.atom.io/docs/v0.36.5/api/remote)模块用来做RPC类型的通信。\n\n## 编写你的第一个Electron应用\n\n通常一个Electron应用的结构类似这样:\n\n```\nyour-app/\n├── package.json\n├── main.js\n└── index.html\n```\n\n`package.json`的格式和Node模块完全一样,`main`字段指定的脚本就是你的应用的启动脚本,它会运行主进程。一个`package.json`可能类似这样:\n\n``` json\n{\n \"name\" : \"your-app\",\n \"version\" : \"0.1.0\",\n \"main\" : \"main.js\"\n}\n```\n\n__注意__:如果`package.json`中没有指定`main`字段,Electron会尝试加载`index.js`。\n\n`main.js`需要创建窗口并且处理系统事件,一个典型的例子如下:\n\n``` javascript\n'use strict';\n\nconst electron = require('electron');\nconst app = electron.app; // 控制应用生命周期的模块.\nconst BrowserWindow = electron.BrowserWindow; // 创建原生浏览器窗口的模块.\n\n// 保存窗口对象的一个全局引用,如果你不这么做,当JavaScript对象被垃圾回收后窗口会自动关闭\nvar mainWindow = null;\n\n// 当所有窗口关闭后退出应用\napp.on('window-all-closed', function() {\n // 在OSX通常会让应用和它的菜单栏保持激活直到用户使用Cmd+Q强制退出\n if (process.platform != 'darwin') {\n app.quit();\n }\n});\n\n// 这个方法会在Electron完成初始化并准备好创建浏览器窗口时调用\napp.on('ready', function() {\n // 创建浏览器窗口\n mainWindow = new BrowserWindow({width: 800, height: 600});\n\n // 加载应用的index.html\n mainWindow.loadURL('file://' + __dirname + '/index.html');\n\n // 打开开发者工具(chromium的控制台)\n mainWindow.webContents.openDevTools();\n\n // 当窗口关闭时触发\n mainWindow.on('closed', function() {\n // 取消对窗口对象的引用,通常如果你的应用支持多窗口,你会将窗口存在一个数组里,这时候你应当删除对应的元素\n mainWindow = null;\n });\n});\n```\n\n最后这个`index.html`就是你想要展现的网页:\n\n``` html\n<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"UTF-8\">\n <title>Hello World!</title>\n </head>\n <body>\n <h1>Hello World!</h1>\n We are using node <script>document.write(process.versions.node)</script>,\n Chrome <script>document.write(process.versions.chrome)</script>,\n and Electron <script>document.write(process.versions.electron)</script>.\n </body>\n</html>\n```\n\n## 运行你的应用\n\n当你创建完最初的`main.js`,`index.html`和`package.json`文件,你肯定迫不及待想要在本地尝试跑一下你的应用,看看它是不是像预期那样的运行。\n\n### electron-prebuilt\n\n如果你通过`npm`全局安装了`electron-prebuilt`,你只需要在你的应用目录下运行:\n\n``` javascript\nelectron .\n```\n\n如果你是局部安装的,运行:\n\n``` javascript\n./node_modules/.bin/electron .\n```\n\n### 手动下载的Electron程序\n\n如果你是手动下载的Electron,你也可以直接运行你的应用。\n\n#### Windows\n\n``` bash\n$ .\\electron\\electron.exe your-app\\\n```\n\n#### Linux\n``` bash\n$ ./electron/electron your-app/\n```\n\n#### OSX\n``` bash\n$ ./Electron.app/Contents/MacOS/Electron your-app/\n```\n\n这里的`Electron.app`是Electron发行包中的一部分,你可以在[这里](https://github.com/atom/electron/releases)下载。\n\n### 发布应用\n\n当你完成了应用的开发,你可以根据[应用发布](http://electron.atom.io/docs/v0.36.5/tutorial/application-distribution)指南来创建一个发布,并执行打包程序。\n\n### 尝试这个例子\n\n在[atom/electron-quick-start](https://github.com/atom/electron-quick-start)下载并运行这个教程的代码。\n\n__注意__:运行代码需要系统支持[Git](https://git-scm.com/)和[Node.js](https://nodejs.org/en/download/)(含[npm](https://npmjs.org/))。\n\n``` bash\n# Clone the repository\n$ git clone https://github.com/atom/electron-quick-start\n# Go into the repository\n$ cd electron-quick-start\n# Install dependencies and run the app\n$ npm install && npm start\n```\n\n__原文地址__:[http://electron.atom.io/docs/latest/tutorial/quick-start/](http://electron.atom.io/docs/latest/tutorial/quick-start/)\n","source":"_posts/introduce-to-electron.md","raw":"title: Electron快速上手(译)\ndate: 2016-01-31 21:20:40\ncategories: nodejs\ntags: [electron, atom, nodejs, chromium, app]\n---\nElectron可以让你使用纯JavaScript创建桌面应用程序,它提供了一个包含丰富的原生(操作系统)API的运行时。你可以把它看作是一个Node.js运行时的变体,只不过它是专注于桌面应用程序而不是web服务器。\n\n这并不是说Electron是一个操作图形用户界面(GUI)库的JavaScript工具,实际上,Electron使用网页作为它的GUI,因此你也可以把它看作是一个精简版的Chromium浏览器,通过JavaScript来控制。\n\n<!--more-->\n\n### 主进程\n\n在Electron中,运行`package.json`中的`main`脚本的进程叫作主进程(the main process)。主进程中运行的脚本可以通过创建网页来显示一个GUI界面。\n\n### 渲染进程\n\n由于Electron使用Chromium来显示网页,Chromium的多线程架构也用到了。Electron中的每一个网页都在一个独立的进程中运行,即渲染进程(the renderer process)。\n\n在普通的浏览器中,网页通常是在一个沙箱环境中运行,不允许访问本地资源。不过使用Electron就可以在网页中利用Node.js的API来实现一些初级的操作系统交互。\n\n### 主进程和渲染进程的区别\n\n主进程通过创建`BrowserWindow`实例来生成网页,每个`BrowserWindow`实例在它自己的渲染进程中运行网页。当一个`BrowserWindow`实例销毁时,对应的渲染进程也被终止。\n\n主进程管理所有的网页和它们相对应的渲染进程,每个渲染进程是独立的,只须关心在它上面运行的网页。\n\n在网页中调用原生的GUI相关API是不允许的,因为在网页中管理原生GUI资源是十分危险的,很容易泄露资源。如果你想在网页中操作GUI,该网页的渲染进程必须要和主进程通信,请求在主进程上进行这些操作。\n\n在Electron中,我们提供了[ipc](http://electron.atom.io/docs/v0.36.5/api/ipc-renderer)模块用来实现主进程和渲染进程之间的通信。另外还有一个[remote](http://electron.atom.io/docs/v0.36.5/api/remote)模块用来做RPC类型的通信。\n\n## 编写你的第一个Electron应用\n\n通常一个Electron应用的结构类似这样:\n\n```\nyour-app/\n├── package.json\n├── main.js\n└── index.html\n```\n\n`package.json`的格式和Node模块完全一样,`main`字段指定的脚本就是你的应用的启动脚本,它会运行主进程。一个`package.json`可能类似这样:\n\n``` json\n{\n \"name\" : \"your-app\",\n \"version\" : \"0.1.0\",\n \"main\" : \"main.js\"\n}\n```\n\n__注意__:如果`package.json`中没有指定`main`字段,Electron会尝试加载`index.js`。\n\n`main.js`需要创建窗口并且处理系统事件,一个典型的例子如下:\n\n``` javascript\n'use strict';\n\nconst electron = require('electron');\nconst app = electron.app; // 控制应用生命周期的模块.\nconst BrowserWindow = electron.BrowserWindow; // 创建原生浏览器窗口的模块.\n\n// 保存窗口对象的一个全局引用,如果你不这么做,当JavaScript对象被垃圾回收后窗口会自动关闭\nvar mainWindow = null;\n\n// 当所有窗口关闭后退出应用\napp.on('window-all-closed', function() {\n // 在OSX通常会让应用和它的菜单栏保持激活直到用户使用Cmd+Q强制退出\n if (process.platform != 'darwin') {\n app.quit();\n }\n});\n\n// 这个方法会在Electron完成初始化并准备好创建浏览器窗口时调用\napp.on('ready', function() {\n // 创建浏览器窗口\n mainWindow = new BrowserWindow({width: 800, height: 600});\n\n // 加载应用的index.html\n mainWindow.loadURL('file://' + __dirname + '/index.html');\n\n // 打开开发者工具(chromium的控制台)\n mainWindow.webContents.openDevTools();\n\n // 当窗口关闭时触发\n mainWindow.on('closed', function() {\n // 取消对窗口对象的引用,通常如果你的应用支持多窗口,你会将窗口存在一个数组里,这时候你应当删除对应的元素\n mainWindow = null;\n });\n});\n```\n\n最后这个`index.html`就是你想要展现的网页:\n\n``` html\n<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"UTF-8\">\n <title>Hello World!</title>\n </head>\n <body>\n <h1>Hello World!</h1>\n We are using node <script>document.write(process.versions.node)</script>,\n Chrome <script>document.write(process.versions.chrome)</script>,\n and Electron <script>document.write(process.versions.electron)</script>.\n </body>\n</html>\n```\n\n## 运行你的应用\n\n当你创建完最初的`main.js`,`index.html`和`package.json`文件,你肯定迫不及待想要在本地尝试跑一下你的应用,看看它是不是像预期那样的运行。\n\n### electron-prebuilt\n\n如果你通过`npm`全局安装了`electron-prebuilt`,你只需要在你的应用目录下运行:\n\n``` javascript\nelectron .\n```\n\n如果你是局部安装的,运行:\n\n``` javascript\n./node_modules/.bin/electron .\n```\n\n### 手动下载的Electron程序\n\n如果你是手动下载的Electron,你也可以直接运行你的应用。\n\n#### Windows\n\n``` bash\n$ .\\electron\\electron.exe your-app\\\n```\n\n#### Linux\n``` bash\n$ ./electron/electron your-app/\n```\n\n#### OSX\n``` bash\n$ ./Electron.app/Contents/MacOS/Electron your-app/\n```\n\n这里的`Electron.app`是Electron发行包中的一部分,你可以在[这里](https://github.com/atom/electron/releases)下载。\n\n### 发布应用\n\n当你完成了应用的开发,你可以根据[应用发布](http://electron.atom.io/docs/v0.36.5/tutorial/application-distribution)指南来创建一个发布,并执行打包程序。\n\n### 尝试这个例子\n\n在[atom/electron-quick-start](https://github.com/atom/electron-quick-start)下载并运行这个教程的代码。\n\n__注意__:运行代码需要系统支持[Git](https://git-scm.com/)和[Node.js](https://nodejs.org/en/download/)(含[npm](https://npmjs.org/))。\n\n``` bash\n# Clone the repository\n$ git clone https://github.com/atom/electron-quick-start\n# Go into the repository\n$ cd electron-quick-start\n# Install dependencies and run the app\n$ npm install && npm start\n```\n\n__原文地址__:[http://electron.atom.io/docs/latest/tutorial/quick-start/](http://electron.atom.io/docs/latest/tutorial/quick-start/)\n","slug":"introduce-to-electron","published":1,"updated":"2016-02-01T15:18:54.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7siol000fa3xxjat2hs9w","content":"<p>Electron可以让你使用纯JavaScript创建桌面应用程序,它提供了一个包含丰富的原生(操作系统)API的运行时。你可以把它看作是一个Node.js运行时的变体,只不过它是专注于桌面应用程序而不是web服务器。</p>\n<p>这并不是说Electron是一个操作图形用户界面(GUI)库的JavaScript工具,实际上,Electron使用网页作为它的GUI,因此你也可以把它看作是一个精简版的Chromium浏览器,通过JavaScript来控制。</p>\n<a id=\"more\"></a>\n<h3 id=\"主进程\"><a class=\"header-anchor\" href=\"#主进程\">¶</a>主进程</h3>\n<p>在Electron中,运行<code>package.json</code>中的<code>main</code>脚本的进程叫作主进程(the main process)。主进程中运行的脚本可以通过创建网页来显示一个GUI界面。</p>\n<h3 id=\"渲染进程\"><a class=\"header-anchor\" href=\"#渲染进程\">¶</a>渲染进程</h3>\n<p>由于Electron使用Chromium来显示网页,Chromium的多线程架构也用到了。Electron中的每一个网页都在一个独立的进程中运行,即渲染进程(the renderer process)。</p>\n<p>在普通的浏览器中,网页通常是在一个沙箱环境中运行,不允许访问本地资源。不过使用Electron就可以在网页中利用Node.js的API来实现一些初级的操作系统交互。</p>\n<h3 id=\"主进程和渲染进程的区别\"><a class=\"header-anchor\" href=\"#主进程和渲染进程的区别\">¶</a>主进程和渲染进程的区别</h3>\n<p>主进程通过创建<code>BrowserWindow</code>实例来生成网页,每个<code>BrowserWindow</code>实例在它自己的渲染进程中运行网页。当一个<code>BrowserWindow</code>实例销毁时,对应的渲染进程也被终止。</p>\n<p>主进程管理所有的网页和它们相对应的渲染进程,每个渲染进程是独立的,只须关心在它上面运行的网页。</p>\n<p>在网页中调用原生的GUI相关API是不允许的,因为在网页中管理原生GUI资源是十分危险的,很容易泄露资源。如果你想在网页中操作GUI,该网页的渲染进程必须要和主进程通信,请求在主进程上进行这些操作。</p>\n<p>在Electron中,我们提供了<a href=\"http://electron.atom.io/docs/v0.36.5/api/ipc-renderer\" target=\"_blank\" rel=\"external\">ipc</a>模块用来实现主进程和渲染进程之间的通信。另外还有一个<a href=\"http://electron.atom.io/docs/v0.36.5/api/remote\" target=\"_blank\" rel=\"external\">remote</a>模块用来做RPC类型的通信。</p>\n<h2 id=\"编写你的第一个electron应用\"><a class=\"header-anchor\" href=\"#编写你的第一个electron应用\">¶</a>编写你的第一个Electron应用</h2>\n<p>通常一个Electron应用的结构类似这样:</p>\n<figure class=\"highlight stylus\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div></pre></td><td class=\"code\"><pre><div class=\"line\">your-app/</div><div class=\"line\">├── package<span class=\"selector-class\">.json</span></div><div class=\"line\">├── main<span class=\"selector-class\">.js</span></div><div class=\"line\">└── index.html</div></pre></td></tr></table></figure>\n<p><code>package.json</code>的格式和Node模块完全一样,<code>main</code>字段指定的脚本就是你的应用的启动脚本,它会运行主进程。一个<code>package.json</code>可能类似这样:</p>\n<figure class=\"highlight json\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">{</div><div class=\"line\"> <span class=\"attr\">\"name\"</span> : <span class=\"string\">\"your-app\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"version\"</span> : <span class=\"string\">\"0.1.0\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"main\"</span> : <span class=\"string\">\"main.js\"</span></div><div class=\"line\">}</div></pre></td></tr></table></figure>\n<p><strong>注意</strong>:如果<code>package.json</code>中没有指定<code>main</code>字段,Electron会尝试加载<code>index.js</code>。</p>\n<p><code>main.js</code>需要创建窗口并且处理系统事件,一个典型的例子如下:</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div><div class=\"line\">15</div><div class=\"line\">16</div><div class=\"line\">17</div><div class=\"line\">18</div><div class=\"line\">19</div><div class=\"line\">20</div><div class=\"line\">21</div><div class=\"line\">22</div><div class=\"line\">23</div><div class=\"line\">24</div><div class=\"line\">25</div><div class=\"line\">26</div><div class=\"line\">27</div><div class=\"line\">28</div><div class=\"line\">29</div><div class=\"line\">30</div><div class=\"line\">31</div><div class=\"line\">32</div><div class=\"line\">33</div><div class=\"line\">34</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"meta\">'use strict'</span>;</div><div class=\"line\"></div><div class=\"line\"><span class=\"keyword\">const</span> electron = <span class=\"built_in\">require</span>(<span class=\"string\">'electron'</span>);</div><div class=\"line\"><span class=\"keyword\">const</span> app = electron.app; <span class=\"comment\">// 控制应用生命周期的模块.</span></div><div class=\"line\"><span class=\"keyword\">const</span> BrowserWindow = electron.BrowserWindow; <span class=\"comment\">// 创建原生浏览器窗口的模块.</span></div><div class=\"line\"></div><div class=\"line\"><span class=\"comment\">// 保存窗口对象的一个全局引用,如果你不这么做,当JavaScript对象被垃圾回收后窗口会自动关闭</span></div><div class=\"line\"><span class=\"keyword\">var</span> mainWindow = <span class=\"literal\">null</span>;</div><div class=\"line\"></div><div class=\"line\"><span class=\"comment\">// 当所有窗口关闭后退出应用</span></div><div class=\"line\">app.on(<span class=\"string\">'window-all-closed'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</div><div class=\"line\"> <span class=\"comment\">// 在OSX通常会让应用和它的菜单栏保持激活直到用户使用Cmd+Q强制退出</span></div><div class=\"line\"> <span class=\"keyword\">if</span> (process.platform != <span class=\"string\">'darwin'</span>) {</div><div class=\"line\"> app.quit();</div><div class=\"line\"> }</div><div class=\"line\">});</div><div class=\"line\"></div><div class=\"line\"><span class=\"comment\">// 这个方法会在Electron完成初始化并准备好创建浏览器窗口时调用</span></div><div class=\"line\">app.on(<span class=\"string\">'ready'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</div><div class=\"line\"> <span class=\"comment\">// 创建浏览器窗口</span></div><div class=\"line\"> mainWindow = <span class=\"keyword\">new</span> BrowserWindow({<span class=\"attr\">width</span>: <span class=\"number\">800</span>, <span class=\"attr\">height</span>: <span class=\"number\">600</span>});</div><div class=\"line\"></div><div class=\"line\"> <span class=\"comment\">// 加载应用的index.html</span></div><div class=\"line\"> mainWindow.loadURL(<span class=\"string\">'file://'</span> + __dirname + <span class=\"string\">'/index.html'</span>);</div><div class=\"line\"></div><div class=\"line\"> <span class=\"comment\">// 打开开发者工具(chromium的控制台)</span></div><div class=\"line\"> mainWindow.webContents.openDevTools();</div><div class=\"line\"></div><div class=\"line\"> <span class=\"comment\">// 当窗口关闭时触发</span></div><div class=\"line\"> mainWindow.on(<span class=\"string\">'closed'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</div><div class=\"line\"> <span class=\"comment\">// 取消对窗口对象的引用,通常如果你的应用支持多窗口,你会将窗口存在一个数组里,这时候你应当删除对应的元素</span></div><div class=\"line\"> mainWindow = <span class=\"literal\">null</span>;</div><div class=\"line\"> });</div><div class=\"line\">});</div></pre></td></tr></table></figure>\n<p>最后这个<code>index.html</code>就是你想要展现的网页:</p>\n<figure class=\"highlight html\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"meta\"><!DOCTYPE html></span></div><div class=\"line\"><span class=\"tag\"><<span class=\"name\">html</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">head</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">meta</span> <span class=\"attr\">charset</span>=<span class=\"string\">\"UTF-8\"</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">title</span>></span>Hello World!<span class=\"tag\"></<span class=\"name\">title</span>></span></div><div class=\"line\"> <span class=\"tag\"></<span class=\"name\">head</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">body</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">h1</span>></span>Hello World!<span class=\"tag\"></<span class=\"name\">h1</span>></span></div><div class=\"line\"> We are using node <span class=\"tag\"><<span class=\"name\">script</span>></span><span class=\"javascript\"><span class=\"built_in\">document</span>.write(process.versions.node)</span><span class=\"tag\"></<span class=\"name\">script</span>></span>,</div><div class=\"line\"> Chrome <span class=\"tag\"><<span class=\"name\">script</span>></span><span class=\"javascript\"><span class=\"built_in\">document</span>.write(process.versions.chrome)</span><span class=\"tag\"></<span class=\"name\">script</span>></span>,</div><div class=\"line\"> and Electron <span class=\"tag\"><<span class=\"name\">script</span>></span><span class=\"javascript\"><span class=\"built_in\">document</span>.write(process.versions.electron)</span><span class=\"tag\"></<span class=\"name\">script</span>></span>.</div><div class=\"line\"> <span class=\"tag\"></<span class=\"name\">body</span>></span></div><div class=\"line\"><span class=\"tag\"></<span class=\"name\">html</span>></span></div></pre></td></tr></table></figure>\n<h2 id=\"运行你的应用\"><a class=\"header-anchor\" href=\"#运行你的应用\">¶</a>运行你的应用</h2>\n<p>当你创建完最初的<code>main.js</code>,<code>index.html</code>和<code>package.json</code>文件,你肯定迫不及待想要在本地尝试跑一下你的应用,看看它是不是像预期那样的运行。</p>\n<h3 id=\"electron-prebuilt\"><a class=\"header-anchor\" href=\"#electron-prebuilt\">¶</a>electron-prebuilt</h3>\n<p>如果你通过<code>npm</code>全局安装了<code>electron-prebuilt</code>,你只需要在你的应用目录下运行:</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">electron .</div></pre></td></tr></table></figure>\n<p>如果你是局部安装的,运行:</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">./node_modules/.bin/electron .</div></pre></td></tr></table></figure>\n<h3 id=\"手动下载的electron程序\"><a class=\"header-anchor\" href=\"#手动下载的electron程序\">¶</a>手动下载的Electron程序</h3>\n<p>如果你是手动下载的Electron,你也可以直接运行你的应用。</p>\n<h4 id=\"windows\"><a class=\"header-anchor\" href=\"#windows\">¶</a>Windows</h4>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ .\\electron\\electron.exe your-app\\</div></pre></td></tr></table></figure>\n<h4 id=\"linux\"><a class=\"header-anchor\" href=\"#linux\">¶</a>Linux</h4>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ./electron/electron your-app/</div></pre></td></tr></table></figure>\n<h4 id=\"osx\"><a class=\"header-anchor\" href=\"#osx\">¶</a>OSX</h4>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ./Electron.app/Contents/MacOS/Electron your-app/</div></pre></td></tr></table></figure>\n<p>这里的<code>Electron.app</code>是Electron发行包中的一部分,你可以在<a href=\"https://github.com/atom/electron/releases\" target=\"_blank\" rel=\"external\">这里</a>下载。</p>\n<h3 id=\"发布应用\"><a class=\"header-anchor\" href=\"#发布应用\">¶</a>发布应用</h3>\n<p>当你完成了应用的开发,你可以根据<a href=\"http://electron.atom.io/docs/v0.36.5/tutorial/application-distribution\" target=\"_blank\" rel=\"external\">应用发布</a>指南来创建一个发布,并执行打包程序。</p>\n<h3 id=\"尝试这个例子\"><a class=\"header-anchor\" href=\"#尝试这个例子\">¶</a>尝试这个例子</h3>\n<p>在<a href=\"https://github.com/atom/electron-quick-start\" target=\"_blank\" rel=\"external\">atom/electron-quick-start</a>下载并运行这个教程的代码。</p>\n<p><strong>注意</strong>:运行代码需要系统支持<a href=\"https://git-scm.com/\" target=\"_blank\" rel=\"external\">Git</a>和<a href=\"https://nodejs.org/en/download/\" target=\"_blank\" rel=\"external\">Node.js</a>(含<a href=\"https://npmjs.org/\" target=\"_blank\" rel=\"external\">npm</a>)。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"comment\"># Clone the repository</span></div><div class=\"line\">$ git <span class=\"built_in\">clone</span> https://github.com/atom/electron-quick-start</div><div class=\"line\"><span class=\"comment\"># Go into the repository</span></div><div class=\"line\">$ <span class=\"built_in\">cd</span> electron-quick-start</div><div class=\"line\"><span class=\"comment\"># Install dependencies and run the app</span></div><div class=\"line\">$ npm install && npm start</div></pre></td></tr></table></figure>\n<p><strong>原文地址</strong>:<a href=\"http://electron.atom.io/docs/latest/tutorial/quick-start/\" target=\"_blank\" rel=\"external\">http://electron.atom.io/docs/latest/tutorial/quick-start/</a></p>\n","excerpt":"<p>Electron可以让你使用纯JavaScript创建桌面应用程序,它提供了一个包含丰富的原生(操作系统)API的运行时。你可以把它看作是一个Node.js运行时的变体,只不过它是专注于桌面应用程序而不是web服务器。</p>\n<p>这并不是说Electron是一个操作图形用户界面(GUI)库的JavaScript工具,实际上,Electron使用网页作为它的GUI,因此你也可以把它看作是一个精简版的Chromium浏览器,通过JavaScript来控制。</p>","more":"<h3 id=\"主进程\"><a class=\"header-anchor\" href=\"#主进程\">¶</a>主进程</h3>\n<p>在Electron中,运行<code>package.json</code>中的<code>main</code>脚本的进程叫作主进程(the main process)。主进程中运行的脚本可以通过创建网页来显示一个GUI界面。</p>\n<h3 id=\"渲染进程\"><a class=\"header-anchor\" href=\"#渲染进程\">¶</a>渲染进程</h3>\n<p>由于Electron使用Chromium来显示网页,Chromium的多线程架构也用到了。Electron中的每一个网页都在一个独立的进程中运行,即渲染进程(the renderer process)。</p>\n<p>在普通的浏览器中,网页通常是在一个沙箱环境中运行,不允许访问本地资源。不过使用Electron就可以在网页中利用Node.js的API来实现一些初级的操作系统交互。</p>\n<h3 id=\"主进程和渲染进程的区别\"><a class=\"header-anchor\" href=\"#主进程和渲染进程的区别\">¶</a>主进程和渲染进程的区别</h3>\n<p>主进程通过创建<code>BrowserWindow</code>实例来生成网页,每个<code>BrowserWindow</code>实例在它自己的渲染进程中运行网页。当一个<code>BrowserWindow</code>实例销毁时,对应的渲染进程也被终止。</p>\n<p>主进程管理所有的网页和它们相对应的渲染进程,每个渲染进程是独立的,只须关心在它上面运行的网页。</p>\n<p>在网页中调用原生的GUI相关API是不允许的,因为在网页中管理原生GUI资源是十分危险的,很容易泄露资源。如果你想在网页中操作GUI,该网页的渲染进程必须要和主进程通信,请求在主进程上进行这些操作。</p>\n<p>在Electron中,我们提供了<a href=\"http://electron.atom.io/docs/v0.36.5/api/ipc-renderer\">ipc</a>模块用来实现主进程和渲染进程之间的通信。另外还有一个<a href=\"http://electron.atom.io/docs/v0.36.5/api/remote\">remote</a>模块用来做RPC类型的通信。</p>\n<h2 id=\"编写你的第一个electron应用\"><a class=\"header-anchor\" href=\"#编写你的第一个electron应用\">¶</a>编写你的第一个Electron应用</h2>\n<p>通常一个Electron应用的结构类似这样:</p>\n<figure class=\"highlight stylus\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div></pre></td><td class=\"code\"><pre><div class=\"line\">your-app/</div><div class=\"line\">├── package<span class=\"selector-class\">.json</span></div><div class=\"line\">├── main<span class=\"selector-class\">.js</span></div><div class=\"line\">└── index.html</div></pre></td></tr></table></figure>\n<p><code>package.json</code>的格式和Node模块完全一样,<code>main</code>字段指定的脚本就是你的应用的启动脚本,它会运行主进程。一个<code>package.json</code>可能类似这样:</p>\n<figure class=\"highlight json\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">{</div><div class=\"line\"> <span class=\"attr\">\"name\"</span> : <span class=\"string\">\"your-app\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"version\"</span> : <span class=\"string\">\"0.1.0\"</span>,</div><div class=\"line\"> <span class=\"attr\">\"main\"</span> : <span class=\"string\">\"main.js\"</span></div><div class=\"line\">}</div></pre></td></tr></table></figure>\n<p><strong>注意</strong>:如果<code>package.json</code>中没有指定<code>main</code>字段,Electron会尝试加载<code>index.js</code>。</p>\n<p><code>main.js</code>需要创建窗口并且处理系统事件,一个典型的例子如下:</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div><div class=\"line\">15</div><div class=\"line\">16</div><div class=\"line\">17</div><div class=\"line\">18</div><div class=\"line\">19</div><div class=\"line\">20</div><div class=\"line\">21</div><div class=\"line\">22</div><div class=\"line\">23</div><div class=\"line\">24</div><div class=\"line\">25</div><div class=\"line\">26</div><div class=\"line\">27</div><div class=\"line\">28</div><div class=\"line\">29</div><div class=\"line\">30</div><div class=\"line\">31</div><div class=\"line\">32</div><div class=\"line\">33</div><div class=\"line\">34</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"meta\">'use strict'</span>;</div><div class=\"line\"></div><div class=\"line\"><span class=\"keyword\">const</span> electron = <span class=\"built_in\">require</span>(<span class=\"string\">'electron'</span>);</div><div class=\"line\"><span class=\"keyword\">const</span> app = electron.app; <span class=\"comment\">// 控制应用生命周期的模块.</span></div><div class=\"line\"><span class=\"keyword\">const</span> BrowserWindow = electron.BrowserWindow; <span class=\"comment\">// 创建原生浏览器窗口的模块.</span></div><div class=\"line\"></div><div class=\"line\"><span class=\"comment\">// 保存窗口对象的一个全局引用,如果你不这么做,当JavaScript对象被垃圾回收后窗口会自动关闭</span></div><div class=\"line\"><span class=\"keyword\">var</span> mainWindow = <span class=\"literal\">null</span>;</div><div class=\"line\"></div><div class=\"line\"><span class=\"comment\">// 当所有窗口关闭后退出应用</span></div><div class=\"line\">app.on(<span class=\"string\">'window-all-closed'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</div><div class=\"line\"> <span class=\"comment\">// 在OSX通常会让应用和它的菜单栏保持激活直到用户使用Cmd+Q强制退出</span></div><div class=\"line\"> <span class=\"keyword\">if</span> (process.platform != <span class=\"string\">'darwin'</span>) {</div><div class=\"line\"> app.quit();</div><div class=\"line\"> }</div><div class=\"line\">});</div><div class=\"line\"></div><div class=\"line\"><span class=\"comment\">// 这个方法会在Electron完成初始化并准备好创建浏览器窗口时调用</span></div><div class=\"line\">app.on(<span class=\"string\">'ready'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</div><div class=\"line\"> <span class=\"comment\">// 创建浏览器窗口</span></div><div class=\"line\"> mainWindow = <span class=\"keyword\">new</span> BrowserWindow({<span class=\"attr\">width</span>: <span class=\"number\">800</span>, <span class=\"attr\">height</span>: <span class=\"number\">600</span>});</div><div class=\"line\"></div><div class=\"line\"> <span class=\"comment\">// 加载应用的index.html</span></div><div class=\"line\"> mainWindow.loadURL(<span class=\"string\">'file://'</span> + __dirname + <span class=\"string\">'/index.html'</span>);</div><div class=\"line\"></div><div class=\"line\"> <span class=\"comment\">// 打开开发者工具(chromium的控制台)</span></div><div class=\"line\"> mainWindow.webContents.openDevTools();</div><div class=\"line\"></div><div class=\"line\"> <span class=\"comment\">// 当窗口关闭时触发</span></div><div class=\"line\"> mainWindow.on(<span class=\"string\">'closed'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</div><div class=\"line\"> <span class=\"comment\">// 取消对窗口对象的引用,通常如果你的应用支持多窗口,你会将窗口存在一个数组里,这时候你应当删除对应的元素</span></div><div class=\"line\"> mainWindow = <span class=\"literal\">null</span>;</div><div class=\"line\"> });</div><div class=\"line\">});</div></pre></td></tr></table></figure>\n<p>最后这个<code>index.html</code>就是你想要展现的网页:</p>\n<figure class=\"highlight html\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"meta\"><!DOCTYPE html></span></div><div class=\"line\"><span class=\"tag\"><<span class=\"name\">html</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">head</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">meta</span> <span class=\"attr\">charset</span>=<span class=\"string\">\"UTF-8\"</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">title</span>></span>Hello World!<span class=\"tag\"></<span class=\"name\">title</span>></span></div><div class=\"line\"> <span class=\"tag\"></<span class=\"name\">head</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">body</span>></span></div><div class=\"line\"> <span class=\"tag\"><<span class=\"name\">h1</span>></span>Hello World!<span class=\"tag\"></<span class=\"name\">h1</span>></span></div><div class=\"line\"> We are using node <span class=\"tag\"><<span class=\"name\">script</span>></span><span class=\"javascript\"><span class=\"built_in\">document</span>.write(process.versions.node)</span><span class=\"tag\"></<span class=\"name\">script</span>></span>,</div><div class=\"line\"> Chrome <span class=\"tag\"><<span class=\"name\">script</span>></span><span class=\"javascript\"><span class=\"built_in\">document</span>.write(process.versions.chrome)</span><span class=\"tag\"></<span class=\"name\">script</span>></span>,</div><div class=\"line\"> and Electron <span class=\"tag\"><<span class=\"name\">script</span>></span><span class=\"javascript\"><span class=\"built_in\">document</span>.write(process.versions.electron)</span><span class=\"tag\"></<span class=\"name\">script</span>></span>.</div><div class=\"line\"> <span class=\"tag\"></<span class=\"name\">body</span>></span></div><div class=\"line\"><span class=\"tag\"></<span class=\"name\">html</span>></span></div></pre></td></tr></table></figure>\n<h2 id=\"运行你的应用\"><a class=\"header-anchor\" href=\"#运行你的应用\">¶</a>运行你的应用</h2>\n<p>当你创建完最初的<code>main.js</code>,<code>index.html</code>和<code>package.json</code>文件,你肯定迫不及待想要在本地尝试跑一下你的应用,看看它是不是像预期那样的运行。</p>\n<h3 id=\"electron-prebuilt\"><a class=\"header-anchor\" href=\"#electron-prebuilt\">¶</a>electron-prebuilt</h3>\n<p>如果你通过<code>npm</code>全局安装了<code>electron-prebuilt</code>,你只需要在你的应用目录下运行:</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">electron .</div></pre></td></tr></table></figure>\n<p>如果你是局部安装的,运行:</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">./node_modules/.bin/electron .</div></pre></td></tr></table></figure>\n<h3 id=\"手动下载的electron程序\"><a class=\"header-anchor\" href=\"#手动下载的electron程序\">¶</a>手动下载的Electron程序</h3>\n<p>如果你是手动下载的Electron,你也可以直接运行你的应用。</p>\n<h4 id=\"windows\"><a class=\"header-anchor\" href=\"#windows\">¶</a>Windows</h4>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ .\\electron\\electron.exe your-app\\</div></pre></td></tr></table></figure>\n<h4 id=\"linux\"><a class=\"header-anchor\" href=\"#linux\">¶</a>Linux</h4>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ./electron/electron your-app/</div></pre></td></tr></table></figure>\n<h4 id=\"osx\"><a class=\"header-anchor\" href=\"#osx\">¶</a>OSX</h4>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ ./Electron.app/Contents/MacOS/Electron your-app/</div></pre></td></tr></table></figure>\n<p>这里的<code>Electron.app</code>是Electron发行包中的一部分,你可以在<a href=\"https://github.com/atom/electron/releases\">这里</a>下载。</p>\n<h3 id=\"发布应用\"><a class=\"header-anchor\" href=\"#发布应用\">¶</a>发布应用</h3>\n<p>当你完成了应用的开发,你可以根据<a href=\"http://electron.atom.io/docs/v0.36.5/tutorial/application-distribution\">应用发布</a>指南来创建一个发布,并执行打包程序。</p>\n<h3 id=\"尝试这个例子\"><a class=\"header-anchor\" href=\"#尝试这个例子\">¶</a>尝试这个例子</h3>\n<p>在<a href=\"https://github.com/atom/electron-quick-start\">atom/electron-quick-start</a>下载并运行这个教程的代码。</p>\n<p><strong>注意</strong>:运行代码需要系统支持<a href=\"https://git-scm.com/\">Git</a>和<a href=\"https://nodejs.org/en/download/\">Node.js</a>(含<a href=\"https://npmjs.org/\">npm</a>)。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"comment\"># Clone the repository</span></div><div class=\"line\">$ git <span class=\"built_in\">clone</span> https://github.com/atom/electron-quick-start</div><div class=\"line\"><span class=\"comment\"># Go into the repository</span></div><div class=\"line\">$ <span class=\"built_in\">cd</span> electron-quick-start</div><div class=\"line\"><span class=\"comment\"># Install dependencies and run the app</span></div><div class=\"line\">$ npm install && npm start</div></pre></td></tr></table></figure>\n<p><strong>原文地址</strong>:<a href=\"http://electron.atom.io/docs/latest/tutorial/quick-start/\">http://electron.atom.io/docs/latest/tutorial/quick-start/</a></p>"},{"title":"平滑升级Nginx并配置HTTP/2","date":"2015-12-23T04:47:40.000Z","_content":"之前说到SPDY已经被HTTP/2上位了,继续用SPDY也不合适,今天就趁空升级了最新的Nginx,并开启了HTTP/2,操作过程如下\n\n<!--more-->\n\n1. 检查当前Nginx版本和配置参数\n\n ``` bash\n $ nginx -V\n nginx version: nginx/1.8.0\n built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)\n built with OpenSSL 1.0.1e-fips 11 Feb 2013\n TLS SNI support enabled\n configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-openssl=../libressl-2.2.2 --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'\n ```\n\n 记下这里的configure arguments,后面编译的时候要用的。\n\n2. 安装PCRE,Nginx的rewrite模块依赖PCRE\n\n ``` bash\n $ cd /ppxu\n $ yum -y install make zlib zlib-devel gcc-c++ libtool\n $ wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz\n $ tar zxvf pcre-8.37.tar.gz\n $ cd pcre-8.37/\n $ ./configure\n $ make && make install\n ```\n\n3. 下载OpenSSL,可以从[OpenSSL](https://www.openssl.org/)或者[LibreSSL](http://www.libressl.org/)下载\n\n ``` bash\n $ cd /ppxu\n $ wget http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.3.1.tar.gz\n $ tar zxvf libressl-2.3.1.tar.gz\n ```\n\n4. 下载,配置并编译Nginx\n\n ``` bash\n $ cd /ppxu\n $ wget http://nginx.org/download/nginx-1.9.9.tar.gz\n $ tar zxvf nginx-1.9.9.tar.gz\n $ cd nginx-1.9.9/\n $ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-openssl=../libressl-2.3.1 --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'\n $ make\n ```\n\n 其中的`--with-http_v2_module`就是开启HTTP/2的设置。\n\n5. 替换Nginx\n\n ``` bash\n $ which nginx //查找nginx路径\n $ mv /usr/sbin/nginx /usr/sbin/nginx.old //备份旧版nginx\n $ cp objs/nginx /usr/sbin/ //将编译好的新版nginx复制过去\n ```\n\n6. 确认更新生效\n\n ``` bash\n $ /usr/sbin/nginx -t\n nginx: the configuration file /etc/nginx/nginx.conf syntax is ok\n nginx: configuration file /etc/nginx/nginx.conf test is successful\n $ /usr/sbin/nginx -v\n nginx version: nginx/1.9.9\n ```\n\n7. 更新Nginx配置文件\n\n ```\n server {\n listen 80;\n listen 443 ssl http2;\n server_name ppxu.me *.ppxu.me;\n\n ...\n ```\n\n8. 重启Nginx即可\n\n访问网站,在响应头里可以看到`server:nginx/1.9.9`,同时,在[chrome://net-internals/#http2](chrome://net-internals/#http2)上面可以看到网站已经支持了HTTP/2\n\n\n\n#### 参考资料\n\n* [http://www.linuxde.net/2011/08/554.html](http://www.linuxde.net/2011/08/554.html)\n* [http://www.linuxidc.com/Linux/2014-02/96137.htm](http://www.linuxidc.com/Linux/2014-02/96137.htm)\n* [http://www.poluoluo.com/server/201403/265778.html](http://www.poluoluo.com/server/201403/265778.html)\n* [https://imququ.com/post/http2-resource.html](https://imququ.com/post/http2-resource.html)\n* [https://imququ.com/post/nginx-http2-patch.html](https://imququ.com/post/nginx-http2-patch.html)\n* [http://www.tuicool.com/articles/3eeIVfi](http://www.tuicool.com/articles/3eeIVfi)\n","source":"_posts/nginx-smooth-upgrade-and-config-http2.md","raw":"title: 平滑升级Nginx并配置HTTP/2\ndate: 2015-12-23 12:47:40\ncategories: blog\ntags: [aliyun, ecs, centos, nginx, http2]\n---\n之前说到SPDY已经被HTTP/2上位了,继续用SPDY也不合适,今天就趁空升级了最新的Nginx,并开启了HTTP/2,操作过程如下\n\n<!--more-->\n\n1. 检查当前Nginx版本和配置参数\n\n ``` bash\n $ nginx -V\n nginx version: nginx/1.8.0\n built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)\n built with OpenSSL 1.0.1e-fips 11 Feb 2013\n TLS SNI support enabled\n configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-openssl=../libressl-2.2.2 --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'\n ```\n\n 记下这里的configure arguments,后面编译的时候要用的。\n\n2. 安装PCRE,Nginx的rewrite模块依赖PCRE\n\n ``` bash\n $ cd /ppxu\n $ yum -y install make zlib zlib-devel gcc-c++ libtool\n $ wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz\n $ tar zxvf pcre-8.37.tar.gz\n $ cd pcre-8.37/\n $ ./configure\n $ make && make install\n ```\n\n3. 下载OpenSSL,可以从[OpenSSL](https://www.openssl.org/)或者[LibreSSL](http://www.libressl.org/)下载\n\n ``` bash\n $ cd /ppxu\n $ wget http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.3.1.tar.gz\n $ tar zxvf libressl-2.3.1.tar.gz\n ```\n\n4. 下载,配置并编译Nginx\n\n ``` bash\n $ cd /ppxu\n $ wget http://nginx.org/download/nginx-1.9.9.tar.gz\n $ tar zxvf nginx-1.9.9.tar.gz\n $ cd nginx-1.9.9/\n $ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-openssl=../libressl-2.3.1 --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'\n $ make\n ```\n\n 其中的`--with-http_v2_module`就是开启HTTP/2的设置。\n\n5. 替换Nginx\n\n ``` bash\n $ which nginx //查找nginx路径\n $ mv /usr/sbin/nginx /usr/sbin/nginx.old //备份旧版nginx\n $ cp objs/nginx /usr/sbin/ //将编译好的新版nginx复制过去\n ```\n\n6. 确认更新生效\n\n ``` bash\n $ /usr/sbin/nginx -t\n nginx: the configuration file /etc/nginx/nginx.conf syntax is ok\n nginx: configuration file /etc/nginx/nginx.conf test is successful\n $ /usr/sbin/nginx -v\n nginx version: nginx/1.9.9\n ```\n\n7. 更新Nginx配置文件\n\n ```\n server {\n listen 80;\n listen 443 ssl http2;\n server_name ppxu.me *.ppxu.me;\n\n ...\n ```\n\n8. 重启Nginx即可\n\n访问网站,在响应头里可以看到`server:nginx/1.9.9`,同时,在[chrome://net-internals/#http2](chrome://net-internals/#http2)上面可以看到网站已经支持了HTTP/2\n\n\n\n#### 参考资料\n\n* [http://www.linuxde.net/2011/08/554.html](http://www.linuxde.net/2011/08/554.html)\n* [http://www.linuxidc.com/Linux/2014-02/96137.htm](http://www.linuxidc.com/Linux/2014-02/96137.htm)\n* [http://www.poluoluo.com/server/201403/265778.html](http://www.poluoluo.com/server/201403/265778.html)\n* [https://imququ.com/post/http2-resource.html](https://imququ.com/post/http2-resource.html)\n* [https://imququ.com/post/nginx-http2-patch.html](https://imququ.com/post/nginx-http2-patch.html)\n* [http://www.tuicool.com/articles/3eeIVfi](http://www.tuicool.com/articles/3eeIVfi)\n","slug":"nginx-smooth-upgrade-and-config-http2","published":1,"updated":"2017-02-21T07:09:56.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7sion000ha3xxq64rdkm1","content":"<p>之前说到SPDY已经被HTTP/2上位了,继续用SPDY也不合适,今天就趁空升级了最新的Nginx,并开启了HTTP/2,操作过程如下</p>\n<a id=\"more\"></a>\n<ol>\n<li>检查当前Nginx版本和配置参数</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ nginx -V</div><div class=\"line\">nginx version: nginx/1.8.0</div><div class=\"line\">built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)</div><div class=\"line\">built with OpenSSL 1.0.1e-fips 11 Feb 2013</div><div class=\"line\">TLS SNI support enabled</div><div class=\"line\">configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/<span class=\"built_in\">log</span>/nginx/error.log --http-log-path=/var/<span class=\"built_in\">log</span>/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-openssl=../libressl-2.2.2 --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt=<span class=\"string\">'-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'</span></div></pre></td></tr></table></figure>\n<p>记下这里的configure arguments,后面编译的时候要用的。</p>\n<ol start=\"2\">\n<li>安装PCRE,Nginx的rewrite模块依赖PCRE</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /ppxu</div><div class=\"line\">$ yum -y install make zlib zlib-devel gcc-c++ libtool</div><div class=\"line\">$ wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz</div><div class=\"line\">$ tar zxvf pcre-8.37.tar.gz</div><div class=\"line\">$ <span class=\"built_in\">cd</span> pcre-8.37/</div><div class=\"line\">$ ./configure</div><div class=\"line\">$ make && make install</div></pre></td></tr></table></figure>\n<ol start=\"3\">\n<li>下载OpenSSL,可以从<a href=\"https://www.openssl.org/\" target=\"_blank\" rel=\"external\">OpenSSL</a>或者<a href=\"http://www.libressl.org/\" target=\"_blank\" rel=\"external\">LibreSSL</a>下载</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /ppxu</div><div class=\"line\">$ wget http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.3.1.tar.gz</div><div class=\"line\">$ tar zxvf libressl-2.3.1.tar.gz</div></pre></td></tr></table></figure>\n<ol start=\"4\">\n<li>下载,配置并编译Nginx</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /ppxu</div><div class=\"line\">$ wget http://nginx.org/download/nginx-1.9.9.tar.gz</div><div class=\"line\">$ tar zxvf nginx-1.9.9.tar.gz</div><div class=\"line\">$ <span class=\"built_in\">cd</span> nginx-1.9.9/</div><div class=\"line\">$ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/<span class=\"built_in\">log</span>/nginx/error.log --http-log-path=/var/<span class=\"built_in\">log</span>/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-openssl=../libressl-2.3.1 --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt=<span class=\"string\">'-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'</span></div><div class=\"line\">$ make</div></pre></td></tr></table></figure>\n<p>其中的<code>--with-http_v2_module</code>就是开启HTTP/2的设置。</p>\n<ol start=\"5\">\n<li>替换Nginx</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">which</span> nginx //查找nginx路径</div><div class=\"line\">$ mv /usr/sbin/nginx /usr/sbin/nginx.old //备份旧版nginx</div><div class=\"line\">$ cp objs/nginx /usr/sbin/ //将编译好的新版nginx复制过去</div></pre></td></tr></table></figure>\n<ol start=\"6\">\n<li>确认更新生效</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ /usr/sbin/nginx -t</div><div class=\"line\">nginx: the configuration file /etc/nginx/nginx.conf syntax is ok</div><div class=\"line\">nginx: configuration file /etc/nginx/nginx.conf <span class=\"built_in\">test</span> is successful</div><div class=\"line\">$ /usr/sbin/nginx -v</div><div class=\"line\">nginx version: nginx/1.9.9</div></pre></td></tr></table></figure>\n<ol start=\"7\">\n<li>更新Nginx配置文件</li>\n</ol>\n <figure class=\"highlight lsl\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> <span class=\"section\">listen</span> <span class=\"number\">80</span>;</div><div class=\"line\"> <span class=\"section\">listen</span> <span class=\"number\">443</span> ssl http2;</div><div class=\"line\"> server_name ppxu.me *.ppxu.me;</div><div class=\"line\"></div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<ol start=\"8\">\n<li>重启Nginx即可</li>\n</ol>\n<p>访问网站,在响应头里可以看到<code>server:nginx/1.9.9</code>,同时,在<a href=\"chrome://net-internals/#http2\" target=\"_blank\" rel=\"external\">chrome://net-internals/#http2</a>上面可以看到网站已经支持了HTTP/2</p>\n<p><img src=\"/img/http2.png\" alt=\"/img/http2.png\"></p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li><a href=\"http://www.linuxde.net/2011/08/554.html\" target=\"_blank\" rel=\"external\">http://www.linuxde.net/2011/08/554.html</a></li>\n<li><a href=\"http://www.linuxidc.com/Linux/2014-02/96137.htm\" target=\"_blank\" rel=\"external\">http://www.linuxidc.com/Linux/2014-02/96137.htm</a></li>\n<li><a href=\"http://www.poluoluo.com/server/201403/265778.html\" target=\"_blank\" rel=\"external\">http://www.poluoluo.com/server/201403/265778.html</a></li>\n<li><a href=\"https://imququ.com/post/http2-resource.html\" target=\"_blank\" rel=\"external\">https://imququ.com/post/http2-resource.html</a></li>\n<li><a href=\"https://imququ.com/post/nginx-http2-patch.html\" target=\"_blank\" rel=\"external\">https://imququ.com/post/nginx-http2-patch.html</a></li>\n<li><a href=\"http://www.tuicool.com/articles/3eeIVfi\" target=\"_blank\" rel=\"external\">http://www.tuicool.com/articles/3eeIVfi</a></li>\n</ul>\n","excerpt":"<p>之前说到SPDY已经被HTTP/2上位了,继续用SPDY也不合适,今天就趁空升级了最新的Nginx,并开启了HTTP/2,操作过程如下</p>","more":"<ol>\n<li>检查当前Nginx版本和配置参数</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ nginx -V</div><div class=\"line\">nginx version: nginx/1.8.0</div><div class=\"line\">built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)</div><div class=\"line\">built with OpenSSL 1.0.1e-fips 11 Feb 2013</div><div class=\"line\">TLS SNI support enabled</div><div class=\"line\">configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/<span class=\"built_in\">log</span>/nginx/error.log --http-log-path=/var/<span class=\"built_in\">log</span>/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-openssl=../libressl-2.2.2 --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt=<span class=\"string\">'-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'</span></div></pre></td></tr></table></figure>\n<p>记下这里的configure arguments,后面编译的时候要用的。</p>\n<ol start=\"2\">\n<li>安装PCRE,Nginx的rewrite模块依赖PCRE</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /ppxu</div><div class=\"line\">$ yum -y install make zlib zlib-devel gcc-c++ libtool</div><div class=\"line\">$ wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz</div><div class=\"line\">$ tar zxvf pcre-8.37.tar.gz</div><div class=\"line\">$ <span class=\"built_in\">cd</span> pcre-8.37/</div><div class=\"line\">$ ./configure</div><div class=\"line\">$ make && make install</div></pre></td></tr></table></figure>\n<ol start=\"3\">\n<li>下载OpenSSL,可以从<a href=\"https://www.openssl.org/\">OpenSSL</a>或者<a href=\"http://www.libressl.org/\">LibreSSL</a>下载</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /ppxu</div><div class=\"line\">$ wget http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.3.1.tar.gz</div><div class=\"line\">$ tar zxvf libressl-2.3.1.tar.gz</div></pre></td></tr></table></figure>\n<ol start=\"4\">\n<li>下载,配置并编译Nginx</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">cd</span> /ppxu</div><div class=\"line\">$ wget http://nginx.org/download/nginx-1.9.9.tar.gz</div><div class=\"line\">$ tar zxvf nginx-1.9.9.tar.gz</div><div class=\"line\">$ <span class=\"built_in\">cd</span> nginx-1.9.9/</div><div class=\"line\">$ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/<span class=\"built_in\">log</span>/nginx/error.log --http-log-path=/var/<span class=\"built_in\">log</span>/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-openssl=../libressl-2.3.1 --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt=<span class=\"string\">'-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'</span></div><div class=\"line\">$ make</div></pre></td></tr></table></figure>\n<p>其中的<code>--with-http_v2_module</code>就是开启HTTP/2的设置。</p>\n<ol start=\"5\">\n<li>替换Nginx</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ <span class=\"built_in\">which</span> nginx //查找nginx路径</div><div class=\"line\">$ mv /usr/sbin/nginx /usr/sbin/nginx.old //备份旧版nginx</div><div class=\"line\">$ cp objs/nginx /usr/sbin/ //将编译好的新版nginx复制过去</div></pre></td></tr></table></figure>\n<ol start=\"6\">\n<li>确认更新生效</li>\n</ol>\n <figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">$ /usr/sbin/nginx -t</div><div class=\"line\">nginx: the configuration file /etc/nginx/nginx.conf syntax is ok</div><div class=\"line\">nginx: configuration file /etc/nginx/nginx.conf <span class=\"built_in\">test</span> is successful</div><div class=\"line\">$ /usr/sbin/nginx -v</div><div class=\"line\">nginx version: nginx/1.9.9</div></pre></td></tr></table></figure>\n<ol start=\"7\">\n<li>更新Nginx配置文件</li>\n</ol>\n <figure class=\"highlight lsl\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> <span class=\"section\">listen</span> <span class=\"number\">80</span>;</div><div class=\"line\"> <span class=\"section\">listen</span> <span class=\"number\">443</span> ssl http2;</div><div class=\"line\"> server_name ppxu.me *.ppxu.me;</div><div class=\"line\"></div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<ol start=\"8\">\n<li>重启Nginx即可</li>\n</ol>\n<p>访问网站,在响应头里可以看到<code>server:nginx/1.9.9</code>,同时,在<a href=\"chrome://net-internals/#http2\">chrome://net-internals/#http2</a>上面可以看到网站已经支持了HTTP/2</p>\n<p><img src=\"/img/http2.png\" alt=\"/img/http2.png\"></p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li><a href=\"http://www.linuxde.net/2011/08/554.html\">http://www.linuxde.net/2011/08/554.html</a></li>\n<li><a href=\"http://www.linuxidc.com/Linux/2014-02/96137.htm\">http://www.linuxidc.com/Linux/2014-02/96137.htm</a></li>\n<li><a href=\"http://www.poluoluo.com/server/201403/265778.html\">http://www.poluoluo.com/server/201403/265778.html</a></li>\n<li><a href=\"https://imququ.com/post/http2-resource.html\">https://imququ.com/post/http2-resource.html</a></li>\n<li><a href=\"https://imququ.com/post/nginx-http2-patch.html\">https://imququ.com/post/nginx-http2-patch.html</a></li>\n<li><a href=\"http://www.tuicool.com/articles/3eeIVfi\">http://www.tuicool.com/articles/3eeIVfi</a></li>\n</ul>"},{"title":"在Mac上使用Docker配置nginx和node.js","date":"2016-09-06T11:52:07.000Z","_content":"简单记录一下如何在 Mac 上使用 Docker,并配置 nginx 和 node.js 开发环境的操作步骤。\n\n<!--more-->\n\nMac 上推荐使用官方的安装包来安装 [Docker](https://www.docker.com/),这是 [下载地址](https://download.docker.com/mac/stable/Docker.dmg),安装好后运行软件,在菜单栏上就可以看到一只萌萌的鲸鱼了。\n\n我的目标是基于一个 Linux 镜像,安装 nginx 和 node.js,运行一个简单的 node.js 服务器。\n\n[Docker Hub](https://hub.docker.com/explore/) 上有很多官方和非官方的镜像,我选的是官方的 [centos](https://hub.docker.com/_/centos/)。\n\n下面所有操作都是在命令行中执行。\n\n``` bash\n // 下载最新 centos 镜像\n $ docker pull centos:latest\n\n // 运行这个镜像,参数说明详见 [reference/commandline](https://docs.docker.com/engine/reference/commandline/)\n $ docker run -it --name web -p 8888:80 centos:latest\n```\n\n然后命令行就会切换到容器系统的命令行中,下面就是安装 nginx 和 node.js 了。\n\n``` bash\n # 不更新了,更新会增加几百M镜像体积\n # // 更新包\n # $ yum -y update\n\n // 安装 nginx\n $ yum -y install wget\n $ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm\n $ rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm\n $ yum -y install nginx\n\n // 修改一下 nginx 配置,转发 node.js 端口\n $ vi /etc/nginx/conf.d/default.conf\n```\n\n```\n server {\n listen 80;\n server_name localhost;\n\n location / {\n proxy_pass http://127.0.0.1:3000/;\n proxy_redirect off;\n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n }\n\n ...\n```\n\n``` bash\n // 启动 nginx\n $ nginx\n```\n\n下面是手动编译安装 node.js\n\n``` bash\n // 安装依赖\n $ yum -y install gcc-c++\n\n // 下载最新 node.js\n $ wget https://nodejs.org/dist/v6.5.0/node-v6.5.0.tar.gz\n\n // 解压&安装\n $ tar zxf node-v6.5.0.tar.gz\n $ cd node-v6.5.0\n $ ./configure\n $ make && make install\n```\n\nnode.js 安装好后写一个简单的 server.js\n\n```\n const http = require('http')\n\n const server = http.createServer((req, res) => {\n res.end('Hello Docker!')\n })\n\n server.listen(3000)\n```\n\n``` bash\n // 启动 server\n $ node server.js\n```\n\n这样子整个流程就跑通了,在浏览器里面访问 `http://localhost:8888`,就可以看到\n\n\n\n接下来我们还可以把这个修改过的容器保存下来\n\n``` bash\n // 退出容器系统\n $ exit\n\n // 查看容器信息\n $ docker ps -a\n\n // 将容器转化成镜像,ppxu 是 Docker Hub 上的用户名,cnn 是镜像名,v1 是一个 tag\n $ docker commit -m 'add nginx & node to centos' -a 'ppxu' web ppxu/cnn:v1\n\n // 查看所有的镜像\n $ docker images\n\n // 运行刚刚保存的镜像\n $ docker run -it -p 8888:80 ppxu/cnn:v1\n```\n\n在这个容器中可以看到刚刚安装的 nginx 和 node.js 都正常运行。\n\n最后我们可以把这个镜像上传到 Docker Hub 中,首先 [注册帐号](https://hub.docker.com/)\n\n``` bash\n // 用刚注册的帐号登录\n $ docker login\n\n // 上传镜像\n $ docker push ppxu/cnn:v1\n```\n\n然后就可以在 [Docker Hub](https://hub.docker.com/) 上看到刚才上传的镜像了,其他人就可以直接使用这个镜像了。\n\n#### 参考资料\n\n* [https://docs.docker.com/engine/getstarted/](https://docs.docker.com/engine/getstarted/)\n* [http://blog.saymagic.cn/2015/06/01/learning-docker.html#bqlkp](http://blog.saymagic.cn/2015/06/01/learning-docker.html#bqlkp)\n","source":"_posts/setup-nginx-nodejs-with-docker-in-mac.md","raw":"title: 在Mac上使用Docker配置nginx和node.js\ndate: 2016-09-06 19:52:07\ncategories: blog\ntags: [mac, docker, centos, nginx, nodejs]\n---\n简单记录一下如何在 Mac 上使用 Docker,并配置 nginx 和 node.js 开发环境的操作步骤。\n\n<!--more-->\n\nMac 上推荐使用官方的安装包来安装 [Docker](https://www.docker.com/),这是 [下载地址](https://download.docker.com/mac/stable/Docker.dmg),安装好后运行软件,在菜单栏上就可以看到一只萌萌的鲸鱼了。\n\n我的目标是基于一个 Linux 镜像,安装 nginx 和 node.js,运行一个简单的 node.js 服务器。\n\n[Docker Hub](https://hub.docker.com/explore/) 上有很多官方和非官方的镜像,我选的是官方的 [centos](https://hub.docker.com/_/centos/)。\n\n下面所有操作都是在命令行中执行。\n\n``` bash\n // 下载最新 centos 镜像\n $ docker pull centos:latest\n\n // 运行这个镜像,参数说明详见 [reference/commandline](https://docs.docker.com/engine/reference/commandline/)\n $ docker run -it --name web -p 8888:80 centos:latest\n```\n\n然后命令行就会切换到容器系统的命令行中,下面就是安装 nginx 和 node.js 了。\n\n``` bash\n # 不更新了,更新会增加几百M镜像体积\n # // 更新包\n # $ yum -y update\n\n // 安装 nginx\n $ yum -y install wget\n $ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm\n $ rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm\n $ yum -y install nginx\n\n // 修改一下 nginx 配置,转发 node.js 端口\n $ vi /etc/nginx/conf.d/default.conf\n```\n\n```\n server {\n listen 80;\n server_name localhost;\n\n location / {\n proxy_pass http://127.0.0.1:3000/;\n proxy_redirect off;\n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n }\n\n ...\n```\n\n``` bash\n // 启动 nginx\n $ nginx\n```\n\n下面是手动编译安装 node.js\n\n``` bash\n // 安装依赖\n $ yum -y install gcc-c++\n\n // 下载最新 node.js\n $ wget https://nodejs.org/dist/v6.5.0/node-v6.5.0.tar.gz\n\n // 解压&安装\n $ tar zxf node-v6.5.0.tar.gz\n $ cd node-v6.5.0\n $ ./configure\n $ make && make install\n```\n\nnode.js 安装好后写一个简单的 server.js\n\n```\n const http = require('http')\n\n const server = http.createServer((req, res) => {\n res.end('Hello Docker!')\n })\n\n server.listen(3000)\n```\n\n``` bash\n // 启动 server\n $ node server.js\n```\n\n这样子整个流程就跑通了,在浏览器里面访问 `http://localhost:8888`,就可以看到\n\n\n\n接下来我们还可以把这个修改过的容器保存下来\n\n``` bash\n // 退出容器系统\n $ exit\n\n // 查看容器信息\n $ docker ps -a\n\n // 将容器转化成镜像,ppxu 是 Docker Hub 上的用户名,cnn 是镜像名,v1 是一个 tag\n $ docker commit -m 'add nginx & node to centos' -a 'ppxu' web ppxu/cnn:v1\n\n // 查看所有的镜像\n $ docker images\n\n // 运行刚刚保存的镜像\n $ docker run -it -p 8888:80 ppxu/cnn:v1\n```\n\n在这个容器中可以看到刚刚安装的 nginx 和 node.js 都正常运行。\n\n最后我们可以把这个镜像上传到 Docker Hub 中,首先 [注册帐号](https://hub.docker.com/)\n\n``` bash\n // 用刚注册的帐号登录\n $ docker login\n\n // 上传镜像\n $ docker push ppxu/cnn:v1\n```\n\n然后就可以在 [Docker Hub](https://hub.docker.com/) 上看到刚才上传的镜像了,其他人就可以直接使用这个镜像了。\n\n#### 参考资料\n\n* [https://docs.docker.com/engine/getstarted/](https://docs.docker.com/engine/getstarted/)\n* [http://blog.saymagic.cn/2015/06/01/learning-docker.html#bqlkp](http://blog.saymagic.cn/2015/06/01/learning-docker.html#bqlkp)\n","slug":"setup-nginx-nodejs-with-docker-in-mac","published":1,"updated":"2017-02-21T07:10:04.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cizf7siop000la3xxlp5n6wki","content":"<p>简单记录一下如何在 Mac 上使用 Docker,并配置 nginx 和 node.js 开发环境的操作步骤。</p>\n<a id=\"more\"></a>\n<p>Mac 上推荐使用官方的安装包来安装 <a href=\"https://www.docker.com/\" target=\"_blank\" rel=\"external\">Docker</a>,这是 <a href=\"https://download.docker.com/mac/stable/Docker.dmg\" target=\"_blank\" rel=\"external\">下载地址</a>,安装好后运行软件,在菜单栏上就可以看到一只萌萌的鲸鱼了。</p>\n<p>我的目标是基于一个 Linux 镜像,安装 nginx 和 node.js,运行一个简单的 node.js 服务器。</p>\n<p><a href=\"https://hub.docker.com/explore/\" target=\"_blank\" rel=\"external\">Docker Hub</a> 上有很多官方和非官方的镜像,我选的是官方的 <a href=\"https://hub.docker.com/_/centos/\" target=\"_blank\" rel=\"external\">centos</a>。</p>\n<p>下面所有操作都是在命令行中执行。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 下载最新 centos 镜像</div><div class=\"line\">$ docker pull centos:latest</div><div class=\"line\"></div><div class=\"line\">// 运行这个镜像,参数说明详见 [reference/commandline](https://docs.docker.com/engine/reference/commandline/)</div><div class=\"line\">$ docker run -it --name web -p 8888:80 centos:latest</div></pre></td></tr></table></figure>\n<p>然后命令行就会切换到容器系统的命令行中,下面就是安装 nginx 和 node.js 了。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"comment\"># 不更新了,更新会增加几百M镜像体积</span></div><div class=\"line\"><span class=\"comment\"># // 更新包</span></div><div class=\"line\"><span class=\"comment\"># $ yum -y update</span></div><div class=\"line\"></div><div class=\"line\">// 安装 nginx</div><div class=\"line\">$ yum -y install wget</div><div class=\"line\">$ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm</div><div class=\"line\">$ rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm</div><div class=\"line\">$ yum -y install nginx</div><div class=\"line\"></div><div class=\"line\">// 修改一下 nginx 配置,转发 node.js 端口</div><div class=\"line\">$ vi /etc/nginx/conf.d/default.conf</div></pre></td></tr></table></figure>\n<figure class=\"highlight tp\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> listen <span class=\"number\">80</span>;</div><div class=\"line\"> server_name localhost;</div><div class=\"line\"></div><div class=\"line\"> location / {</div><div class=\"line\"> proxy_pass http:<span class=\"comment\">//127.0.0.1:3000/;</span></div><div class=\"line\"> proxy_redirect off;</div><div class=\"line\"> proxy_set_header <span class=\"keyword\">X</span>-Real-IP <span class=\"variable\">$remote_addr</span>;</div><div class=\"line\"> proxy_set_header <span class=\"keyword\">X</span>-Forwarded-For <span class=\"variable\">$proxy_add_x_forwarded_for</span>;</div><div class=\"line\"> }</div><div class=\"line\"></div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 启动 nginx</div><div class=\"line\">$ nginx</div></pre></td></tr></table></figure>\n<p>下面是手动编译安装 node.js</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 安装依赖</div><div class=\"line\">$ yum -y install gcc-c++</div><div class=\"line\"></div><div class=\"line\">// 下载最新 node.js</div><div class=\"line\">$ wget https://nodejs.org/dist/v6.5.0/node-v6.5.0.tar.gz</div><div class=\"line\"></div><div class=\"line\">// 解压&安装</div><div class=\"line\">$ tar zxf node-v6.5.0.tar.gz</div><div class=\"line\">$ <span class=\"built_in\">cd</span> node-v6.5.0</div><div class=\"line\">$ ./configure</div><div class=\"line\">$ make && make install</div></pre></td></tr></table></figure>\n<p>node.js 安装好后写一个简单的 server.js</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"keyword\">const</span> http = <span class=\"built_in\">require</span>(<span class=\"string\">'http'</span>)</div><div class=\"line\"></div><div class=\"line\"><span class=\"keyword\">const</span> server = http.createServer(<span class=\"function\">(<span class=\"params\">req, res</span>) =></span> {</div><div class=\"line\"> res.end(<span class=\"string\">'Hello Docker!'</span>)</div><div class=\"line\">})</div><div class=\"line\"></div><div class=\"line\">server.listen(<span class=\"number\">3000</span>)</div></pre></td></tr></table></figure>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 启动 server</div><div class=\"line\">$ node server.js</div></pre></td></tr></table></figure>\n<p>这样子整个流程就跑通了,在浏览器里面访问 <code>http://localhost:8888</code>,就可以看到</p>\n<p><img src=\"/img/docker.png\" alt=\"/img/docker.png\"></p>\n<p>接下来我们还可以把这个修改过的容器保存下来</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 退出容器系统</div><div class=\"line\">$ <span class=\"built_in\">exit</span></div><div class=\"line\"></div><div class=\"line\">// 查看容器信息</div><div class=\"line\">$ docker ps <span class=\"_\">-a</span></div><div class=\"line\"></div><div class=\"line\">// 将容器转化成镜像,ppxu 是 Docker Hub 上的用户名,cnn 是镜像名,v1 是一个 tag</div><div class=\"line\">$ docker commit -m <span class=\"string\">'add nginx & node to centos'</span> <span class=\"_\">-a</span> <span class=\"string\">'ppxu'</span> web ppxu/cnn:v1</div><div class=\"line\"></div><div class=\"line\">// 查看所有的镜像</div><div class=\"line\">$ docker images</div><div class=\"line\"></div><div class=\"line\">// 运行刚刚保存的镜像</div><div class=\"line\">$ docker run -it -p 8888:80 ppxu/cnn:v1</div></pre></td></tr></table></figure>\n<p>在这个容器中可以看到刚刚安装的 nginx 和 node.js 都正常运行。</p>\n<p>最后我们可以把这个镜像上传到 Docker Hub 中,首先 <a href=\"https://hub.docker.com/\" target=\"_blank\" rel=\"external\">注册帐号</a></p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 用刚注册的帐号登录</div><div class=\"line\">$ docker login</div><div class=\"line\"></div><div class=\"line\">// 上传镜像</div><div class=\"line\">$ docker push ppxu/cnn:v1</div></pre></td></tr></table></figure>\n<p>然后就可以在 <a href=\"https://hub.docker.com/\" target=\"_blank\" rel=\"external\">Docker Hub</a> 上看到刚才上传的镜像了,其他人就可以直接使用这个镜像了。</p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li><a href=\"https://docs.docker.com/engine/getstarted/\" target=\"_blank\" rel=\"external\">https://docs.docker.com/engine/getstarted/</a></li>\n<li><a href=\"http://blog.saymagic.cn/2015/06/01/learning-docker.html#bqlkp\" target=\"_blank\" rel=\"external\">http://blog.saymagic.cn/2015/06/01/learning-docker.html#bqlkp</a></li>\n</ul>\n","excerpt":"<p>简单记录一下如何在 Mac 上使用 Docker,并配置 nginx 和 node.js 开发环境的操作步骤。</p>","more":"<p>Mac 上推荐使用官方的安装包来安装 <a href=\"https://www.docker.com/\">Docker</a>,这是 <a href=\"https://download.docker.com/mac/stable/Docker.dmg\">下载地址</a>,安装好后运行软件,在菜单栏上就可以看到一只萌萌的鲸鱼了。</p>\n<p>我的目标是基于一个 Linux 镜像,安装 nginx 和 node.js,运行一个简单的 node.js 服务器。</p>\n<p><a href=\"https://hub.docker.com/explore/\">Docker Hub</a> 上有很多官方和非官方的镜像,我选的是官方的 <a href=\"https://hub.docker.com/_/centos/\">centos</a>。</p>\n<p>下面所有操作都是在命令行中执行。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 下载最新 centos 镜像</div><div class=\"line\">$ docker pull centos:latest</div><div class=\"line\"></div><div class=\"line\">// 运行这个镜像,参数说明详见 [reference/commandline](https://docs.docker.com/engine/reference/commandline/)</div><div class=\"line\">$ docker run -it --name web -p 8888:80 centos:latest</div></pre></td></tr></table></figure>\n<p>然后命令行就会切换到容器系统的命令行中,下面就是安装 nginx 和 node.js 了。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"comment\"># 不更新了,更新会增加几百M镜像体积</span></div><div class=\"line\"><span class=\"comment\"># // 更新包</span></div><div class=\"line\"><span class=\"comment\"># $ yum -y update</span></div><div class=\"line\"></div><div class=\"line\">// 安装 nginx</div><div class=\"line\">$ yum -y install wget</div><div class=\"line\">$ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm</div><div class=\"line\">$ rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm</div><div class=\"line\">$ yum -y install nginx</div><div class=\"line\"></div><div class=\"line\">// 修改一下 nginx 配置,转发 node.js 端口</div><div class=\"line\">$ vi /etc/nginx/conf.d/default.conf</div></pre></td></tr></table></figure>\n<figure class=\"highlight tp\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div></pre></td><td class=\"code\"><pre><div class=\"line\">server {</div><div class=\"line\"> listen <span class=\"number\">80</span>;</div><div class=\"line\"> server_name localhost;</div><div class=\"line\"></div><div class=\"line\"> location / {</div><div class=\"line\"> proxy_pass http:<span class=\"comment\">//127.0.0.1:3000/;</span></div><div class=\"line\"> proxy_redirect off;</div><div class=\"line\"> proxy_set_header <span class=\"keyword\">X</span>-Real-IP <span class=\"variable\">$remote_addr</span>;</div><div class=\"line\"> proxy_set_header <span class=\"keyword\">X</span>-Forwarded-For <span class=\"variable\">$proxy_add_x_forwarded_for</span>;</div><div class=\"line\"> }</div><div class=\"line\"></div><div class=\"line\"> ...</div></pre></td></tr></table></figure>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 启动 nginx</div><div class=\"line\">$ nginx</div></pre></td></tr></table></figure>\n<p>下面是手动编译安装 node.js</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 安装依赖</div><div class=\"line\">$ yum -y install gcc-c++</div><div class=\"line\"></div><div class=\"line\">// 下载最新 node.js</div><div class=\"line\">$ wget https://nodejs.org/dist/v6.5.0/node-v6.5.0.tar.gz</div><div class=\"line\"></div><div class=\"line\">// 解压&安装</div><div class=\"line\">$ tar zxf node-v6.5.0.tar.gz</div><div class=\"line\">$ <span class=\"built_in\">cd</span> node-v6.5.0</div><div class=\"line\">$ ./configure</div><div class=\"line\">$ make && make install</div></pre></td></tr></table></figure>\n<p>node.js 安装好后写一个简单的 server.js</p>\n<figure class=\"highlight javascript\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div></pre></td><td class=\"code\"><pre><div class=\"line\"><span class=\"keyword\">const</span> http = <span class=\"built_in\">require</span>(<span class=\"string\">'http'</span>)</div><div class=\"line\"></div><div class=\"line\"><span class=\"keyword\">const</span> server = http.createServer(<span class=\"function\">(<span class=\"params\">req, res</span>) =></span> {</div><div class=\"line\"> res.end(<span class=\"string\">'Hello Docker!'</span>)</div><div class=\"line\">})</div><div class=\"line\"></div><div class=\"line\">server.listen(<span class=\"number\">3000</span>)</div></pre></td></tr></table></figure>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 启动 server</div><div class=\"line\">$ node server.js</div></pre></td></tr></table></figure>\n<p>这样子整个流程就跑通了,在浏览器里面访问 <code>http://localhost:8888</code>,就可以看到</p>\n<p><img src=\"/img/docker.png\" alt=\"/img/docker.png\"></p>\n<p>接下来我们还可以把这个修改过的容器保存下来</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div><div class=\"line\">6</div><div class=\"line\">7</div><div class=\"line\">8</div><div class=\"line\">9</div><div class=\"line\">10</div><div class=\"line\">11</div><div class=\"line\">12</div><div class=\"line\">13</div><div class=\"line\">14</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 退出容器系统</div><div class=\"line\">$ <span class=\"built_in\">exit</span></div><div class=\"line\"></div><div class=\"line\">// 查看容器信息</div><div class=\"line\">$ docker ps <span class=\"_\">-a</span></div><div class=\"line\"></div><div class=\"line\">// 将容器转化成镜像,ppxu 是 Docker Hub 上的用户名,cnn 是镜像名,v1 是一个 tag</div><div class=\"line\">$ docker commit -m <span class=\"string\">'add nginx & node to centos'</span> <span class=\"_\">-a</span> <span class=\"string\">'ppxu'</span> web ppxu/cnn:v1</div><div class=\"line\"></div><div class=\"line\">// 查看所有的镜像</div><div class=\"line\">$ docker images</div><div class=\"line\"></div><div class=\"line\">// 运行刚刚保存的镜像</div><div class=\"line\">$ docker run -it -p 8888:80 ppxu/cnn:v1</div></pre></td></tr></table></figure>\n<p>在这个容器中可以看到刚刚安装的 nginx 和 node.js 都正常运行。</p>\n<p>最后我们可以把这个镜像上传到 Docker Hub 中,首先 <a href=\"https://hub.docker.com/\">注册帐号</a></p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><div class=\"line\">1</div><div class=\"line\">2</div><div class=\"line\">3</div><div class=\"line\">4</div><div class=\"line\">5</div></pre></td><td class=\"code\"><pre><div class=\"line\">// 用刚注册的帐号登录</div><div class=\"line\">$ docker login</div><div class=\"line\"></div><div class=\"line\">// 上传镜像</div><div class=\"line\">$ docker push ppxu/cnn:v1</div></pre></td></tr></table></figure>\n<p>然后就可以在 <a href=\"https://hub.docker.com/\">Docker Hub</a> 上看到刚才上传的镜像了,其他人就可以直接使用这个镜像了。</p>\n<h4 id=\"参考资料\"><a class=\"header-anchor\" href=\"#参考资料\">¶</a>参考资料</h4>\n<ul>\n<li><a href=\"https://docs.docker.com/engine/getstarted/\">https://docs.docker.com/engine/getstarted/</a></li>\n<li><a href=\"http://blog.saymagic.cn/2015/06/01/learning-docker.html#bqlkp\">http://blog.saymagic.cn/2015/06/01/learning-docker.html#bqlkp</a></li>\n</ul>"}],"PostAsset":[],"PostCategory":[{"post_id":"cizf7sio60005a3xxp6xvf9ok","category_id":"cizf7sinx0002a3xxa3qhpj7h","_id":"cizf7sioh000aa3xx1h96hq5f"},{"post_id":"cizf7sino0000a3xxxmxo77d3","category_id":"cizf7sinx0002a3xxa3qhpj7h","_id":"cizf7siol000ea3xxuqb41dxq"},{"post_id":"cizf7siod0009a3xx8rely9t2","category_id":"cizf7sinx0002a3xxa3qhpj7h","_id":"cizf7sion000ga3xx6ymtog3k"},{"post_id":"cizf7sint0001a3xxvura8kul","category_id":"cizf7sinx0002a3xxa3qhpj7h","_id":"cizf7siop000ka3xx4igo0cpm"},{"post_id":"cizf7sioh000ba3xxnhokh5p0","category_id":"cizf7sinx0002a3xxa3qhpj7h","_id":"cizf7sior000ma3xx6qoed5fp"},{"post_id":"cizf7sio40004a3xxrlp2zxu9","category_id":"cizf7sinx0002a3xxa3qhpj7h","_id":"cizf7sios000na3xxdgnyn7xr"},{"post_id":"cizf7sion000ha3xxq64rdkm1","category_id":"cizf7sinx0002a3xxa3qhpj7h","_id":"cizf7siot000pa3xxt2fobi9f"},{"post_id":"cizf7siop000la3xxlp5n6wki","category_id":"cizf7sinx0002a3xxa3qhpj7h","_id":"cizf7siow000qa3xx17qulwrb"},{"post_id":"cizf7siol000fa3xxjat2hs9w","category_id":"cizf7siop000ja3xxic35r5ap","_id":"cizf7sioy000sa3xxi8kuciid"}],"PostTag":[{"post_id":"cizf7sino0000a3xxxmxo77d3","tag_id":"cizf7sio30003a3xx3l271kec","_id":"cizf7sioz000ta3xxymjhj4a9"},{"post_id":"cizf7sino0000a3xxxmxo77d3","tag_id":"cizf7sioa0008a3xx0xawrutk","_id":"cizf7sip0000ua3xxbr7mvedg"},{"post_id":"cizf7sino0000a3xxxmxo77d3","tag_id":"cizf7siol000da3xxeogu9xv9","_id":"cizf7sip1000wa3xxc7ene9mh"},{"post_id":"cizf7sino0000a3xxxmxo77d3","tag_id":"cizf7siop000ia3xxrx5neqm5","_id":"cizf7sip2000xa3xxo242pk0f"},{"post_id":"cizf7sino0000a3xxxmxo77d3","tag_id":"cizf7siot000oa3xxp7l5kfer","_id":"cizf7sip2000za3xxusvhbi26"},{"post_id":"cizf7sint0001a3xxvura8kul","tag_id":"cizf7siox000ra3xxtjx5dlsy","_id":"cizf7sip40013a3xxbfiltg2m"},{"post_id":"cizf7sint0001a3xxvura8kul","tag_id":"cizf7sip0000va3xx39xd5hxq","_id":"cizf7sip40014a3xx9zuotgj5"},{"post_id":"cizf7sint0001a3xxvura8kul","tag_id":"cizf7sip2000ya3xx19o6t99v","_id":"cizf7sip40016a3xxvsb52vtw"},{"post_id":"cizf7sint0001a3xxvura8kul","tag_id":"cizf7sip30010a3xxgf2oyp6i","_id":"cizf7sip40017a3xxc90ya2fg"},{"post_id":"cizf7sint0001a3xxvura8kul","tag_id":"cizf7sip30011a3xx35phzh73","_id":"cizf7sip50019a3xxf3dye221"},{"post_id":"cizf7sio40004a3xxrlp2zxu9","tag_id":"cizf7siox000ra3xxtjx5dlsy","_id":"cizf7sip7001ea3xxfxh40vw1"},{"post_id":"cizf7sio40004a3xxrlp2zxu9","tag_id":"cizf7sip0000va3xx39xd5hxq","_id":"cizf7sip8001fa3xx5k8hfoly"},{"post_id":"cizf7sio40004a3xxrlp2zxu9","tag_id":"cizf7sip2000ya3xx19o6t99v","_id":"cizf7sip8001ha3xxkrbdlgn4"},{"post_id":"cizf7sio40004a3xxrlp2zxu9","tag_id":"cizf7sip30010a3xxgf2oyp6i","_id":"cizf7sipa001ia3xxzq8ny3y1"},{"post_id":"cizf7sio40004a3xxrlp2zxu9","tag_id":"cizf7sip5001ba3xxjv2o7va0","_id":"cizf7sipb001ka3xx4kbarbgw"},{"post_id":"cizf7sio40004a3xxrlp2zxu9","tag_id":"cizf7sip6001ca3xxwb6zog3j","_id":"cizf7sipb001la3xxs72cdt9t"},{"post_id":"cizf7sio60005a3xxp6xvf9ok","tag_id":"cizf7siol000da3xxeogu9xv9","_id":"cizf7sipd001na3xx7avfp1ej"},{"post_id":"cizf7sio60005a3xxp6xvf9ok","tag_id":"cizf7sip8001ga3xxx1lnuwpf","_id":"cizf7sipd001oa3xxua6cuu69"},{"post_id":"cizf7sio60005a3xxp6xvf9ok","tag_id":"cizf7sipb001ja3xx9m5kxdsu","_id":"cizf7sipe001qa3xxmomhihle"},{"post_id":"cizf7siod0009a3xx8rely9t2","tag_id":"cizf7siox000ra3xxtjx5dlsy","_id":"cizf7sipk001wa3xx4c5x9q3m"},{"post_id":"cizf7siod0009a3xx8rely9t2","tag_id":"cizf7sip0000va3xx39xd5hxq","_id":"cizf7sipk001xa3xx59z7oscr"},{"post_id":"cizf7siod0009a3xx8rely9t2","tag_id":"cizf7sip2000ya3xx19o6t99v","_id":"cizf7sipl001za3xx2llc31w4"},{"post_id":"cizf7siod0009a3xx8rely9t2","tag_id":"cizf7sipf001sa3xx3w0tstzw","_id":"cizf7sipl0020a3xx0z6wir5m"},{"post_id":"cizf7siod0009a3xx8rely9t2","tag_id":"cizf7siol000da3xxeogu9xv9","_id":"cizf7sipl0022a3xxgztvot96"},{"post_id":"cizf7siod0009a3xx8rely9t2","tag_id":"cizf7siph001ua3xxd2yf5c1e","_id":"cizf7sipl0023a3xxbzxrind2"},{"post_id":"cizf7sioh000ba3xxnhokh5p0","tag_id":"cizf7siox000ra3xxtjx5dlsy","_id":"cizf7sipm0026a3xxayfhjbpz"},{"post_id":"cizf7sioh000ba3xxnhokh5p0","tag_id":"cizf7sip0000va3xx39xd5hxq","_id":"cizf7sipm0027a3xxl3gye0wt"},{"post_id":"cizf7sioh000ba3xxnhokh5p0","tag_id":"cizf7sip2000ya3xx19o6t99v","_id":"cizf7sipn0029a3xxsrqcdll9"},{"post_id":"cizf7sioh000ba3xxnhokh5p0","tag_id":"cizf7sipm0024a3xxqsrjgxv6","_id":"cizf7sipn002aa3xxqbxx2mpy"},{"post_id":"cizf7siol000fa3xxjat2hs9w","tag_id":"cizf7sipm0025a3xxd0pjhwmn","_id":"cizf7sipq002fa3xx43twbzlp"},{"post_id":"cizf7siol000fa3xxjat2hs9w","tag_id":"cizf7sipm0028a3xx225tihsh","_id":"cizf7sipq002ga3xxahz0fetn"},{"post_id":"cizf7siol000fa3xxjat2hs9w","tag_id":"cizf7sipf001sa3xx3w0tstzw","_id":"cizf7sipr002ia3xx5b4mf3ed"},{"post_id":"cizf7siol000fa3xxjat2hs9w","tag_id":"cizf7sipo002ca3xxu7qt4ymp","_id":"cizf7sipr002ja3xx04xbjx53"},{"post_id":"cizf7siol000fa3xxjat2hs9w","tag_id":"cizf7sipo002da3xx0lyrv44v","_id":"cizf7sipr002la3xxh7p4bql6"},{"post_id":"cizf7sion000ha3xxq64rdkm1","tag_id":"cizf7siox000ra3xxtjx5dlsy","_id":"cizf7sipt002pa3xxlwa3ua16"},{"post_id":"cizf7sion000ha3xxq64rdkm1","tag_id":"cizf7sip0000va3xx39xd5hxq","_id":"cizf7sipt002qa3xx84r4ytez"},{"post_id":"cizf7sion000ha3xxq64rdkm1","tag_id":"cizf7sip2000ya3xx19o6t99v","_id":"cizf7sipt002sa3xxyzj0icyi"},{"post_id":"cizf7sion000ha3xxq64rdkm1","tag_id":"cizf7sip30010a3xxgf2oyp6i","_id":"cizf7sipt002ta3xxp2le7t36"},{"post_id":"cizf7sion000ha3xxq64rdkm1","tag_id":"cizf7sips002na3xxg46us62o","_id":"cizf7sipu002va3xxmns6qoz0"},{"post_id":"cizf7siop000la3xxlp5n6wki","tag_id":"cizf7sips002oa3xx1i1zgkfu","_id":"cizf7siq0002ya3xxq38bkv4p"},{"post_id":"cizf7siop000la3xxlp5n6wki","tag_id":"cizf7sipt002ra3xxkxcoskyx","_id":"cizf7siq0002za3xxmxqh9a4w"},{"post_id":"cizf7siop000la3xxlp5n6wki","tag_id":"cizf7sip2000ya3xx19o6t99v","_id":"cizf7siq00030a3xxd5kmuz0l"},{"post_id":"cizf7siop000la3xxlp5n6wki","tag_id":"cizf7sip30010a3xxgf2oyp6i","_id":"cizf7siq00031a3xxu4l5t1pp"},{"post_id":"cizf7siop000la3xxlp5n6wki","tag_id":"cizf7sipf001sa3xx3w0tstzw","_id":"cizf7siq00032a3xxxr666288"}],"Tag":[{"name":"github","_id":"cizf7sio30003a3xx3l271kec"},{"name":"webhooks","_id":"cizf7sioa0008a3xx0xawrutk"},{"name":"hexo","_id":"cizf7siol000da3xxeogu9xv9"},{"name":"deploy","_id":"cizf7siop000ia3xxrx5neqm5"},{"name":"shell","_id":"cizf7siot000oa3xxp7l5kfer"},{"name":"aliyun","_id":"cizf7siox000ra3xxtjx5dlsy"},{"name":"ecs","_id":"cizf7sip0000va3xx39xd5hxq"},{"name":"centos","_id":"cizf7sip2000ya3xx19o6t99v"},{"name":"nginx","_id":"cizf7sip30010a3xxgf2oyp6i"},{"name":"spdy","_id":"cizf7sip30011a3xx35phzh73"},{"name":"ssl","_id":"cizf7sip5001ba3xxjv2o7va0"},{"name":"https","_id":"cizf7sip6001ca3xxwb6zog3j"},{"name":"markdown","_id":"cizf7sip8001ga3xxx1lnuwpf"},{"name":"emoji","_id":"cizf7sipb001ja3xx9m5kxdsu"},{"name":"nodejs","_id":"cizf7sipf001sa3xx3w0tstzw"},{"name":"blog","_id":"cizf7siph001ua3xxd2yf5c1e"},{"name":"shadowsocks","_id":"cizf7sipm0024a3xxqsrjgxv6"},{"name":"electron","_id":"cizf7sipm0025a3xxd0pjhwmn"},{"name":"atom","_id":"cizf7sipm0028a3xx225tihsh"},{"name":"chromium","_id":"cizf7sipo002ca3xxu7qt4ymp"},{"name":"app","_id":"cizf7sipo002da3xx0lyrv44v"},{"name":"http2","_id":"cizf7sips002na3xxg46us62o"},{"name":"mac","_id":"cizf7sips002oa3xx1i1zgkfu"},{"name":"docker","_id":"cizf7sipt002ra3xxkxcoskyx"}]}}