Setup Caddy to host WordPress site

2022-03-03 - Instaructions for setting up Caddy web server and WordPress to self-host a website.

1. What is Caddy?

Caddy is a relatively new web server, that can be used instead of Apache httpd and nginx for web hosting. As oposed to those two more popular servers, its configuration is dead simple with sane defaults. What takes a long and cumbersome configuration in Apache and nginx, is just a few lines of config in Caddy.

Some things don't even have to be configured at all, like for example https certificates, which get automatically configured and set up by Caddy using Lets Encrypt.

2. Preresquisites

In order to proceed with instruction I assume that you aleady have a Linux server available where you will host your website. Also, should already have a domain pointing to your sever IP.

This post assumes havin a Debian derivative distribution, but any Linux will do.

3. Install Mysql and PHP

First install mysql and php-fpm that will be used for php handling.

sudo apt update
sudo apt install mysql-server php8.1-fpm curl wget

sudo mysql_secure_installation

sudo systemctl start mysql
sudo systemctl enable mysql

Then create a database for the Wordpres.

sudo mysql 

create database wp_example;
create user wp_example@localhost identified by 'wp_example_pass';
grant all privileges on wp_example.* to wp_example@localhost;
flush privileges;
exit;

4. Install and configure WordPress

First install WordPress:

sudo -i
mkdir -p /var/www/wp_example
cd /var/www/wp_example
wget http://wordpress.org/latest.tar.gz -O - | tar xvfz -
cd wordpress
cp wp-config-sample.php wp-config.php
vi wp-config.php

Not that you should edit wp-config.php file to configure the database that you created in previous steps.

5. Install and set up Caddy

To install Caddy server, please follow the official installation instructions.

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc
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

After installing you need to configure it by editing its configuration at /etc/caddy/Caddyfile

wp.example.com {
    root * /var/www/wp_example/wordpress
    php_fastcgi unix/run/php/php8.1-fpm.sock
    file_server
    encode zstd gzip
    @disallowed {
        path /xmlrpc.php
        path *.sql
        path /wp-content/uploads/*.php
    }
    rewrite @disallowed 'index.php'
}

This will tell Caddy to host our site using fastcgi at /var/www/wp_example/wordpres location.

Now you can start Caddy.

systemctl start caddy

Caddy will automatically enable https and you would be able to access your newly created WordPress site at https://wp.example.com.

After accessing the site, you will be prompted by WordPress to finalize your setup by setting up administrator user. For actual WordPress site development please check one of the wordpress.com tutorials.

6. How to host multiple sites?

If you want to host multiple sites in one server, it's easy. For each WordPres installation you have to create a separate DB and a separate directory, as already described above.

Then, you can use Caddy templating functionality to skip boilerplate configuration in the Caddyfile, for example by using something like this:

(wordpress) {
    root * /var/www/{args.0}/wordpress
    php_fastcgi unix/run/php/php8.1-fpm.sock
    file_server
    encode gzip
    @disallowed {
        path /xmlrpc.php
        path *.sql
        path /wp-content/uploads/*.php
    }
    rewrite @disallowed 'index.php'
}

wp.example.com  {
    import wordpress wp_example
}

wp2.example.com {
    import wordpres wp_example2
}

wp3.example.com {
    import wordpres wp_example3
}

In this example (wordpres) is a template and {args.0} is the template placeholder that will be replaced with the first paramter provided when importing template.

7. Next steps

If you want to use Caddy to configure Wedbav to have your files accessible using webdav protocol over the internet, you can check next post how to do it.

Keywords: caddy WordPress site hosting linux