How to set up WordPress with Caddy 2

This step-by-step guide will show you how to set up a WordPress blog based on the Caddy 2 web server, on Ubuntu 20.04 LTS. In this guide, WordPress will be put inside the /wordpress folder of the website root.

Caddy is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go. Caddy has relatively limited adoption vs. other major platform such as Apache and Nginx, but is gaining share due to its simplicity of use yet still very powerful. It was recently updated to Caddy 2, but the documentation available online about the various configurations using Caddy 2 is very limited. There are actually quite a few tricks in order to correctly configure Caddy v2 to work with WordPress.

1. Get the Prerequisites for WordPress

First up, let’s install the necessary backbone packages for WordPress.

1.1 Install PHP

Since WordPress relies on PHP, we will need to install the required php packages on Ubuntu. You can do so by running the following commands

$ sudo apt -y install php-fpm php-mysql php-curl php-gd php-mbstring php-common php-xml php-xmlrpc

1.2 Install MariaDB

We will use MariaDB instead of MySQL. MySQL has played a key role in building the internet as we know it today. However, MariaDB has gained lots of traction due to its full compatibility with MySQL, superior out-of-box performance, and being fully open sourced. To install MariaDB, take the following steps.

$ sudo apt-get install software-properties-common
$ sudo apt-get install software-properties-common
$ sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
$ sudo add-apt-repository "deb [arch=amd64,arm64,ppc64el] http://mariadb.mirror.liquidtelecom.com/repo/10.4/ubuntu $(lsb_release -cs) main"
$ sudo apt update
$ sudo apt -y install mariadb-server mariadb-client
$ sudo mysql_secure_installation

You can verify if the installation is successful by running $ mysql -u root -p and log in as root using the password created during setup, then run

"SELECT VERSION();"

1.3 Create WordPress database and user

The next step is to create a database (‘wp_website’ in the below example), and a user (‘wp_username’ in the below example) for WordPress to use. First get into the SQL console,

$ mysql -u root -p

Then run the following quries

MariaDB [(none)]> CREATE DATABASE wp_website;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON wp_website.* to 'wp_username'@'localhost' IDENTIFIED BY 'type_user_password_here';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> quit

2. Install Caddy 2 Web server

If you have not installed Caddy on your server, you can follow this Caddy 2 web server installation guide to get Caddy 2 installed. When following the above guide, there is no need to worry about the Caddyfile, because I will show you how to configure the right Caddyfile for WordPress in Section 4 below.

3. Download and Install WordPress

With all the prerequisite steps done, we can now download and install WordPress. To resolve any potential WordPress write/read issue, make sure the caddy user owns the html folder. In the below example, WordPress is copied to its own sub-folder /WordPress from the site root.

$ wget http://wordpress.org/latest.tar.gz
$ tar xvf latest.tar.gz
$ sudo cp -R wordpress /var/www/html/
$ sudo chown -R caddy:caddy /var/www/html
$  sudo mv /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

Then update the wp-config.php with the WordPress database information created in Section 1.3.

4. Configure Caddyfile for WordPress

Now it’s the interesting part. Let’s edit Caddyfile which is usually located under /etc/caddy/. Assuming you followed the Caddy installation guide, and the WordPressis installed to /var/www/html/wordpress.

domain.com {
    encode zstd gzip
    root * /var/www/html    

    ####### WordPress ####### 
    redir /wordpress /wordpress/

    # Change to the right version, then change php-fpm www.config with caddy being the owner and listengroup following step 4
    php_fastcgi unix//run/php/php7.4-fpm.sock  
 
    # Prevent malicious php uploads
    @uploads {
        path_regexp uploadpath /uploads\/(.*)\.php
    }
    rewrite @uploads /

   # Not really needed
    #@wp-admin {
    #    path not ^\/wp-admin/*
    #}    
     
    # Enable the static file server.
    file_server 

Next, reload the Caddyfile to apply the changes.

$ sudo systemctl reload caddy

5 Configure PHP-FPM to work with Caddy 2

To ensure WordPress to have the correct write permission after being served by Caddy, we need to update the PHP-FPM config. Otherwise, WordPress might throw out “error occurred while updating” during theme update, or require FTP access to install any themes or plugins. If you already know the path of the PHP-FPM pool config, you can skip to Section 5.2.

5.1 Figure out the installation path of the running instance of PHP-FPM

First, figure out which path the FPM is running off of. To do this, create a phpinfo.php file and put it to the web root of your caddy server with the following content.

<?php 
    phpinfo(); 
?>

Visit this page (e.g. https://yourdomain.com/phpinfo.php), and it should show the php configurations on the server. Note the Configuration File (php.ini) Path text. It should look something like this: /etc/php/7.4/fpm.

Don’t forget to remove the phpinfo.php after you are done, since it contains sensitive information about the web server.

5.2 Modify the FPM Pool Config

In the FPM Pool config file, change the listen.group, listen.user, user and group to caddy, or the username created to run the Caddy server.

$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf

Then restart PHP-FPM service to apply the changes

$ sudo service php7.4-fpm restart

6. Check if WordPress is Working with Caddy 2

In browser, go to https://yourdomain.com/wordpress and WordPress should be running. If not, take a look at the Common Issues sections below or leave a comment below.

Cheers!

Leave a Reply