caddy is a modern, open-source web server written in Go. It is famous for its default HTTPS configuration, simple configuration syntax, and high performance. This guide covers installing Caddy and configuring it to serve dynamic PHP applications using PHP 8.3 on Ubuntu 24.04.
Prerequisites
- A server running Ubuntu 24.04.
- A valid domain name pointing to your server’s IP address (required for automatic SSL).
- Root or
sudoaccess.
Step 1: Install Caddy Web Server
Caddy is not included in the standard Ubuntu repositories. We will add the official Caddy repository to ensure we get the latest stable version.
Install necessary dependencies:
sudo apt update sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
Add the Caddy GPG key:
curl -1sLf '[https://dl.cloudsmith.io/public/caddy/stable/gpg.key](https://dl.cloudsmith.io/public/caddy/stable/gpg.key)' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
Add the official Caddy repository:
curl -1sLf '[https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt](https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt)' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
Update package lists and install Caddy:
sudo apt update sudo apt install caddy
Verify the installation:
Caddy starts automatically after installation.
Check the status
systemctl status caddy
Step 2: Install PHP 8.3
While Ubuntu 24.04 includes recent PHP versions, using the ondrej/php PPA is recommended to ensure you have access to specific versions like 8.3 and all necessary extensions.
Add the PHP PPA:
sudo add-apt-repository ppa:ondrej/php sudo apt update
Install PHP 8.3 and common extensions:
sudo apt install php8.3 php8.3-fpm php8.3-CLI php8.3-common php8.3-mysql php8.3-zip php8.3-gd php8.3-mbstring php8.3-curl php8.3-xml php8.3-bcmath
Verify PHP-FPM is running:
systemctl status php8.3-fpm
Important: Note the location of the PHP socket file. For PHP 8.3, it is typically located at /run/php/php8.3-fpm.sock.
Step 3: Configure Caddy
Caddy is configured using a file called the Caddyfile.
Open the Caddyfile
sudo nano /etc/caddy/Caddyfile
Configure your domain:Replace the existing content with the configuration below. Be sure to change example.com to your actual domain name.example.com
{ # Set the root directory root * /var/www/html # Enable compression for faster loading encode zstd gzip # Pass PHP requests to the FastCGI server php_fastcgi unix//run/php/php8.3-fpm.sock # Enable the static file server file_server # Optional: Security headers header { X-Frame-Options "SAMEORIGIN" X-XSS-Protection "1; mode=block" X-Content-Type-Options "nosniff" } }
Save and exit: Press CTRL+O, Enter, then CTRL+X.
Reload Caddy:Apply the configuration changes without restarting the server:sudo systemctl reload caddy
Step 4: Test the Setup
Create a PHP info file:sudo nano /var/www/html/info.php
Add the following content:
<?php phpinfo(); ?>
Access the page:Open your browser and navigate to https://your-domain.com/info.php.
Success: You should see the PHP configuration page.
SSL: Check the address bar to confirm the connection is secure (HTTPS).
Troubleshooting
502 Bad Gateway
This indicates Caddy cannot connect to PHP-FPM.
- Check if
php8.3-fpmis running:systemctl status php8.3-fpm. - Verify the socket path in your
Caddyfilematches the actual file on disk (ls /run/php/).
Permission Issues
If you see “403 Forbidden” or upload errors:
- Ensure the web root is owned by
www-data:sudo chown -R www-data:www-data /var/www/html - Add the
caddyuser to thewww-datagroup so it can read files:sudo usermod -aG www-data caddy
Conclusion
You now have a secure, high-performance web server running the latest version of PHP. Caddy automatically handles certificate renewals, making maintenance significantly easier than older web servers. From here, you can proceed to install your favorite PHP framework (like Laravel or Symfony) or CMS (like WordPress) by simply unpacking the files into your web root directory.
