Switching to Caddy: A Viable Alternative to Nginx and Apache? Install and Run Your First Site
Caddy, a Nginx and Apache Alternative, is it the time to Switch?
Caddy is a powerful, open-source web server known for its simplicity, automatic HTTPS, and ease of use.
Written in Go, Caddy is unique because it automatically manages TLS certificates via Let's Encrypt, eliminating the need for manual configuration. It supports HTTP/2 and QUIC by default, offers a flexible configuration through its API, and can serve static files, proxy requests, and run as a reverse proxy.
Caddy is distinct from other web servers due to its user-friendly setup, strong security features, and modern protocol support, making it ideal for both developers and sysadmins.
Matthew Holt began developing Caddy in 2014 while studying computer science at Brigham Young University. (The name "Caddy" was chosen because this software helps with the tedious, mundane tasks of serving the Web, and is also a single place for multiple things to be organized together.) It soon became the first web server to use HTTPS automatically and by default, and now has hundreds of contributors and has served trillions of HTTPS requests.
Features
- Easy configuration with the Caddyfile
- Powerful configuration with its native JSON config
- Dynamic configuration with the JSON API
- Config adapters if you don't like JSON
- Automatic HTTPS by default
- ZeroSSL and Let's Encrypt for public names
- Fully-managed local CA for internal names & IPs
- Can coordinate with other Caddy instances in a cluster
- Multi-issuer fallback
- Stays up when other servers go down due to TLS/OCSP/certificate-related issues
- Production-ready after serving trillions of requests and managing millions of TLS certificates
- Scales to hundreds of thousands of sites as proven in production
- HTTP/1.1, HTTP/2, and HTTP/3 all supported by default
- Highly extensible modular architecture lets Caddy do anything without bloat
- Runs anywhere with no external dependencies (not even libc)
- Written in Go, a language with higher memory safety guarantees than other servers
- Actually fun to use
- Runtime dependencies
- So much more to discover
Supported Systems
Caddy runs on every major platform for which Go compiles.
- Linux
- Windows
- macOS
- FreeBSD
- OpenBSD
- NetBSD
- Android
Caddy VS Nginx VS Apache
Here's a comparison table highlighting the key differences between Caddy, Nginx, and Apache:
Feature | Caddy | Nginx | Apache |
---|---|---|---|
License | Apache 2.0 | BSD-2-Clause | Apache 2.0 |
Default TLS/SSL | Automatic with Let's Encrypt, built-in | Manual configuration required | Manual configuration required |
Configuration | Simple, with Caddyfile | Complex, with Nginx config files | Complex, with .conf files |
Ease of Use | Very easy to set up and manage | Moderate complexity | Moderate to high complexity |
Reverse Proxy | Supported, simple to configure | Supported, widely used | Supported, widely used |
Performance | High performance, especially with HTTPS | High performance | High performance, but slightly slower |
HTTP/2 and HTTP/3 | Supported by default | HTTP/2 supported by default, HTTP/3 optional | HTTP/2 supported, HTTP/3 via modules |
Dynamic Content | Requires external app server (e.g., PHP-FPM) | Requires external app server (e.g., PHP-FPM) | Built-in support for dynamic content (e.g., PHP) |
Modularity | Lightweight with built-in essential modules | Modular, but less extensive than Apache | Highly modular, with extensive modules |
Community and Support | Growing, active community | Large, well-established community | Large, well-established community |
Resource Usage | Low to moderate | Low | Moderate to high |
Logging and Monitoring | Basic built-in logging, external tools for monitoring | Extensive logging, third-party tools available | Extensive logging, built-in and third-party tools |
Deployment | Easy deployment, particularly with containers | Widely used in containers and cloud setups | Widely used, especially in legacy systems |
Platform Support | Cross-platform | Cross-platform | Cross-platform |
Key Takeaways:
- Caddy is ideal for those who want simplicity, automatic HTTPS, and modern protocol support with minimal configuration.
- Nginx is known for its high performance and flexibility, particularly in handling static content and acting as a reverse proxy.
- Apache remains a robust choice with extensive module support, making it suitable for complex, legacy systems that require specific configurations.
Each server has its strengths depending on your needs, and the choice may vary based on the specific requirements of your projects.
Install Caddy on Ubuntu and Setup your First Site
Caddy is a modern, lightweight web server that automatically handles HTTPS and is easy to configure.
This tutorial will guide you through installing Caddy on an Ubuntu server and setting up a website using it.
Update Your System
Before installing Caddy, ensure your system is up to date:
sudo apt update
sudo apt upgrade -y
Install Caddy
After adding the repository, install Caddy using the following command:
sudo apt update
sudo apt install caddy -y
This will install Caddy and configure it to run as a systemd service.
Add the Caddy APT Repository:Caddy provides an official APT repository for easy installation.
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
Set Up a Website with Caddy
Caddy’s configuration is simple and uses a file called Caddyfile
. This file is located in /etc/caddy/Caddyfile
by default.
Add a Sample HTML File:Add a simple index.html
file to test the setup:
echo "<!DOCTYPE html><html><body><h1>Hello, Caddy!</h1></body></html>" | sudo tee /var/www/html/index.html
Create Your Website Directory:Ensure the directory where your website files will be stored exists:
sudo mkdir -p /var/www/html
Edit the Caddyfile:Open the Caddyfile in a text editor:
sudo nano /etc/caddy/Caddyfile
Add the following configuration to serve a simple website:
example.com {
root * /var/www/html
file_server
}
Replace example.com
with your actual domain name. This configuration tells Caddy to serve files from the /var/www/html
directory.
Configure DNS
Point your domain to the IP address of your Ubuntu server using your domain registrar's DNS settings. Ensure the DNS A record is set correctly.
Restart Caddy
After setting up the Caddyfile and your DNS, restart Caddy to apply the configuration:
sudo systemctl restart caddy
Access Your Website
Open a web browser and navigate to http://example.com
(replace with your actual domain). Caddy will automatically request and configure HTTPS for your site.
End Note
Caddy is a modern, lightweight web server that automatically handles HTTPS and is easy to configure. This tutorial will guide you through installing Caddy on an Ubuntu server and setting up a website using it.
You have successfully installed Caddy on Ubuntu and set up a simple website with automatic HTTPS. Caddy's ease of configuration and built-in TLS management make it a strong choice for hosting websites with minimal setup effort.
License
Caddy is released under the Apache-2.0 License.