Skip to content

Docker-compose stack for NGINX with Certbot (Let's Encrypt), featuring automatic certificate obtain/renewal, DNS/HTTP challenges, multi-domain support, subdomains, and advanced NGINX configurations.

License

Notifications You must be signed in to change notification settings

bybatkhuu/stack.nginx

Repository files navigation

NGINX Stack

MIT License GitHub Workflow Status GitHub release (latest SemVer)

This is a docker-compose stack for NGINX with Certbot (Let's Encrypt).

Features

  • NGINX - https://nginx.org
  • Let's Encrypt - https://letsencrypt.org
  • Certbot - https://certbot.eff.org
  • TLS/SSL certificates
  • Automatic certificate obtain
  • Automatic certificate renewal (checks every week)
  • DNS challenges [recommended]:
    • Cloudflare DNS
    • DigitalOcean DNS
    • GoDaddy DNS
    • AWS Route53
    • Google Cloud DNS
  • HTTP challenges:
    • Standalone
    • Webroot
  • Multiple domains per certificate
  • Subdomains:
    • Multiple subdomains per domain/certificate
    • Wildcard subdomains (only DNS challenges)
  • NGINX template configuration
  • Web server
  • Reverse proxy
  • Load balancer
  • Rate limiting
  • HTTP cache
  • HTTP header transformations
  • HTTP/2 and HTTPS
  • Basic authentication
  • Websockets
  • Docker and docker-compose

Getting started

1. Prerequisites

For DEVELOPMENT:

2. Download or clone the repository

2.1. Prepare projects directory (if not exists) in your server with public IP address:

# Create projects directory:
mkdir -pv ~/workspaces/projects

# Enter into projects directory:
cd ~/workspaces/projects

2.2. Follow one of the below options [A], [B] or [C]:

A. Clone the repository (for LOCAL, TEST, STAGING or PRODUCTION environments):

git clone https://github.com/bybatkhuu/stack.nginx.git && \
    cd stack.nginx

B. Clone with all submodules (for DEVELOPMENT environments - Gigachad developers):

git clone --recursive https://github.com/bybatkhuu/stack.nginx.git && \
    cd stack.nginx && \
    git submodule update --init --recursive && \
    git submodule foreach --recursive git checkout main

C. Download source code from releases page.

3. Configure environment

Tip

Skip this step, if you've already configured environment.

3.1. Configure .env file:

Important

Please, check environment variables!

# Copy .env.example file into .env file:
cp -v .env.example .env

# Edit environment variables to fit in your environment:
nano .env

3.2. Configure compose.override.yml file:

Important

Please, check arguments!

# Copy 'compose.override.[ENV].yml' file to 'compose.override.yml' file:
cp -v ./templates/compose/compose.override.[ENV].yml ./compose.override.yml
# For example, DEVELOPMENT environment:
cp -v ./templates/compose/compose.override.dev.yml ./compose.override.yml
# For example, STATGING or PRODUCTION environment:
cp -v ./templates/compose/compose.override.prod.yml ./compose.override.yml

# Edit 'compose.override.yml' file to fit in your environment:
nano ./compose.override.yml

3.3. Validate docker compose configuration:

Warning

If you get an error or warning, check your configuration files (.env or compose.override.yml).

./compose.sh validate
# Or:
docker compose config

4. Configure NGINX

Tip

Skip this step, if you've already configured NGINX.

Important

Please, check nginx configuration and best practices:

Use template files to configure NGINX:

# Copy template file into storage directory:
cp -v ./templates/nginx.conf/[TEMPLATE_BASENAME].conf.template ./volumes/storage/nginx/configs/templates/[CUSTOM_BASENAME].conf.template
# For example, Let's Encrypt HTTPS configuration for example.com domain:
cp -v ./templates/nginx.conf/example.com_https.lets.conf.template ./volumes/storage/nginx/configs/templates/example.com.conf.template

# Edit template file to fit in your nginx configuration:
nano ./volumes/storage/nginx/configs/templates/[CUSTOM_BASENAME].conf.template
# For example:
nano ./volumes/storage/nginx/configs/templates/example.com.conf.template

5. Run docker compose

Caution

If container names are conflicting, you should change project directory name (stack.nginx) from 2.2. step.

./compose.sh start -l
# Or:
docker compose up -d --remove-orphans --force-recreate && \
    docker compose logs -f --tail 100

6. Check service is running

./compose.sh list
# Or:
docker compose ps

Check certificates:

./compose.sh certs
# Or check certificates in container:
docker compose exec certbot certbot certificates
# Or check certificates in host:
ls -alhF ./volumes/storage/nginx/ssl
# Or check certificates in host with tree:
tree -alFC --dirsfirst -L 5 ./volumes/storage/nginx/ssl

7. Stop docker compose

./compose.sh stop
# Or:
docker compose down --remove-orphans

👍 ✨


Environment Variables

You can use the following environment variables to configure:

.env.example

## --- CERTBOT configs --- ##
CERTBOT_EMAIL=user@email.com
CERTBOT_DOMAINS="example.com,www.example.com"
CERTBOT_DNS_TIMEOUT=30


## --- NGINX configs --- ##
NGINX_BASIC_AUTH_USER=nginx_admin
NGINX_BASIC_AUTH_PASS="NGINX_ADMIN_PASSWORD123" # !!! CHANGE THIS TO RANDOM PASSWORD !!!

Arguments

You can use the following arguments to configure:

nginx:

-s=*, --https=[self | valid | lets]
    Enable HTTPS mode:
        self  - Self-signed certificate
        valid - Valid certificate
        lets  - Let's Encrypt certificate
-b, --bash, bash, /bin/bash
    Run only bash shell.

For example as in compose.override.yml file:

    command: ["--https=self"]
    command: ["--https=valid"]
    command: ["--https=lets"]
    command: ["/bin/bash"]

certbot:

-s=, --server=[staging | production]
    Let's Encrypt server. Default: staging.
-n=, --new=[standalone | webroot]
    Obtain option for new certificates. Default: standalone.
-r=, --renew=[webroot | standalone]
    Renew option for existing certificates. Default: webroot.
-d=, --dns=[cloudflare | route53 | google | godaddy | digitalocean]
    Use DNS challenge instead of HTTP challenge.
-D, --disable-renew
    Disable automatic renewal of certificates.
-b, --bash, bash, /bin/bash
    Run only bash shell.

For example as in compose.override.yml file:

    command: ["--server=production"]
    command: ["--server=production", "--renew=standalone"]
    command: ["--new=webroot", "--disable-renew"]
    command: ["--server=production", "--dns=cloudflare"]
    command: ["--dns=digitalocean"]
    command: ["--dns=route53"]
    command: ["--dns=google"]
    command: ["--dns=godaddy"]
    command: ["/bin/bash"]

Roadmap

  • Add more DNS providers.
  • Add more documentation.

References