Caddy Server and WordPress (PHP-FPM)

Caddy Logo

Update: I added rewrite rules for WordPress permalinks to the Caddyfile. I also removed all unnecessary bits which are not needed anymore since Caddy now has built-in HTTPS support with Let’s Encrypt.

Recently I discovered an amazing open source project: Caddy, a fully functional HTTP/2 webserver, written in Go.

Caddy is a lightweight, general-purpose web server for Windows, Mac, Linux, BSD, and Android. It is a capable alternative to other popular and easy to use web servers.

The most notable features are HTTP/2, Virtual Hosts, TLS + SNI, and easy configuration with a Caddyfile. Usually, you have one Caddyfile per site. Most directives for the Caddyfile invoke a layer of middleware which can be used in your own Go programs.

In this tutorial I will explain how to run WordPress with Caddy server (on Ubuntu), using Caddy’s FastCGI support and PHP-FPM (i.e. PHP FastCGI Process Manager). Of course you also need MySQL/MariaDB to run WordPress.

MySQL

I assume that you have a MySQL server up and running (or that you have the knowledge to do so) .

PHP

Install PHP-FPM:

$ sudo apt-get install php5-fpm

PHP-FPM listens to FastCGI requests on a unix socket by default (on Ubuntu and Arch Linux): unix:/var/run/php5-fpm.sock (or unix:/var/run/php/php7.0-fpm.sock for PHP 7). So we’ll have to tell the webserver to pass the PHP requests to it.

You could also use a regular UDP socket if you want: change listen in /etc/php5/fpm/pool.d/www.conf to 127.0.0.1:9000. On some operating systems (e.g. FreeBSD), PHP-FPM listens on this UDP socket by default.

Caddyfile

Now we need to configure Caddy so it will use PHP-FPM. We also need to specify virtual hosts for the domain(s) we will be using.

The following Caddyfile contains everything you need to run WordPress with PHP-FPM on HTTP/2.

www.my-wordpress-blog.com {
    redir https://my-wordpress-blog.com{uri}
}

my-wordpress-blog.com {
    root wordpress
    log access.log
    errors error.log
    
    # PHP-FPM with Unix socket
    fastcgi / /var/run/php5-fpm.sock php
    
    # PHP-FPM with regular udp socket
    # fastcgi / 127.0.0.1:9000 php

    # Routing for WordPress
    rewrite /{
        to {path} {path}/ /index.php?{query}
    }
}

Caddy will use the wordpress folder as document root, and will pass all PHP requests to a FastCGI process (which is the PHP-FPM process). It will also automatically install SSL certificates (from Let’s Encrypt) and serve the website over HTTPS. HTTP requests will be redirected to the HTTPS domain.

The Caddy documentation contains more detailed information on the available directives. Checkout my other blogposts if you need help Running Caddy Server as a service or Creating a self-signed certificate.

WordPress

Now it’s time to install WordPress. Simple download WordPress and copy the files to the wordpress folder. To install WordPress you can just follow the steps on the screen like any other easy WordPress configuration.

If you want to use HTTP/2, you should configure WordPress with HTTPS enabled. You can do this in Settings, General: change http in the urls to https.
Most browsers only speak HTTP/2 when using TLS. Although TLS only is not marked as mandatory in the RFC 7540, Firefox and Chrome development teams have no actual intents to implement HTTP/2 without TLS.