Building a WordPress blog or website (Docker)
WordPress is a program that lets you build a blog or website without coding. Here's how to start WordPress and its DB together with Docker Compose, then connect a domain and HTTPS.
What you need
- A server with 2GB of memory or more (Ubuntu)
- A domain (pointed at the server IP — see "Connect your domain" in Putting up a website)
- Docker (Installing and using Docker)
1. Create the configuration file
mkdir ~/wordpress && cd ~/wordpress
nano compose.yaml
Paste in the content below and change the two passwords to long, complex values of their own. (db-password must be the same value in both places.)
services:
db:
image: mariadb:11
restart: unless-stopped
environment:
MARIADB_DATABASE: wordpress
MARIADB_USER: wordpress
MARIADB_PASSWORD: db-password
MARIADB_ROOT_PASSWORD: root-password
volumes:
- db:/var/lib/mysql
wordpress:
image: wordpress:latest
restart: unless-stopped
depends_on:
- db
ports:
- "127.0.0.1:8080:80"
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: db-password
WORDPRESS_DB_NAME: wordpress
volumes:
- wp:/var/www/html
volumes:
db:
wp:
The DB doesn't open any port at all, and WordPress is bound only to
127.0.0.1. From outside, it can only be reached through Nginx, set up below.
docker compose up -d
2. Connect your domain with Nginx
If you don't have Nginx, install it. (sudo apt install -y nginx)
sudo nano /etc/nginx/sites-available/example.com
Replace example.com with your domain.
server {
listen 80;
server_name example.com www.example.com;
client_max_body_size 64M;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
3. Add HTTPS
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
If you use the firewall (ufw), open 80 and 443. (Opening firewall ports)
4. The WordPress installation screen
Go to https://mydomain in your browser and you'll see the language selection screen. Choose Korean, set the site title and the admin username and password, and you're done. The admin screen is at https://mydomain/wp-admin.
The installation can be completed by whoever visits first. Once you've finished step 3, complete the installation right away.
Management tips
- Updates: Update WordPress itself, themes and plugins from the admin screen. Outdated plugins are the most common cause of hacks, so delete plugins you don't use.
- Backups: Posts and settings are in the DB, and uploaded images and themes are in the
wpvolume. Use a backup plugin, or export the DB as below.
cd ~/wordpress
docker compose exec db mariadb-dump -u root -p wordpress > wp-db-$(date +%F).sql
(It asks for the root password.) For how to download it, see Backing up your server data.
If it doesn't work
- 502 Bad Gateway — Check with
docker compose psthat the containers are running. - "Error establishing a database connection" — Check that the two
db-passwordvalues incompose.yamlare the same. If you changed the password after the first run, the DB still has the old value. - After adding HTTPS, the page looks broken or keeps looping — Check that your Nginx configuration has the
X-Forwarded-Protoline.