How do you configure Caddy to handle multiple WP subdomains?

1. Caddy version (caddy version):

latest

2. How I run Caddy:

Within a docker-compose.yml file along with an external environment file and an external Caddyfile.

a. System environment:

Ubuntu v20.04

b. Command:

docker-compose up -d

c. Service/unit/compose file:

version: '3.3'
services:
  # Database
  database:
    image: mysql:latest
    container_name: database
    volumes:
      - ./db:/var/lib/mysql
    restart: always
    env_file: 
      - .env
    environment: 
      MYSQL_DATABASE: $WP_DB_NAME
    command: '--default-authentication-plugin=mysql_native_password'
    networks:
      - blog-network
  # WordPress
  wordpress:
    depends_on:
      - database
    image: wordpress:php7.4-fpm-alpine
    container_name: wordpress
    restart: always
    user: "root:root"
    env_file: 
      - .env
    environment:
      - WORDPRESS_DB_HOST=database:3306
      - WORDPRESS_DB_USER=$MYSQL_USER
      - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
      - WORDPRESS_DB_NAME=$WP_DB_NAME
    volumes:
      - ./php.ini:/usr/local/etc/php/conf.d/custom.ini
      - ./wordpress:/var/www/html
    networks:
      - blog-network
  # Webserver
  caddy:
    image: caddy:alpine
    container_name: webserver
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./wordpress:/var/www/html
      - ./caddy_data:/data
      - ./caddy_config:/config
      - ./Caddyfile:/etc/caddy/Caddyfile
    networks:
      - blog-network
networks:
  blog-network:
    driver: bridge

d. My complete Caddyfile or JSON config:

example.com {
    root * /var/www/html
    php_fastcgi wordpress:9000
    file_server
}

Additionally I have an .env environment file to define required credentials:

MYSQL_ROOT_PASSWORD=<password-goes-here>
MYSQL_USER=<user-goes-here>
MYSQL_PASSWORD=<password-goes-here>

3. The problem I’m having:

I want to add one or more additional WordPress instances. Each instance is referenced by a subdomain, and I’d like Caddy to do the routing and SSL generation.

I successfully created a docker-compose.yml file to create many WordPress instances referenced by port, but I can’t figure out how to format the Caddyfile to have Caddy route the subdomains properly.

What’s confusing me is that in the docker-compose.yml file above, WordPress is defined without any reference to ports, yet the Caddyfile referenced port 9000. How is this determined, and more importantly, if I want to create additional reverse proxy entries in my Caddyfile, how do I do this?

4. Error messages and/or full log output:

None at this time, as I am still trying to figure out how to format the Caddyfile.

5. What I already tried:

I successfully created a docker-compose.yml file to create many WordPress instances referenced by port, but I can’t figure out how to format the Caddyfile to have Caddy route the subdomains properly.

6. Links to relevant resources:

The php-fpm process that runs inside the wordpress container defaults to port 9000. Networking between containers doesn’t require configuring ports, because as long as they’re in the same Docker network, they can communicate to eachother.

The port config on your caddy service is so that you can bind ports 80 and 443 to the host machine, so that connections can enter the Docker network via Caddy.

It’s kinda tricky. Since Caddy needs the WordPress source files mounted (as you did with ./wordpress:/var/www/html), having more than one can cause conflicts.

What you’d have to do is mount each one at different locations inside Caddy, then override the root for php_fastcgi to /var/www/html which is the location the files are actually located inside the php-fpm container. So maybe something like this:

example.com {
	root * /var/www/example.com
	php_fastcgi wordpress:9000 {
		root /var/www/html
	}
	file_server
}

another.com {
	root * /var/www/another.com
	php_fastcgi another-wordpress-service:9000 {
		root /var/www/html
	}
	file_server
}

And you’d mount the different sites in Caddy like this maybe:

      - ./wordpress:/var/www/example.com
      - ./another:/var/www/another.com
1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.