Docker Stack - Wordpress + MariaDB + Caddy + FastCGI

Dear Caddy Community,

I just did my first Caddy project, coming from Traefik and I already love Caddy. It is simple, intuitive and fast as hell.

I am using Portainer to make a Stack with Wordpress, MariaDB and Caddy all in one.
Right now it is working, but I want to speed up the pages and enable fastcgi, which i did not successfully managed till now:

Here is my working docker-compose file:

version: '3'

services:
   db:
     image: mariadb:${MARIADB_VERSION_TAG}
     volumes:
       - ${DB_DATA_FOLDER}:/var/lib/mysql
     restart: always
     environment:
       - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
       - MYSQL_DATABASE=${MYSQL_DATABASE}
       - MYSQL_USER=${MYSQL_USER}
       - MYSQL_PASSWORD=${MYSQL_PASSWORD}

   wordpress:
     image: wordpress:${WORDPRESS_VERSION_TAG}
     ports:
       - 80
     restart: always
     environment:
      - WORDPRESS_DB_HOST=db:3306
      - WORDPRESS_DB_NAME=${MYSQL_DATABASE}
      - WORDPRESS_DB_USER=${MYSQL_USER}
      - WORDPRESS_DB_PASSWORD=${MYSQL_PASSWORD}
     volumes:
      - ${WORDPRESS_DATA_FOLDER}:/var/www/html
      - ${PHP_DATA_FOLDER}/php.ini:/usr/local/etc/php/conf.d/custom.ini

   caddy:
     image: caddy:${CADDY_VERSION_TAG}
     restart: unless-stopped
     ports:
       - "80:80"
       - "443:443"
     volumes:
       - ${CADDY_DATA_FOLDER}/Caddyfile:/etc/caddy/Caddyfile
       - ${CADDY_DATA_FOLDER}/data:/data
       - ${CADDY_DATA_FOLDER}/config:/config
     links:
       - wordpress

Here is my Caddyfile:

{
   email email@example.com
}

wordpress.example.com {
   reverse_proxy wordpress:80 {
     flush_interval 10
   }
}

Can anyone help me to complete this Caddyfile to make fastcgi working and speed up the wordpress sites?

I searched a lot, but only found incomplete Caddyfiles which were not working for me.
Thank you very much.

You need to mount the code to the Caddy container as well, so it can see and serve static files, and send requests to PHP files to the php-fpm service.

I highly doubt you need this. You can remove it from your config. It’s very rare that flush_interval is ever relevant to configure, and I can’t think of any reason a wordpress app would ever need to.

1 Like

Thank you francislavoie,
but the setup like I posted is working perfectly, also with the flush_interval.

The question is “How is the complete Caddyile looking, for a fastcgi setup?”

The docs have examples.

example.com {
	root * /var/www/wordpress
	encode gzip
	php_fastcgi php-fpm:9000
	file_server
}
1 Like

Hi Francislavoie,

I did that config now with the following compose and Caddyfile:

docker-compose: (guess it is important to use the fpm Docker-Tag)

version: '3'

services:
   db:
     image: mariadb:${MARIADB_VERSION_TAG}
     volumes:
       - ${DB_DATA_FOLDER}:/var/lib/mysql
     restart: always
     environment:
       - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
       - MYSQL_DATABASE=${MYSQL_DATABASE}
       - MYSQL_USER=${MYSQL_USER}
       - MYSQL_PASSWORD=${MYSQL_PASSWORD}

   wordpress:
     image: wordpress:6.0.1-php8.1-fpm
     ports:
       - 9000
     restart: always
     environment:
      - WORDPRESS_DB_HOST=db:3306
      - WORDPRESS_DB_NAME=${MYSQL_DATABASE}
      - WORDPRESS_DB_USER=${MYSQL_USER}
      - WORDPRESS_DB_PASSWORD=${MYSQL_PASSWORD}
     volumes:
      - ${WORDPRESS_DATA_FOLDER}:/var/www/html
      - ${PHP_DATA_FOLDER}/php.ini:/usr/local/etc/php/conf.d/custom.ini

   caddy:
     image: caddy:${CADDY_VERSION_TAG}
     restart: unless-stopped
     ports:
       - "80:80"
       - "443:443"
     volumes:
       - ${CADDY_DATA_FOLDER}/Caddyfile:/etc/caddy/Caddyfile
       - ${CADDY_DATA_FOLDER}/data:/data
       - ${CADDY_DATA_FOLDER}/config:/config
       - ${WORDPRESS_DATA_FOLDER}:/var/www/html
     links:
       - wordpress

Caddyfile:

{
   email mail@example.com
}
domain.example.com {
	root * /var/www/html
	encode gzip
	php_fastcgi wordpress:9000
	file_server
}

Directly calling php-fpm like it is in your Caddyfile example did not work.

The site is showing up without errors now. Thank you.

Are there any possibilities to add more tunes to the Caddyfile to increase Security and speed of wordpress?

Not really. The defaults are fine.

In the future I want to seperate the Caddy into a different stack and server, but also keep the fastcgi functionality. How would the Caddyfile look in this case?

{
   email mail@example.com
}
domain.example.com {
	root * /var/www/html    #Here I have no clue, how to map the volume
	encode gzip
	php_fastcgi wordpress:9000    #I guess the machine local IP instead of the link? 
	file_server
}

You just have to have a copy of the code on both servers. How you do that is up to you. But that is a requirement.

And if one server has only a single Caddy stack and the other server has only Wordpress containers and no Caddy?

Like I said, both Caddy and PHP need the code.

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