greenlock-hapi.js/README.md

113 lines
3.2 KiB
Markdown
Raw Normal View History

2018-04-20 06:30:13 +00:00
# greenlock-hapi
2016-12-30 09:22:46 +00:00
2018-04-20 06:30:13 +00:00
(previously letsencrypt-hapi)
2016-11-02 00:30:09 +00:00
2018-04-20 06:30:13 +00:00
| [greenlock (lib)](https://git.coolaj86.com/coolaj86/greenlock.js)
| [greenlock-cli](https://git.coolaj86.com/coolaj86/greenlock-cli.js)
| [greenlock-express](https://git.coolaj86.com/coolaj86/greenlock-express.js)
| [greenlock-cluster](https://git.coolaj86.com/coolaj86/greenlock-cluster.js)
| [greenlock-koa](https://git.coolaj86.com/coolaj86/greenlock-koa.js)
2017-01-25 21:28:44 +00:00
| **greenlock-hapi**
2016-04-22 18:20:43 +00:00
|
2016-04-19 04:29:03 +00:00
Free SSL and Automatic HTTPS for node.js with hapi.js and other middleware systems via Let's Encrypt
* Automatic Registration via SNI (`httpsOptions.SNICallback`)
* **registrations** require an **approval callback** in *production*
* Automatic Renewal (around 80 days)
* **renewals** are *fully automatic* and happen in the *background*, with **no downtime**
* Automatic vhost / virtual hosting
All you have to do is start the webserver and then visit it at it's domain name.
## Install
```
2017-01-25 21:28:44 +00:00
npm install --save greenlock-express@2.x
2016-04-19 04:29:03 +00:00
```
2017-01-25 21:28:44 +00:00
*Pay no attention to the man behind the curtain.* (just ignore that the name of the module is greenlock-express)
2016-04-19 04:29:03 +00:00
2017-01-25 21:28:44 +00:00
### Part 1: Configure Greenlock
2016-04-19 04:29:03 +00:00
```javascript
'use strict';
2017-01-25 21:28:44 +00:00
var le = require('greenlock-express').create({
2018-04-20 06:30:13 +00:00
// You MUST change this to 'https://acme-v02.api.letsencrypt.org/directory' in production
server: 'https://acme-staging-v02.api.letsencrypt.org/directory'
, version: 'draft-11' // Let's Encrypt v2
2016-08-16 20:41:18 +00:00
, configDir: require('os').homedir() + '/letsencrypt/etc'
, approveDomains: function (opts, certs, cb) {
opts.domains = certs && certs.altnames || opts.domains;
opts.email = 'john.doe@example.com' // CHANGE ME
opts.agreeTos = true;
cb(null, { options: opts, certs: certs });
2016-04-19 04:29:03 +00:00
}
2016-08-16 20:41:18 +00:00
, debug: true
2016-04-19 04:29:03 +00:00
});
```
2016-08-16 20:41:18 +00:00
WARNING: If you don't do any checks and simply complete `approveDomains` callback,
2016-04-19 04:29:03 +00:00
an attacker will spoof SNI packets with bad hostnames and that will cause you to be rate-limited
and/or blocked from the ACME server.
Alternatively, You can run registration *manually*:
```bash
2017-01-25 21:28:44 +00:00
npm install -g greenlock-cli
2016-04-19 04:29:03 +00:00
2017-01-25 21:28:44 +00:00
greenlock certonly --standalone \
2018-04-20 06:30:13 +00:00
--server 'https://acme-v02.api.letsencrypt.org/directory' \
--config-dir ~/acme/etc \
2016-04-19 04:29:03 +00:00
--agree-tos --domains example.com --email user@example.com
# Note: the '--webrootPath' option is also available if you don't want to shut down your webserver to get the cert.
```
### Part 2: Just add Hapi
```javascript
var hapi = require('hapi');
var https = require('spdy');
var server = new hapi.Server();
2016-08-16 20:41:18 +00:00
var acmeResponder = le.middleware();
var httpsServer = https.createServer(le.httpsOptions).listen(443);
2016-04-19 04:29:03 +00:00
server.connection({ listener: httpsServer, autoListen: false, tls: true });
server.route({
method: 'GET'
, path: '/.well-known/acme-challenge'
, handler: function (request, reply) {
var req = request.raw.req;
var res = request.raw.res;
reply.close(false);
acmeResponder(req, res);
}
});
server.route({
method: 'GET'
, path: '/'
, handler: function (request, reply) {
reply("Hello, I'm so Hapi!");
}
});
```
### Part 3: Redirect http to https
```javascript
var http = require('http');
2016-08-16 20:41:18 +00:00
var redirectHttps = require('redirect-https')();
2016-04-19 04:29:03 +00:00
2016-08-16 20:41:18 +00:00
http.createServer(le.middleware(redirectHttps)).listen(80, function () {
console.log('handle ACME http-01 challenge and redirect to https');
});
2016-04-19 04:29:03 +00:00
```