Caddy is a contemporary, open-source Go web server that has been described as having a clean configuration, automatic HTTPS, and performance-oriented characteristics. With a combination of Caddy and PHP-FPM, you can now easily and reliably serve dynamic PHP sites.
Step 1: Install Caddy and PHP-FPM
You can start by installing Caddy and PHP-FPM on your server:
- Caddy: Add the official Caddy repository to your machine, refresh packages, and install.
Step 2: Design your directory of the site and modify its ownership.
Step 3: Open Caddyfile and Configure
Step 4: Learn PHPFastCGI Behavior
The phpfastcgi directive is a simpler implementation of reverse proxying to FastCGI applications such as PHP-FPM. It performs these steps:
Tests whether the path requested is a file-serves it when it is found.
- Checks index.php-rewrites and serves, if available.
- Otherwise, index.php needs to be rewritten to allow router-based frameworks to support dynamic routes.
- To get more control (e.g., custom non-PHP URLs or frameworks), we can use reverseproxy with the fastcgi transport, but wrapped in handle blocks:
handle {
root /var/www/app.example.org/public
rewrite /index.php
reverseproxy app:9000 {
transport fastcgi {
split .php
capturestderr
}
}
}
This makes sure that all requests are gracefully redirected through your application router, and not just existing files.
Step 5: Test and Reload
Once the Caddyfile has been edited, run:
- sudo caddy validate -f /etc/caddy/Caddyfile
- sudo systemctl reload caddy
Next, open the URL example.com/info.php to verify that PHP is working. When your application operates on pretty URLs or has a router, you want to test navigating to different routes to ensure that the index.php is grabbing them and rendering them.
Summary
- Install Caddy and PHP-FPM.
- Build a public directory on your site and put a phpinfo() test file.
- Set up Caddyfile with root, encode, phpfastcgi, and fileserver.
- Know routing: phpfastcgi automatically maintains file detection and routing–reverse-proxy + fastcgi might be required when you want framework-like capabilities.
- Reload and test and make sure nothing is amiss.
Simple and fast Caddy combined with the performance of PHP-FPM will make your PHP-based site run safely, efficiently, and cleanly. Need assistance with making this work in Docker, frameworks (such as Laravel or Symphony), or multi-site configurations? Just ask!