Ravindra BagaleCourses & study guides मराठी

Chapter 57: Docker Projects Part 1: Websites, Databases, LAMP, LEMP, WordPress, 3-Tier and Every Language with MySQL and MongoDB

DevOps

57.4 Project 4: WordPress with MySQL, nginx and free HTTPS from Let's Encrypt

Friends, today we put WordPress behind nginx with MySQL and a free HTTPS certificate. First plain HTTP on a domain, then certbot, then HTTPS redirect, renew, backup and update. Type every command yourself.

What you will learn in this lesson

  1. Why WordPress needs MySQL, PHP-FPM and nginx together.
  2. How volumes keep uploads and the database across updates.
  3. How Let's Encrypt proves you own a domain with a webroot challenge.
  4. How to switch from HTTP to HTTPS and renew the certificate.
  5. How to back up and update WordPress with a new image tag.

1. Start from a real problem

Suppose a Pune bakery, like a small Chitale-style shop, asks us for a blog and order page. WordPress powers a large share of websites, so it is the natural choice.

A normal install needs nginx, PHP with the right extensions, MySQL, file permissions, and an SSL certificate that expires every 90 days. Each step can break a manual setup.

With Docker we run four containers: MySQL, WordPress PHP-FPM, nginx and certbot. DigitalOcean and many hosts teach this same pattern for one-server WordPress.

BrowserHTTPS to the domainnginxTLS + reverse proxywordpress:fpmPHP on door 9000MySQL 8.4posts and users
Picture 1: the four containers

2. Domain, folder and secrets

Step 1: Point the domain

Why: Let's Encrypt must reach your server.

What: Create an A record like blog.example.com to the public IP of your Ubuntu VM. Without a real domain, practice the HTTP part on localhost and skip the certificate steps.

Example 1: Check DNS from the VM

Type this:

dig +short blog.example.com

You will see:

15.252.55.24

What it means: The A record should match this VM public IP.

Example 2: Make project folders

Type this:

mkdir -p wp/nginx/conf.d wp/certbot/www wp/certbot/conf && cd wp && pwd

You will see:

/home/you/wp

What it means: Folders for nginx config and certificate files.

Example 3: Write .env

Type this:

printf '%s\n' 'DOMAIN=blog.example.com' 'EMAIL=admin@example.com' 'DB_NAME=wordpress' 'DB_USER=wp' 'DB_PASSWORD=change-me-wp' 'DB_ROOT_PASSWORD=change-me-root' > .env && wc -l .env

You will see:

6 .env

What it means: Domain, email and database passwords in one file.

Step 2: Ignore secrets

Why: Keep Git clean.

What: Never commit .env or the live certificate private keys.

Example 4: Write .gitignore

Type this:

printf '%s\n' '.env' 'certbot/conf/' 'backups/' > .gitignore && cat .gitignore

You will see:

.env
certbot/conf/
backups/

What it means: Secrets and certs stay local.

Example 5: Show domain from env

Type this:

grep DOMAIN .env

You will see:

DOMAIN=blog.example.com

What it means: Compose will read this value.

Example 6: Confirm folders

Type this:

find . -type d | sort

You will see:

.
./certbot
./certbot/conf
./certbot/www
./nginx
./nginx/conf.d

What it means: Tree ready for Compose.

3. Compose and HTTP nginx

Step 3: compose.yaml

Why: Four services, two volumes.

What: db holds MySQL data. wordpress runs PHP-FPM. nginx is the public door. certbot only runs when we ask for a certificate.

Example 7: Write compose.yaml

Type this:

printf '%s\n' 'services:' '  db:' '    image: mysql:8.4' '    environment:' '      MYSQL_DATABASE: ${DB_NAME}' '      MYSQL_USER: ${DB_USER}' '      MYSQL_PASSWORD: ${DB_PASSWORD}' '      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}' '    volumes:' '      - db-data:/var/lib/mysql' '    healthcheck:' '      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-p${DB_ROOT_PASSWORD}"]' '      interval: 5s' '      retries: 20' '' '  wordpress:' '    image: wordpress:6.6-fpm' '    environment:' '      WORDPRESS_DB_HOST: db' '      WORDPRESS_DB_USER: ${DB_USER}' '      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}' '      WORDPRESS_DB_NAME: ${DB_NAME}' '    volumes:' '      - wp-content:/var/www/html' '    depends_on:' '      db:' '        condition: service_healthy' '' '  nginx:' '    image: nginx:1.27-alpine' '    ports:' '      - "80:80"' '      - "443:443"' '    volumes:' '      - wp-content:/var/www/html:ro' '      - ./nginx/conf.d:/etc/nginx/conf.d:ro' '      - ./certbot/www:/var/www/certbot:ro' '      - ./certbot/conf:/etc/letsencrypt:ro' '    depends_on: [wordpress]' '' '  certbot:' '    image: certbot/certbot:v2.11.0' '    volumes:' '      - ./certbot/www:/var/www/certbot' '      - ./certbot/conf:/etc/letsencrypt' '' 'volumes:' '  db-data:' '  wp-content:' > compose.yaml && wc -l compose.yaml

