Software, electronics and anything else

Installing WordPress on Caddy

Thanks to an interesting e-mail from Oracle recently, I’ve been consolidating some servers and wanted to try installing WordPress on the Caddy webserver. While doing it, I figured I’d write a howto guide so here it is. Note that I’m doing this on Ubuntu 22.04.

Installing Caddy is fairly straightforward from their website i.e.:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
sudo apt install php-fpm php-mysql php-curl

Caddy should already be running; if not, start it manually:

sudo service caddy start
sudo service caddy status
● caddy.service - Caddy
     Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-03-29 00:48:53 UTC; 1min 30s ago
       Docs: https://caddyserver.com/docs/
   Main PID: 5163 (caddy)
      Tasks: 9 (limit: 28703)
     Memory: 9.6M
        CPU: 50ms
     CGroup: /system.slice/caddy.service
             └─5163 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

Mar 29 00:48:53 selfhost2 caddy[5163]: {"level":"info","ts":1680050933.9117732,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":""}
Mar 29 00:48:53 selfhost2 caddy[5163]: {"level":"info","ts":1680050933.9129455,"logger":"admin","msg":"admin endpoint started","address":"localhost:2019","enforce_origin":false,"origins":["//localhost:2019","//[::1]:2019","//127.0.0.1:2019"]}
....

You can verify it’s working by navigating to the URL of your site; you should see the default Welcome page:

Now we need to download WordPress. We’ll come back to Caddy later, so stop it for now.

sudo service caddy stop

Create the basic directories we need, download and extract WordPress, and set permissions:

sudo mkdir /var/www/
cd /var/www/
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xf latest.tar.gz
sudo chown www-data:www-data -R wordpress/

Install a database server. In my case, MariaDB because it’s open source:

apt install mariadb-server

Configure the database with appropriate permissions. I followed the official guide, which as of the time of writing says the following:

$ mysql -u adminusername -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5340 to server version: 3.23.54

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CREATE DATABASE databasename;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON databasename.* TO "wordpressusername"@"hostname"
-> IDENTIFIED BY "password";
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec) 

mysql> EXIT
Bye
$ 

Modify the Caddyfile to point to your new WordPress install. Here’s an example taken from the official documentation, showing just how short it can be:

example.com

root * /var/www/wordpress
php_fastcgi unix//run/php/php-fpm.sock
file_server

Start caddy and you should have the WordPress installer pop up, which you can configure with your database credentials to get your site up and running:

Note: By default, Caddy will serve the site over HTTPS and use the wonderful Let’s Encrypt for the certificates. However, if you have your site behind Cloudflare, it interferes with the certificate generation process, so you’ll need to either disable Cloudflare’s proxying or build/download a custom Caddy version to get the Cloudflare DNS plugin to do it that way.

Lastly, if you use Wordference, note that you can select the Nginx option and it will work fine with Caddy.

Leave a Reply

Your email address will not be published. Required fields are marked *