How To Install Caddy on Ubuntu 24.04 LTS
Caddy is an open-source web server written in Go, designed with simplicity and security in mind. It offers several advantages over traditional web servers, including:
- Automatic HTTPS by default
- Easy-to-use configuration syntax
- Built-in support for HTTP/2 and HTTP/3
- Powerful reverse proxy capabilities
- Native support for static file serving and dynamic content
In this tutorial, we’ll guide you through the process of installing Caddy on Ubuntu 24.04 LTS, configuring it to serve your web applications, and securing it with HTTPS. By the end of this guide, you’ll have a fully functional Caddy web server ready to host your websites and applications.
Prerequisites
Before we begin, ensure that you have the following:
- A server running Ubuntu 24.04 LTS
- A non-root user with sudo privileges
- A domain name pointing to your server’s IP address
- Basic familiarity with the command line
Additionally, it’s always a good practice to update your system before installing new software. Run the following commands to update your package list and upgrade existing packages:
sudo apt update
sudo apt upgrade -y
If a system reboot is required after the upgrade, use the following command:
[ -e /var/run/reboot-required ] && sudo reboot
Step 1: Install Caddy on Ubuntu 24.04
There are two primary methods to install Caddy on Ubuntu 24.04: using the default Ubuntu repository or the official Caddy repository. We’ll cover both options to give you flexibility in your installation process.
Option A: Using Default Ubuntu Repository
The simplest way to install Caddy is through the default Ubuntu repository. This method is straightforward but may not always provide the latest version of Caddy.
- Update the package list:
sudo apt update
- Install Caddy:
sudo apt install caddy -y
- Verify the installation:
caddy version
Option B: Using Official Caddy Repository
For those who prefer to have the latest version of Caddy, installing from the official repository is recommended. This method ensures you have access to the most recent features and security updates.
- Install required dependencies:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
- Add the GPG key for the Caddy repository:
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
- Add the Caddy repository to your system:
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
- Update the package list and install Caddy:
sudo apt update sudo apt install caddy
Step 2: Configure Caddy Service
After installing Caddy, it’s important to properly configure the service to ensure it runs smoothly and securely.
Start and Enable Caddy
To start the Caddy service and enable it to run on system boot, use the following commands:
sudo systemctl start caddy
sudo systemctl enable caddy
Verify Service Status
Check if Caddy is running correctly by examining its status:
sudo systemctl status caddy
You should see output indicating that Caddy is active and running.
Allow Privileged Ports
By default, non-root processes can’t bind to ports below 1024. To allow Caddy to bind to privileged ports like 80 and 443, use the following command:
sudo setcap 'cap_net_bind_service=+ep' /usr/bin/caddy
Step 3: Configure the Caddyfile
The Caddyfile is Caddy’s configuration file, where you define how your web server should behave. Let’s explore how to set up a basic configuration.
Locate and Edit the Configuration File
The default Caddyfile is located at /etc/caddy/Caddyfile
. Open it using a text editor like Nano or Vi:
sudo nano /etc/caddy/Caddyfile
Basic Configuration Example
Here’s a simple configuration to serve a static website:
your-domain-name.com {
root * /var/www/your-domain-name
file_server
encode gzip zstd
}
This configuration tells Caddy to serve files from the /var/www/your-domain-name
directory for requests to your-domain-name.com
.
PHP Integration
If you’re planning to serve PHP applications, you’ll need to install PHP and configure Caddy to work with PHP-FPM. Here’s how:
- Install PHP and required extensions:
sudo apt install php php-fpm php-mysql php-curl php-gd php-mbstring php-common php-xml php-xmlrpc
- Configure PHP-FPM for Caddy by editing the PHP-FPM pool configuration:
sudo nano /etc/php/8.3/fpm/pool.d/www.conf
Update the following lines:
user = caddy group = caddy listen.owner = caddy listen.group = caddy
- Update your Caddyfile to include PHP processing:
your-domain-name.com { root * /var/www/your-domain-name php_fastcgi unix//run/php/php8.3-fpm.sock file_server encode gzip zstd }
Validate Configuration
Before applying changes, it’s crucial to validate your Caddyfile for syntax errors:
sudo caddy validate --config /etc/caddy/Caddyfile
Restart Service
After making changes to your Caddyfile, restart the Caddy service to apply them:
sudo systemctl restart caddy
Step 4: Secure Your Server with HTTPS
One of Caddy’s standout features is its automatic HTTPS provisioning. Let’s explore how to leverage this for your domain.
Automatic HTTPS with Let’s Encrypt
Caddy automatically obtains and renews SSL/TLS certificates from Let’s Encrypt for domains specified in your Caddyfile. To enable this, ensure your domain is correctly configured in the Caddyfile and that ports 80 and 443 are open on your firewall.
Firewall Configuration
If you’re using UFW (Uncomplicated Firewall), allow HTTP and HTTPS traffic:
sudo ufw allow http
sudo ufw allow https
sudo ufw reload
Verify HTTPS Setup
Open a web browser and navigate to your domain using HTTPS (e.g., https://your-domain-name.com
). You should see a secure connection with a valid SSL certificate.
Step 5: Testing Your Setup
To ensure everything is working correctly, let’s create a test page and verify that Caddy is serving it properly.
Create a Test Page
Create a simple PHP file to test both static file serving and PHP processing:
echo "" | sudo tee /var/www/your-domain-name/info.php
Access the Test Page
Visit https://your-domain-name.com/info.php
in your web browser. You should see the PHP info page, confirming that both Caddy and PHP are working correctly.
Troubleshooting Tips
If you encounter issues, here are some troubleshooting steps:
- Check Caddy logs:
journalctl -u caddy --no-pager | tail
- Verify file permissions in your web root directory
- Ensure your domain’s DNS is correctly configured
- Check firewall settings to allow incoming traffic on ports 80 and 443
Advanced Configurations (Optional)
Caddy offers powerful features for more complex setups. Here are some advanced configuration examples:
Reverse Proxy Setup
To use Caddy as a reverse proxy for another service:
your-domain-name.com {
reverse_proxy localhost:8080
}
Load Balancing Example
For load balancing between multiple backend servers:
your-domain-name.com {
reverse_proxy /api localhost:8080 localhost:8081 {
lb_policy round_robin
}
}
Congratulations! You have successfully installed Caddy. Thanks for using this tutorial for installing the Caddy web server on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Caddy website.