basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    A service over HTTPS: Traefik and the first service

    Bring up Traefik as its own stack and expose memos through it by name with a Let’s Encrypt certificate. The why is in the manuals, linked from every stage.

    Start froma server after level 2, “Protection”: key-only login, ufw lets in SSH, 80 and 443, Docker installed with capped logs.

    What you get

    internet ── 80, 443 ──▶ Traefik ─┬─▶ whoami.example.org   test service: shows what arrived
                                     └─▶ memos.example.org    notes; data in /opt/memos/data
    /opt/traefik/    the proxy stack: compose, traefik.yml, acme.json
    /opt/whoami/     the test service
    /opt/memos/      the first real service
    • You’ll needA domain: A records for whoami.example.org and memos.example.org pointing at the server, 203.0.113.10, without the Cloudflare proxy (grey cloud)
    • You’ll needAn email for the Let’s Encrypt account: [email protected]

    Your values

    Filled into the commands and files of this and other recipes on the path; stored in this browser only.
    Email
    [email protected]placeholder
    Domain
    example.orgplaceholder
    Server address
    203.0.113.10placeholder

    Without the script the commands show placeholder values: replace them with yours.

    Steps

    The same recipe, short: commands from the stages, files as one line. For a second server and for those who already know what each command does.

    DNS and ports

    Why — Traefik from scratch
    Names and ports
    Command What it does
    dig +short whoami.example.org where the test service’s name points
    dig +short memos.example.org where memos’ name points
    sudo ss -tlnp | grep -E ':(80|443) ' who holds ports 80 and 443
    Done when
    Command What you should see
    dig +short memos.example.org 203.0.113.10, the server’s address; the same for whoami
    sudo ss -tlnp | grep -E ':(80|443) ' nothing: the ports are free
    Not like this — see “If it doesn’t work”, stage cases.

    Traefik: network, directories, config, start

    Why — Traefik from scratch
    Network and directories
    Command What it does
    sudo docker network create proxy the shared network for the proxy and services, one per server
    sudo mkdir -p /opt/traefik/data/dynamic /opt/traefik/data/logs the stack’s directories
    sudo touch /opt/traefik/data/acme.json the certificate file — before the start, or Docker creates a directory in its place
    sudo chmod 600 /opt/traefik/data/acme.json Traefik rejects the file with wider permissions
    /opt/traefik/data/traefik.ymlstatic config; the email is for the Let’s Encrypt account
    global:
      checkNewVersion: false
      sendAnonymousUsage: false
    
    log:
      level: INFO
    
    accesslog:
      format: json
      filePath: /logs/access.log
    
    entryPoints:
      http:
        address: ":80"
        http:
          aliasHeadersStrategy: delete
          redirections:
            entryPoint:
              to: https
              scheme: https
      https:
        address: ":443"
        http:
          aliasHeadersStrategy: delete
          tls:
            certResolver: letsencrypt
    
    providers:
      docker:
        exposedByDefault: false
        network: proxy
      file:
        directory: /dynamic/
        watch: true
    
    certificatesResolvers:
      letsencrypt:
        acme:
          email: [email protected]
          storage: acme.json
          httpChallenge:
            entryPoint: http

    Write the email literally: ${…} isn’t substituted in this file (traefik.yml doesn’t substitute variables).

    /opt/traefik/docker-compose.yamlthe proxy stack
    services:
      traefik:
        image: traefik:v3.7
        container_name: traefik
        restart: unless-stopped
        security_opt:
          - no-new-privileges:true
        ports:
          - 80:80
          - 443:443
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock:ro
          - ./data/traefik.yml:/traefik.yml:ro
          - ./data/acme.json:/acme.json
          - ./data/dynamic/:/dynamic/:ro
          - ./data/logs/:/logs/
        networks:
          - proxy
    
    networks:
      proxy:
        external: true
    Start
    Command What it does
    cd /opt/traefik && sudo docker compose up -d starts the stack
    sudo docker logs traefik 2>&1 | grep -E 'ERR|WRN' errors and warnings in the log
    Done when
    Command What you should see
    curl -sI http://whoami.example.org | head -1 HTTP/1.1 308 Permanent Redirect: the HTTPS redirect works even before whoami exists
    sudo docker logs traefik 2>&1 | grep -E 'ERR|WRN' nothing
    Not like this — see “If it doesn’t work”, stage cases.

    whoami and memos by name

    Why — Traefik labels
    Stack directories
    Command What it does
    sudo mkdir -p /opt/whoami /opt/memos a directory per stack
    /opt/whoami/docker-compose.yamlthe test service at whoami.example.org
    services:
      whoami:
        image: traefik/whoami
        container_name: whoami
        restart: unless-stopped
        labels:
          - "traefik.enable=true"
          - "traefik.http.routers.whoami.rule=Host(`whoami.example.org`)"
          - "traefik.http.routers.whoami.entrypoints=https"
        networks:
          - proxy
    
    networks:
      proxy:
        external: true
    /opt/memos/docker-compose.yamlmemos at memos.example.org, port 5230
    services:
      memos:
        image: neosmemo/memos:stable
        container_name: memos
        restart: unless-stopped
        volumes:
          - ./data:/var/opt/memos
        labels:
          - "traefik.enable=true"
          - "traefik.http.routers.memos.rule=Host(`memos.example.org`)"
          - "traefik.http.routers.memos.entrypoints=https"
          - "traefik.http.services.memos.loadbalancer.server.port=5230"
        networks:
          - proxy
    
    networks:
      proxy:
        external: true

    The services have no ports:: from the outside they’re reachable only through Traefik, over the proxy network.

    Start
    Command What it does
    cd /opt/whoami && sudo docker compose up -d starts the test service
    cd /opt/memos && sudo docker compose up -d starts memos
    Done when
    Command What you should see
    curl -s https://whoami.example.org | grep -E '^(Host|X-Forwarded-Proto):' Host: whoami.example.org and X-Forwarded-Proto: https
    curl -s -o /dev/null -w '%{http_code}\n' https://memos.example.org 200
    Not like this — see “If it doesn’t work”, stage cases.

    Check from the outside

    Why — Traefik from scratch

    Run these from your own computer, not the server: that way the request takes the same route as a visitor’s. On Windows, openssl comes with Git Bash.

    From the outside
    Command What it does
    curl -sI http://whoami.example.org | head -3 HTTP sends you to HTTPS
    echo | openssl s_client -connect whoami.example.org:443 -servername whoami.example.org 2>/dev/null | openssl x509 -noout -issuer -enddate who issued the certificate and when it expires
    curl -m 5 -s http://203.0.113.10:5230 || echo closed whether memos’ port is open directly
    Done when
    Command What you should see
    curl -sI http://whoami.example.org | head -3 308 Permanent Redirect and Location: https://whoami.example.org/
    echo | openssl s_client -connect whoami.example.org:443 -servername whoami.example.org 2>/dev/null | openssl x509 -noout -issuer -enddate issuer says Let's Encrypt, notAfter is about 90 days from today
    curl -m 5 -s http://203.0.113.10:5230 || echo closed closed
    Not like this — see “If it doesn’t work”, stage cases.

    If it doesn’t work

    Stage 1

    dig +short prints nothing

    Cause
    There’s no A record yet, or DNS hasn’t caught up: a new record shows up within minutes, a changed one after the old record’s TTL.
    Fix
    Check the record at the registrar and wait; don’t go on — without DNS Let’s Encrypt won’t issue a certificate.
    Stage 1

    ss shows nginx or apache on :80 or :443

    Cause
    The ports are taken, and the Traefik container won’t start: Bind for 0.0.0.0:80 failed: port is already allocated.
    Fix
    sudo systemctl disable --now nginx (or apache2), then run ss again.
    Stage 2

    The log says is a directory about acme.json

    Cause
    The file didn’t exist on the first start, so Docker created a directory in its place (A missing volume path).
    Fix
    sudo docker compose down, sudo rmdir data/acme.json, then touch and chmod 600 from stage 2 again, sudo docker compose up -d.
    Stage 2

    The log says permissions 644 for /acme.json are too open, please use 600

    Cause
    The file was created with default permissions.
    Fix
    sudo chmod 600 /opt/traefik/data/acme.json and sudo docker compose restart traefik.
    Stage 3

    curl to a new name prints nothing, and curl -v complains about the certificate: TRAEFIK DEFAULT CERT

    Cause
    Traefik is still getting the certificate: the first request to a new name waits 10–30 s for it.
    Fix
    Try again in half a minute. Longer than that — sudo docker logs traefik 2>&1 | grep -i acme and the stage 4 case.
    Stage 3

    404 page not found on whoami or memos

    Cause
    traefik.enable=true is missing, the name in Host has a typo, or the container isn’t running.
    Fix
    sudo docker ps --filter name=memos, and compare the labels with the stage 3 file.
    Stage 3

    Gateway Timeout after about 30 seconds

    Cause
    The container isn’t on the proxy network: its compose file has no networks block.
    Fix
    Add the network as in the stage 3 files and run sudo docker compose up -d.
    Stage 4

    No certificate gets issued, and the log shows an acme error with connection or timeout

    Cause
    Let’s Encrypt couldn’t reach port 80: the provider’s firewall or a rule on the server blocks it.
    Fix
    Open http://whoami.example.org from a phone on mobile data — it should redirect. If port 80 can’t be opened, use the DNS challenge (Traefik and Let’s Encrypt).
    Stage 4

    The log says too many failed authorizations recently

    Cause
    Five failed validations per name per hour — after that Let’s Encrypt refuses until the hour is up.
    Fix
    Find the cause with the case above and wait an hour. While experimenting, use the staging server: caServer: https://acme-staging-v02.api.letsencrypt.org/directory in traefik.yml (Traefik from scratch).
    Stage 4

    Port 5230 answers from the outside

    Cause
    A ports: block appeared in the memos compose file: Docker publishes the port past ufw (Docker ports bypass the input chain).
    Fix
    Remove ports: and run cd /opt/memos && sudo docker compose up -d.

    How to remove it

    cd /opt/memos && sudo docker compose down
    cd /opt/whoami && sudo docker compose down
    cd /opt/traefik && sudo docker compose down
    sudo docker network rm proxy
    sudo rm -r /opt/memos /opt/whoami /opt/traefik

    The last line deletes the memos notes and the certificates too. Need the notes — copy /opt/memos/data first.

    Next on the path