You will see:

48 compose.yaml

What it means: Four services and two named volumes.

Example 8: Count image pins

Type this:

grep -cE 'image: (mysql|wordpress|nginx|certbot)' compose.yaml

You will see:

4

What it means: All four images are pinned.

Example 9: Show WordPress DB host

Type this:

grep WORDPRESS_DB_HOST compose.yaml

You will see:

WORDPRESS_DB_HOST: db

What it means: PHP reaches MySQL by the service name db.

Step 4: HTTP nginx config

Why: Serve PHP and the ACME folder.

What: nginx passes .php files to wordpress:9000. It also serves /.well-known/acme-challenge/ from a shared folder so Let's Encrypt can prove the domain.

Example 10: Write HTTP site config

Type this:

printf '%s\n' 'server {' '    listen 80;' '    server_name blog.example.com;' '    root /var/www/html;' '    index index.php;' '' '    location /.well-known/acme-challenge/ {' '        root /var/www/certbot;' '    }' '' '    location / {' '        try_files $uri $uri/ /index.php?$args;' '    }' '' '    location ~ \.php$ {' '        include fastcgi_params;' '        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' '        fastcgi_pass wordpress:9000;' '    }' '}' > nginx/conf.d/blog.conf && wc -l nginx/conf.d/blog.conf

You will see:

20 nginx/conf.d/blog.conf

What it means: HTTP server with PHP and ACME locations.

Example 11: Show the FPM pass

Type this:

grep fastcgi_pass nginx/conf.d/blog.conf

You will see:

fastcgi_pass wordpress:9000;

What it means: PHP goes to the wordpress service on door 9000.

Example 12: Show the ACME root

Type this:

grep -A1 acme-challenge nginx/conf.d/blog.conf

You will see:

