How to use Caddy Server with PHP

By Akash
on 25-11-2023 12:34 PM

Caddy Server is a modular and modern web server platform that supports automatic HTTPS certificates, QUIC and HTTP/2, Zstd and Brotli compression, and various modern features as well as classic web server features such as configurable virtual hosts, URL rewriting and redirects, reverse proxying, and more. Caddy 2, the current version that was released in 2020 May introduced significant improvements to its configuration syntax, automation, plugins, and more.
This article explains how to integrate PHP with the Caddy web server version 2 series, and advanced configuration. It also compares similar configurations with Apache and Nginx configurations to ease the migration from Apache and Nginx to Caddy.
Caddy is available on many operating systems and Linux-based distros. Caddy documentation explains how to install Caddy, and configure it as a service/daemon that runs automatically with the server start.

Once Caddy is installed, Caddy can be configured with minimal configuration that serves static files if they are present, and passes other requests to PHP-FPM. Caddy Server comes with secure and performant default configuration, which makes it easy to configure with minimal configuration.When Caddy is installed and configured as a system service, a default /etc/caddy/Caddyfile can be used as the global configuration file, and a sub-directory with a suggested name /etc/caddy/sites to contain the configuration files for individual sites, akin to Apache and Nginx configuration.The global Caddyfile can specify the global configuration, and include config/* and sites/* directories to include the additional configuration.

This configures Caddy to write system logs to /var/log/caddy/system.log file (but not HTTP request logs), as well as loading additional configuration files from config and sites directories.