Setup Nginx Web Server using Docker Compose and Let’s Encrypt SSL

Nginx (engine-X) is open-source software used for web server, reverse proxy, caching, load balancing, media streaming, and much more. Nginx is designed as a web server focused on maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and as a load balancer for HTTP, TCP, and UDP servers.

This time, we will install the Nginx web server using Docker Compose.

The first step is to generate an SSL certificate with Let’s Encrypt. Before running the command below, we have already installed Certbot in a previous step.

$ sudo certbot certonly --manual --preferred-challenges dns

Next, enter the subdomain that will be registered for accessing the web server. In the example below, we are using the subdomain named nginx.inetmede.com. After inputting the subdomain to be used, enter the text _acme-challenge.nginx as a TXT Record in the DNS Management. Once it has been added to the DNS Management, press Enter.

The next step is to create the installation folder.

$ mkdir -p nginx
$ mkdir -p nginx/ssl
$ mkdir -p nginx/conf
$ mkdir -p nginx/html
$ cd nginx

Create an index.html file for the display on the Nginx web server.

$ vi html/index.html
Testing Web Server Nginx and Docker Compose.

Then, create the Nginx configuration file named default.conf.

$ vi conf/default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  nginx.inetmede.com;

    #access_log  /var/log/nginx/host.access.log  main;

    location ~ /.well-known/acme-challenge {
        root   /var/www/html;
        index  index.html index.htm;
    }

    #error_page  404              /index.html;
    location / {
                rewrite ^ https://$host$request_uri? permanent;
    }
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name nginx.inetmede.com;

    location / {
        try_files $uri $uri/ /index.html;
        root   /var/www/html;
        index  index.html index.htm;
    }

    error_page  404              /index.html;

        server_tokens off;

        ssl_certificate /etc/ssl/fullchain.pem;
        ssl_certificate_key /etc/ssl/privkey.pem;
}

After configuring Nginx and generating the SSL certificate with Let’s Encrypt, it is now time to configure Docker Compose. Create a configuration file named docker-compose.yml.

$ vi docker-compose.yml
version: "3"
services:
    apps:
        image: nginx:alpine
        container_name: docker-nginx
        ports:
            - 80:80
            - 443:443
        volumes:
            - ./html/index.html:/var/www//html/index.html
            - ./conf/default.conf:/etc/nginx/conf.d/default.conf
            - ./ssl:/etc/ssl
        environment:
            - TZ=Asia/Jakarta

Then, run the command docker-compose up -d to start the Nginx installation using Docker Compose.

$ sudo docker-compose up -d

Next, try accessing https://nginx.inetmede.com in your web browser and check the status of the SSL certificate.

Leave a Reply

Your email address will not be published. Required fields are marked *