Caddy and WordPress

This is not something you won’t find elsewhere on the web, but it is my take on making Caddy and WordPress play nice. Assuming an Ubuntu 22.04 VM, we follow the site instructions to install Caddy.

Next, we install PHP-FPM (the FastCGI server for PHP) and other required libraries

sudo apt install php8.1-fpm php-pear php-bcmath php-curl \ 
php-imagick php-intl php-json php-mbstring \ 
php-mysql php-xml php-zip

Now we are ready to configure /etc/caddy/Caddyfile to work with PHP-FPM

website-test.example.net {
	tls hostmaster@example.com
	root * /usr/share/caddy/wordpress
	file_server
	encode zstd gzip
	php_fastcgi unix//run/php/php8.1-fpm.sock
	@disallowed {
		path /xmlrpc.php
		path *.sql
		path /wp-content/uploads/*.php
	}
	rewrite @disallowed '/index.php'
}

The trickiest line for me in the above configuration was the file URL for php_fastcgi. Not exactly in the format I expected. This configuration assumes that we have unpacked WordPress in /usr/share/caddy/wordpress . Keep in mind that php-fpm runs as UID:GID www-data:www-data so you may want to sudo chown -R www-data:www-data /usr/share/caddy/wordpress

We are still missing a database. If you do not want to mess much with installing MariaDB, you can use something like the following docker-compose.yaml:

# docker-compose up -d 
version: '3.1'
services:
  mysql:
    image: mariadb:10.11.7
    restart: always
    ports:
    - "3306:3306"
    environment:
      MYSQL_DATABASE: wordpress-database
      MYSQL_USER: wordpress-user
      MYSQL_ROOT_PASSWORD: PASSWORD
      MYSQL_PASSWORD: PASSWORD
    volumes:
      - mysql:/var/lib/mysql
volumes:
  mysql:

You now only need to edit wp-config.php to set the database settings and the salt keys and you are all set to play.

PS: I’ve read a number of posts on the web before reaching to the summary above. I did not keep track at the time for proper citation.

Leave a comment