location /.well-known/acme-challenge/ {
    root /var/www/certbot;

What it means: certbot and nginx share this folder.

4. Start on HTTP and install WordPress

Step 5: Bring the stack up

Why: HTTP first, always.

What: Start db, wordpress and nginx. Open the domain in a browser and finish the WordPress install form.

Example 13: Start services

Type this:

docker compose up -d db wordpress nginx

You will see:

[+] Running 5/5
 ✔ Volume wp_db-data     Created
 ✔ Volume wp_wp-content  Created
 ✔ Container wp-db-1     Healthy
 ✔ Container wp-wordpress-1  Started
 ✔ Container wp-nginx-1  Started

What it means: MySQL healthy. WordPress and nginx running.

Example 14: Test HTTP status

Type this:

curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: blog.example.com' http://127.0.0.1/

You will see:

302

What it means: WordPress redirects to the install or login page.

Example 15: Confirm PHP-FPM answers

Type this:

docker compose exec wordpress php -v | head -1

You will see:

PHP 8.2.24 (fpm-fcgi) ...

What it means: PHP inside the wordpress container is ready.

Step 6: Finish the web installer

Why: Create the admin user in the browser.

What: Open http://blog.example.com, choose the language, set the site title and a strong admin password. After that the home page loads.

Example 16: Hit the installer path

Type this:

curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: blog.example.com' http://127.0.0.1/wp-admin/install.php

You will see:

200

What it means: Installer page is reachable.

Example 17: See wp-content volume

Type this:

docker volume inspect wp_wp-content --format '{{.Mountpoint}}'

You will see:

/var/lib/docker/volumes/wp_wp-content/_data

What it means: Themes, plugins and uploads will live here.

Example 18: Count tables after install

Type this:

docker compose exec -e MYSQL_PWD=change-me-wp db mysql -uwp wordpress -e 'SHOW TABLES;' | wc -l

You will see:

13

What it means: About a dozen WordPress tables after a successful install.

5. Get the certificate and switch to HTTPS

certbotwrites a token filenginx serves/.well-known/acme-challenge/Let's Encryptfetches the file and issues the cert
Picture 2: webroot challenge

Step 7: Request the certificate

Why: Use staging first.

What: Staging avoids rate limits while you practice. When staging works, run the same command without --staging for the real certificate.

Example 19: Create ACME webroot

Type this:

mkdir -p certbot/www/.well-known/acme-challenge && echo ok > certbot/www/.well-known/acme-challenge/ping && curl -s -H 'Host: blog.example.com' http://127.0.0.1/.well-known/acme-challenge/ping

You will see:

ok

What it means: nginx can serve challenge files.

Example 20: Staging certificate

Type this:

docker compose run --rm certbot certonly --webroot -w /var/www/certbot -d blog.example.com --email admin@example.com --agree-tos --no-eff-email --staging

You will see:

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/blog.example.com/fullchain.pem

What it means: Staging cert issued. Files land under certbot/conf.

Example 21: List cert files

Type this:

ls certbot/conf/live/blog.example.com/

You will see:

cert.pem
chain.pem
fullchain.pem
privkey.pem
README

What it means: Full chain and private key are ready.

Step 8: HTTPS nginx config

Why: Redirect 80 to 443.

What: Add a 443 server block that uses the certificate files. Keep the ACME location on port 80 so renewals still work.

Example 22: Write HTTPS config

Type this:

printf '%s\n' 'server {' '    listen 80;' '    server_name blog.example.com;' '    location /.well-known/acme-challenge/ { root /var/www/certbot; }' '    location / { return 301 https://$host$request_uri; }' '}' '' 'server {' '    listen 443 ssl;' '    server_name blog.example.com;' '    ssl_certificate     /etc/letsencrypt/live/blog.example.com/fullchain.pem;' '    ssl_certificate_key /etc/letsencrypt/live/blog.example.com/privkey.pem;' '    root /var/www/html;' '    index index.php;' '    location / { try_files $uri $uri/ /index.php?$args; }' '    location ~ \.php$ {' '        include fastcgi_params;' '        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' '        fastcgi_pass wordpress:9000;' '    }' '}' > nginx/conf.d/blog.conf && wc -l nginx/conf.d/blog.conf

You will see:

22 nginx/conf.d/blog.conf

What it means: Port 80 redirects. Port 443 serves PHP over TLS.

Example 23: Reload nginx

Type this:

docker compose exec nginx nginx -t && docker compose exec nginx nginx -s reload

You will see:

nginx: configuration file ... test is successful

What it means: New config is live without recreating the container.

Example 24: Test HTTPS redirect

Type this:

curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' -H 'Host: blog.example.com' http://127.0.0.1/

You will see:

301 https://blog.example.com/

What it means: HTTP now sends browsers to HTTPS.

6. Renew, backup, update

Step 9: Renew the certificate

Why: Certificates last 90 days.

What: certbot renew checks every cert and renews those close to expiry. Reload nginx after a successful renew.

Example 25: Dry-run renew

Type this:

docker compose run --rm certbot renew --dry-run

You will see:

Congratulations, all simulated renewals succeeded

What it means: Renew path works. Schedule this twice a day with cron on a real server.

Example 26: Show expiry

Type this:

docker compose run --rm certbot certificates 2>/dev/null | grep -E 'Expiry|Domains' || ls certbot/conf/live/blog.example.com/fullchain.pem

You will see:

Domains: blog.example.com
Expiry Date: ...

What it means: You can see the domain and expiry date.

Example 27: Reload after renew

Type this:

docker compose exec nginx nginx -s reload && echo reloaded

You will see:

reloaded

What it means: nginx picks up new cert files from the same paths.

Step 10: Backup and update

Why: Volumes make updates safe.

What: Dump MySQL and copy wp-content. To update WordPress, change the image tag and recreate the wordpress service. Uploads stay in the volume.

Example 28: Backup database

Type this:

mkdir -p backups && docker compose exec -e MYSQL_PWD=change-me-root db mysqldump -uroot wordpress > backups/wp-$(date +%F).sql && wc -l backups/wp-*.sql | tail -1

You will see:

180 backups/wp-2026-10-28.sql

What it means: SQL dump saved on the host.

Example 29: Update WordPress image

Type this:

sed -i 's/wordpress:6.6-fpm/wordpress:6.7-fpm/' compose.yaml && docker compose up -d wordpress

You will see:

... Pull complete
Container wp-wordpress-1  Recreated
Container wp-wordpress-1  Started

What it means: New PHP image. wp-content volume still attached.

Example 30: Confirm site after update

Type this:

curl -s -o /dev/null -w '%{http_code}\n' -k -H 'Host: blog.example.com' https://127.0.0.1/

You will see:

200

What it means: Site still answers after the image change.

7. Common mistakes and clean up

Step 11: Common mistakes

Why: Avoid rate limits and lost uploads.

What: Never request production certificates in a loop. Never put uploads outside the volume. Never forget the ACME location when you switch to HTTPS.

Example 31: Staging before production

Type this:

grep -c staging <<<'use --staging first' || true; echo 'rule: staging-then-prod'

You will see:

rule: staging-then-prod

What it means: Staging mistakes do not burn the production rate limit.

Example 32: Prove uploads volume

Type this:

docker compose exec wordpress sh -c 'ls /var/www/html/wp-content | head'

You will see:

themes
plugins
uploads

What it means: wp-content is the volume mount. Uploads survive updates.

Example 33: ACME still on port 80

Type this:

grep acme-challenge nginx/conf.d/blog.conf

You will see:

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

What it means: Renewals need this location forever.

Step 12: Clean up

Why: Remove practice resources.

What: On a throwaway VM, down -v removes the database and uploads. On a real bakery site, take a backup first and never pass -v by accident.

Example 34: Stop the stack

Type this:

docker compose down

You will see:

Container wp-nginx-1  Removed
Container wp-wordpress-1  Removed
Container wp-db-1  Removed

What it means: Containers gone. Volumes still present without -v.

Example 35: Remove volumes only when sure

Type this:

docker compose down -v

You will see:

Volume wp_db-data  Removed
Volume wp_wp-content  Removed

What it means: Practice data deleted.

Example 36: Remove local cert practice files

Type this:

rm -rf certbot/conf/* certbot/www/* backups/* && echo cleaned

You will see:

cleaned

What it means: Local cert and dump files removed.

Ravindra Bagale's Tip

Always get a staging certificate first. Let's Encrypt production has rate limits. One wrong loop can lock your domain for a week. Hosting teams at GoDaddy and Cloudflare teach the same staging-first habit.

Check yourself

Q1. Why run wordpress:fpm behind nginx?

nginx handles TLS and static files. PHP-FPM runs PHP on door 9000.

Q2. What does the webroot challenge prove?

Let's Encrypt fetches a file from your domain to prove you control it.

Q3. Where do uploads live so updates are safe?

In the wp-content named volume mounted at /var/www/html.

Q4. Why keep an ACME location on port 80 after HTTPS?

Certificate renewals still need to serve challenge files over HTTP.

Q5. How do you update WordPress with Docker?

Change the image tag in compose.yaml and recreate the wordpress service.

Q6. What should you do before a production cert request?

Succeed with a --staging request first.

Friends, you ran WordPress with MySQL, nginx, a staging certificate, HTTPS redirect, renew, backup and an image update. Did you understand? Let us try it once more. If not, look at Pictures 1 and 2 again and repeat the HTTP-then-HTTPS steps. Next project: Python FastAPI with MySQL, MongoDB and PostgreSQL.

Lab: try at home

  1. Point a domain A record at your VM (or practice HTTP on localhost).
  2. Write .env, compose.yaml and the HTTP nginx config.
  3. Start db, wordpress and nginx and finish the installer.
  4. Prove the ACME webroot path serves a test file.
  5. Request a staging certificate with certbot.
  6. Switch nginx to HTTPS and test the redirect.
  7. Dry-run renew and take a MySQL backup.
  8. Update the WordPress tag and confirm the site still answers.
  9. Clean up with down -v on a practice VM.

Recap

  • WordPress needs MySQL, PHP-FPM and nginx together.
  • Volumes keep uploads and the database across updates.
  • Get a staging certificate before production.
  • Keep ACME on port 80 for renewals.
  • Backup MySQL before every risky change.
  • Update by changing the image tag, not by editing inside the container.

Ravi Sir's Cards for this lesson

मराठी cards