How to

How to Install Nginx and Let’s Encrypt with Docker Ubuntu 20.04

How to Install Nginx and Let's Encrypt with Docker Ubuntu 20.04

How to Install Nginx and Let’s Encrypt with Docker – Ubuntu 20.04. In this guide you will learn how to install and configure Nginx with Let’s Encrypt SSL using Docker and Docker Compose on Ubuntu 20.04.

In this tutorial we will use the latest Nginx images and the latest Certbot images and validate settings and finally configure SSL with a basic HTML site that is presented using Nginx.

These installations and settings are tested on the Google Cloud Compute Engine running Ubuntu 20.04 with Docker and Docker Compose. So this arrangement will work on other cloud service providers such as AWS or Digital Ocean or Azure or VPS or other special servers.

After all the prerequisites are complete, you can continue to make settings and install SSL.

Create a Docker Write a YML file

Now SSH on your server or Virtual machine and create a directory to save all configurations by running the following command.

sudo mkdir ~/nginx-ssl

Move into the directory and create a docker-compose.yml file that holds our configuration.

cd ~/nginx-ssl
sudo nano ~/nginx-ssl/docker-compose.yml

Paste the following configuration in the file.

version: "3.8"
services:
    web: 
        image: nginx:latest
        restart: always
        volumes:
            - ./public:/var/www/html/domain-name/public
            - ./conf.d:/etc/nginx/conf.d
            - ./certbot/conf:/etc/nginx/ssl
            - ./certbot/data:/var/www/certbot
        ports:
            - 80:80
            - 443:443

    certbot:
        image: certbot/certbot:latest
        command: certonly --webroot --webroot-path=/var/www/certbot --email your-email@domain.com --agree-tos --no-eff-email -d domain.com -d www.domain.com
        volumes:
            - ./certbot/conf:/etc/letsencrypt
            - ./certbot/logs:/var/log/letsencrypt
            - ./certbot/data:/var/www/certbot

Press CTRL-X followed by Y and ENTER to save and exit the file.

The following configuration details.

  • version: Compile a file version that is compatible with Docker Engine. You can check compatibility.
  • image: We use the latest Nginx and Certbot images available on the Docker hub.
  • volume:
  • public: we have configured this directory to synchronize with the directory that we want to use as the web root in the container.
  • conf.d: here we will place the Nginx configuration file to synchronize with the default Nginx conf folder in the container.
  • cedtbot / conf: this is where we will receive an SSL certificate and this will synchronize with the folder that we want to be in the container.
  • ports: configure the container to listen for the registered port.
    command: this command is used to receive an SSL certificate.

Now you have docker-compose.yml in its place.
Configure Nginx

Now we need to configure Nginx for validation to get the SSL Let’s Let’s Encrypt certificate.

We will create the directory as mentioned in the docker-compose file as conf.d.

sudo mkdir ~/nginx-ssl/conf.d

Create a configuration file with the .conf extension.

sudo nano ~/nginx-ssl/conf.d/default.conf

Paste the following configuration in the file.

server {
     listen [::]:80;
     listen 80;

     server_name domain.com www.domain.com;

     location ~ /.well-known/acme-challenge {
         allow all; 
         root /var/www/certbot;
     }
}

Press CTRL-X followed by Y and ENTER to save and exit the file.

Now you have the Nginx configuration synchronized to the /etc/nginx/conf.d folder which is automatically loaded by Nginx.

Start Container

Now it’s time to start the container using the following command to receive an SSL certificate.

You must pass the -d flag which starts the container in the background and lets it run.

docker-compose up -d

You will see an output similar to the one below.

Output
Creating network "nginx-ssl_default" with the default driver
Pulling web (nginx:latest)…
latest: Pulling from library/nginx
8559a31e96f4: Pull complete
8d69e59170f7: Pull complete
3f9f1ec1d262: Pull complete
d1f5ff4f210d: Pull complete
1e22bfa8652e: Pull complete
Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133
Status: Downloaded newer image for nginx:latest
Pulling certbot (certbot/certbot:latest)…
latest: Pulling from certbot/certbot
cbdbe7a5bc2a: Pull complete
26ebcd19a4e3: Pull complete
a29d43ca1bb4: Pull complete
979dbbcf63e0: Pull complete
30beed04940c: Pull complete
48a1f8a4d505: Pull complete
4416e9b4bbe0: Pull complete
8173b4be7870: Pull complete
21c8dd124dab: Pull complete
c19b04e11dc7: Pull complete
1b560611cec1: Pull complete
Digest: sha256:568b8ebd95641a365a433da4437460e69fb279f6c9a159321988d413c6cde0ba
Status: Downloaded newer image for certbot/certbot:latest
Creating nginx-ssl_certbot_1 … done
Creating nginx-ssl_web_1     … done

This Nginx and Certbot image was pulled from the Docker hub and the container was successfully created.

To view the container, you can run the following command.

docker-compose ps
Output
       Name                      Command               State                     Ports
nginx-ssl_certbot_1   certbot certonly --webroot …   Exit 0                                           
nginx-ssl_web_1       /docker-entrypoint.sh ngin …   Up       0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

Exit 0 indicates that the setting is complete without errors.

Now when you check your working directory, there will be a new directory created as certbot where you will synchronize the SSL certificate.

ls ~/nginx-ssl/certbot/conf/live/domain.com

Configure SSL with Nginx

Because you have received the Let’s Let’s Encrypt SSL certificate, you can configure HTTPS and set up a switch to HTTPS.

Edit default.conf and make the following changes.

Your file should look like the one at the end.

server {
    listen [::]:80;
    listen 80;

    server_name domain.com www.domain.com;

    location ~ /.well-known/acme-challenge {
        allow all; 
        root /var/www/certbot;
    }

    # redirect http to https www
    return 301 https://www.domain.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name domain.com;

    # SSL code
    ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem;

    root /var/www/html/domain-name/public;

    location / {
        index index.html;
    }

    return 301 https://www.domain.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name www.domain.com;

    # SSL code
    ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem;

    root /var/www/html/domain-name/public;

    location / {
        index index.html;
    }
}

Press CTRL-X followed by Y and ENTER to save and exit the file.

Create index.html file

Now you can create an index.html file in the public directory which is then synchronized to the directory that is configured.

Create a public directory.

sudo mkdir ~/nginx-ssl/public
sudo nano ~/nginx-ssl/public/index.html
<html>
    <body>
        <h1>Docker setup with Nginx and Let's Encrypt SSL.</h1>
    </body>
</html

Press CTRL-X followed by Y and ENTER to save and exit the file.

Restart the container

Now you can restart the container to load the new configuration.

docker-compose restart

After the container starts again you can check your domain name. You will get a switch to your HTTPS and SSL.

Conclusion

Now you have learned how to install and configure Nginx and Let’s Encrypt with Docker on Ubuntu 20.04.

Thank you for your time. If you encounter a problem or feedback, please leave a comment below.

Related posts

How to Use Guest Mode on Chromecast

Howto

How to Translate Cells Automatically in Google Cell Sheet

Howto

How to Transfer PSP Videos to Memory Stick

Howto

How to set up VPN for Roku

Howto

How to Get Notifications for Only Emails You Care About in Microsoft Outlook

Howto

How to Fix Google Chrome Translate Not Working

Howto

How to Back Up Photos Uploaded to Google Photos

Howto

How to Extend Storage for Linux on your Chromebook

Howto

How to Enter Text in Microsoft Excel Graphics

Howto