Master Caddy 2: Install, Configure, and Integrate PHP for Fast Web Hosting

Learn how to install Caddy 2, set up system services, configure global and site-specific Caddyfiles, and integrate PHP‑FPM, covering static site hosting, reverse proxy, HTTP redirects, and troubleshooting, with step‑by‑step commands and code examples for seamless migration from Apache or Nginx.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Master Caddy 2: Install, Configure, and Integrate PHP for Fast Web Hosting

Installation

The official installation guide is available at https://caddyserver.com/docs/install. On Debian‑based systems you can install Caddy with the following commands:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
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

Verify Service Startup

Use the following commands to control the Caddy service:

systemctl start caddy
caddy start
caddy run # Starts server and blocks indefinitely

systemctl stop caddy
caddy stop

systemctl reload caddy
caddy reload

systemctl restart caddy
caddy stop && caddy start

Enable the service to start on boot:

sudo systemctl daemon-reload
sudo systemctl start caddy.service
sudo systemctl enable caddy.service

Configuration

Caddy works on many Linux distributions. After installation the default global configuration file is /etc/caddy/Caddyfile. Individual site configurations can be placed in /etc/caddy/conf/, mirroring the Apache/Nginx layout:

/etc/caddy
  ├── Caddyfile
  └── conf/
      └── caddy.tinywan.com.conf

Primary Configuration (Caddyfile)

{
    log default {
        format console
        output file /var/log/caddy/system.log
        exclude http.log.access
    }
}
import conf/*

Site‑Specific Configuration Example

caddy.tinywan.com {
    # Set the document root for this site
    root * /home/www/website/caddy

    # Enable static file serving
    file_server
}

Applications

Static Site Hosting

caddy.tinywan.com {
    root * /home/www/website/caddy
    file_server
}

Access the site at https://caddy.tinywan.com/. The default Caddy welcome page will appear, which can be replaced with a custom HTML page.

Reverse Proxy

proxy-caddy.tinywan.com {
    reverse_proxy localhost:6140
}

HTTP Redirect to www Subdomain

tinywan.com {
    redir https://www.{host}{uri}
}

www.tinywan.com {
    root * /home/www/website/full-stack/dist
    file_server
}

Requests to tinywan.com are redirected to https://www.tinywan.com/.

PHP‑FPM Integration

Caddy uses FastCGI reverse proxy to hand PHP requests to PHP‑FPM, similar to Apache or Nginx.

caddy-php.tinywan.com {
    # Document root for the PHP application
    root * /home/www/website/demo/public

    file_server
    encode gzip

    # Forward .php requests to the PHP‑FPM socket
    php_fastcgi unix//var/run/php8.0.7-fpm.sock
}

If you encounter a 502 Bad Gateway error, check the Caddy error log for permission issues on the PHP‑FPM socket. Adjust the service user and group in /lib/systemd/system/caddy.service from caddy to www (or another appropriate user) and reload the service.

Testing PHP

Create /home/www/website/demo/public/index.php with the following content:

<?php
echo "Hello 开源技术小栈";

Visit https://caddy-php.tinywan.com/ (ensure DNS resolves to the server) to see the PHP output.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ConfigurationLinuxreverse proxyWeb serverphp-fpmCaddy
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.