← Back to list

Install Caddy Server on DigitalOcean (Ubuntu 22.04) with Multi-Domain Support

This guide shows you how to install Caddy Server on a DigitalOcean droplet running Ubuntu 22.04, and configure it to serve multiple domains…

MUHSIN MOHAMED PC · 2026-04-20 07:17 · 0 claps · 1.8 min read paywalled
#caddy #digitalocean #php
Open on Medium ↗
Wiki topics: 🔓 · Open Source 🏃 · Running & Endurance

Install Caddy Server on DigitalOcean (Ubuntu 22.04) with Multi-Domain Support

This guide shows you how to install Caddy Server on a DigitalOcean droplet running Ubuntu 22.04, and configure it to serve multiple domains with automatic HTTPS.

1. Create Your Droplet

  • Create a new droplet with Ubuntu 22.04
  • Connect to it using SSH

2. Install Caddy

Run the following commands:

sudo apt update
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https

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

3. Start and Enable Caddy

sudo systemctl start caddy
sudo systemctl enable caddy

4. Install PHP

Since we are using PHP for dynamic handling, install it:

sudo apt update
sudo apt install php php-fpm php-mysql php-curl php-xml php-mbstring php-zip -y

Check if PHP-FPM is running:

sudo systemctl status php*-fpm

5. Create Website Directory

sudo mkdir -p /var/www/html
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

6. Configure Caddy for Multi-Domain Support

sudo nano /etc/caddy/Caddyfile

Add the following configuration:

{
    email your-real-email@example.com

    on_demand_tls {
        ask http://127.0.0.1/allow-domain.php
    }
}

:80, :443 {
    tls {
        on_demand
    }

    root * /var/www/html

    # Replace with your actual PHP version
    php_fastcgi unix//run/php/php8.2-fpm.sock

    file_server
}

👉 Make sure the PHP socket matches your installed version (example: php8.2-fpm.sock).

7. Create Domain Validation Script (Important)

Caddy will call this script before issuing SSL certificates.

Create the file:

sudo nano /var/www/html/allow-domain.php

Add this basic example:

<?php

$allowed_domains = [
    "yourdomain.com",
    "www.yourdomain.com"
];

$domain = $_GET['domain'] ?? '';

if (in_array($domain, $allowed_domains)) {
    http_response_code(200);
} else {
    http_response_code(403);
}

👉 You can later connect this to a database for dynamic domain control.

8. Create a Test File

sudo nano /var/www/html/index.php

Add:

<?php
echo "PHP is working - Domain: " . $_SERVER['HTTP_HOST'];

9. Restart Services

sudo systemctl restart php8.2-fpm
sudo systemctl reload caddy

10. Configure Your Domain

  • Go to your domain provider
  • Add an A record pointing to your droplet IP

Example:

example.com → YOUR_SERVER_IP

11. Test Multi-Domain Access

  • Open your domain in a browser
  • Caddy will:
  • Automatically generate SSL
  • Serve your site
  • You should see:
PHP is working - Domain: yourdomain.com

⚠️ Important Notes (Do Not Skip)

1. Do NOT allow all domains in production

If you don’t restrict domains:

  • Anyone can point a domain to your server
  • SSL certificates may be abused

👉 Always validate domains via allow-domain.php

2. Basic Firewall Setup

Enable firewall:

sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

메타데이터
post_id
fcddee6d4f84
slug
install-caddy-server-on-digitalocean-ubuntu-22-04-with-multi-domain-support-fcddee6d4f84
url
https://medium.com/@waphunt/install-caddy-server-on-digitalocean-ubuntu-22-04-with-multi-domain-support-fcddee6d4f84
canonical_url
https://medium.com/@waphunt/install-caddy-server-on-digitalocean-ubuntu-22-04-with-multi-domain-support-fcddee6d4f84
author_url
https://medium.com/@waphunt
status
ok
fetched_at
2026-06-22 19:40:15