Demo
There is a ready to use git repository on github. clone and follow instruction to setup.
Create dummy web service to test the result. you may remove these docker container later on.
|
|
If you access the localhost with curl http://localhost:8080 you will receive message like hello world from ...
Clone project and initialize the proxy to port 8080 after the certificate signed.
For example, let’s assume the domain name is www.example.com and use the certbot proxy prot is 53388
|
|
After the above shell script execution complete, we can access hello-world-app through https (curl https://www.example.com) in the internet.
Prequisite
- docker is available
- User have full control of the domain (Can manipulate the DNS record)
- the target server is opening the port 80 (http), 443 (https) to internet
Explanination
We have followed the demo to setup a SSL certificate for the our domain name (www.example.com).
Now I’m going to go explaine the detail of what does the first-init.sh do.
Architecture
Below diagram shows a very simple version of how the let’s encrypt and nginx work togather to complete the SSL certificate signing process. \ For detailed information about the ACME, please go throught rfc8555.
Steps
- Start up certbot in standalone mode The certbot would access to ACME Server for request signing certificate
- ACME server request for challenge to proof the onwnership of the domain
- ACME server reache the target host for verify the ownership of domain
- ACME sign the SSL certificate when the verification complete successfully
Scripts
when execute first-init.sh, the script:
-
create a custom nginx configuration for domain (www.example.com) using
./conf/half_conf_template. Any request towww.example.com/.well-known/acme-challenge/will be route to the port 53388. start the withdocker compose up -d1 2 3 4 5 6 7 8 9 10server { listen 80; server_name {hostname}; location /.well-known/acme-challenge/ { proxy_pass http://mainhost:{port}; } #return 301 https://{hostname}; } -
start up certbot with standalone mode with port 53388 and perform ACME http verification. (
certbox_template)1 2 3 4 5 6 7 8 9 10docker run \ -p 53388:80 \ -v "{dir}/letsencrypt:/etc/letsencrypt" \ certbot/certbot \ certonly \ --standalone --email [email protected]\ --agree-tos \ --preferred-challenges http \ --keep-until-expiring \ -d www.example.com -
the ACME server verify the domain name by access
www.example.com/.well-known/acme-challenge/ -
After the SSL certificate signed, replace the nginx config with
./conf/full_conf_template1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25server { listen 80; server_name www.example.com; location /.well-known/acme-challenge/ { proxy_pass http://mainhost:53388; } #return 301 https://{hostname}; } server { listen 443 ssl; server_name www.example.com; ssl_certificate /letsencrypt/live/www.example.com/fullchain.pem; ssl_certificate_key /letsencrypt/live/www.example.com/privkey.pem; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://mainhost:8080; } } -
Start the docker compose again with the new configuration.
Renew
Since the let’s encrypt generated certificate only available for 90 day, we could renew the certificate with Certbot scheduled as daily.
The Certbot is intelligent enough to only renew the certificate that is about to be expired and renew them without affecting other certificates.
The renew.sh serve the purpose to renew certificate and reload the nginx server.
See the script below:
|
|
Add crontab job as below, the Certbot renewal will be triggered every day on 5:18 pm.
|
|