NGINX and HTTPs with Let’s Encrypt, Certbot, and Cron dockerization in production
Docker is a popular open source containerization platform and it frees your hands to build your applications in development and production. In this post, I'm going to walk you through how to build a production grade HTTPs secured Nginx server with Docker, Docker Compose, Let’s Encrypt(its client certbot). Let’s Encrypt certificates last 90 days and will need to be renewed after the certificate expires. So I will also provide details to script the renewal in crontab in Docker container.
1. Basic Example
In development, we need a basic Nginx container without HTTPs to fast setup our local test environment. I use Nginx official docker image and wrap up all the stuff with docker-compose.
I choose to use nginx.conf along with conf.d folder to manage all the configurations. So nginx.conf is for generic configuration while conf.d folder is for site specific configurations like below.
2. Configure HTTPs
2.1 Let’s Encrypt
To enable HTTPS on your website, you need to get a certificate from a Certificate Authority (CA). Let’s Encrypt is a free, automated, and open certificate authority (CA). In order to get a certificate for your website’s domain from Let’s Encrypt, you have to demonstrate control over the domain. With Let’s Encrypt, you do this using software that uses the ACME protocol which typically runs on your web host.
2.1 Certbot
we recommend using Certbot needs port 80 to be enabled, so the host firewall should allow incoming traffic on port 80 (HTTP) from anywhere. I'm using Oracle cloud, I need to open up port 80 on the security list, and also the VM firewall(as below):certbot-auto
, which automates the process of installing Certbot on your system.The certbot-auto
wrapper script installs Certbot, obtaining some dependencies from your web server OS and putting others in a python virtual environment. You can download and run it as follows. In addition,
2.3 Setup NGINX
We need configure ports, domain names, certificates as well as reverse proxy mappings for the servers. Here is a quick example grabbed from my project which contains two servers: one is for HTTPs and another is for HTTP that will redirect to HTTPs.
2.4 Automatically Renew Certificates
We just need to add the following script to crontab, which will run monthly to check and renew the certificate.
2.5 Wrap all in Docker
We will need a Dockerfile and docker-compose.yml.
Dockerfile
docker-compose.yml
We use network mode - host at the time of docker build so that it can share host network, which is quite tricky because the port mapping(80,443) are not ready at building phrase. Otherwise, running certbot-auto will fail due to HTTP port 80 is not reachable.
That's it. You're good to go. Contact me if you need whole source code.
Hi Kunkka, your articles are very insightful, and I really like them.
ReplyDeleteWould you please expand the size of the code boxes so that the contents are fully displayed? Thanks.
I'm not aware of that. You can check my post in DZone which should be good.
Deletehttps://dzone.com/articles/nginx-and-https-with-lets-encrypt-certbot-and-cron
Hey Kunkka, thanks for your time, it's great article. I want to ask some question it's possible to have multiple configs inside the same nginx.conf?
ReplyDeleteFor example:
I have 2 domain, example.com & api.example.com and I want to create SSL certificate for both. Can u provide some example with multiple websites configs inside of single nginx.conf?
Yes you can put the two conf files inside config.d folder. Here is the folder tree of one of my NGINX in production
Delete├── Dockerfile
├── config
│ ├── conf.d
│ │ ├── local
│ │ │ ├── default.conf
│ │ │ └── strapi.conf
│ │ └── prod
│ │ ├── default.conf
│ │ └── strapi.conf
│ └── nginx.conf
└── html
└── default
I have two comments:
ReplyDelete1. It looks like you are running nginx as a root, which is not advides. Is it correct?
2. Nginx needs to communicate to the web server which is running in my case not on a host network, but on a network created by the docker-compose. How to not attach the nginx to the host network?
Only use host network in building phase.
DeleteDear Kunkka, when I used your guide, to build I have getting error
ReplyDelete```
93.17 Domain: xxxxx.com
93.17 Type: connection
93.17 Detail: Fetching
93.17 http://xxxxx.com/.well-known/acme-challenge/_jrChASDFhpV9ormavMcEOA_M2fz0MAi_NYpurs_XjI:
93.17 Error getting validation data
```
These errors are most probably related to firewall.
Deletesimilar error when building!
ReplyDeleteFailed authorization procedure. www.example.com (http-01): urn: ietf: params: acme: error: connection :: The server could not connect to the client to verify the domain :: Fetching http://www.example.com/. well-known / acme-challenge / - 8pR7yFxZ2qm1WKURHffhMCKc7ZWKP9VX8mt6nMfh8: Connection refused
Looking in /var/log/letsencrypt/letsencrypt.log I found:
certbot.errors.StandaloneBindError: Problem binding to port 80: Could not bind to IPv4 or IPv6.
I wonder how the standalone webserver for authentication is going to connect port 80 if the container is not up yet?
Hi mate,
Deleteyou should modify www.example.com to your own domain