Setup Caddy Web Server on Azure/AWS/GCP

Setup and install Caddy web server on Ubuntu 24.04 in the cloud on Azure, AWS or Google GCP.  Deploy using the public image available on any of the cloud marketplaces below.  Caddy simplifies serving static and dynamic web content, acting as a reverse proxy, and automatically obtaining and renewing SSL certificates through Let’s Encrypt. 

Cloud Caddy Web Server

Caddy Web Server Azure

caddy web server azure

Deploy Caddy Web Server on Ubuntu 24.04 in Azure

Caddy Web Server AWS

Coming Soon…

Caddy Web Server GCP

Coming Soon…

Caddy Features

  • Serving static websites with automatic HTTPS setup.
  • Acting as a reverse proxy for dynamic web applications.
  • Managing SSL/TLS certificates automatically for multiple domains.
  • Load balancing traffic across multiple backend servers.
  • Running self-hosted services securely with HTTPS.
  • Proxying requests to internal APIs and services.

Getting Started with Caddy Web Server

Once your Caddy server has been deployed, the following links explain how to connect to a Linux VM:

 

 

Once connected and logged in, the following section explains how to start using Caddy to setup your web server.

Check Caddy Status and Version

To confirm Caddy is running and the version, run the following commands:

				
					sudo systemctl status caddy

				
			
				
					caddy version

				
			

Understanding Caddy’s Default Setup

Caddy runs as a service and listens on port 80 by default. When a valid domain name is provided, Caddy will automatically obtain and configure an SSL/TLS certificate for HTTPS through Let’s Encrypt.

Key Configuration Files:

  • Caddyfile: The configuration file for Caddy is located at /etc/caddy/Caddyfile. This file defines how Caddy should serve your websites.
  • Logs: Caddy logs can be viewed in the system journal:
				
					sudo journalctl -u caddy

				
			

Basic Caddy Configuration (Caddyfile)

The primary configuration file for Caddy is called the Caddyfile. You can edit it to define how Caddy should serve content for your domain or IP address.

 

To open and edit the Caddyfile:

				
					sudo nano /etc/caddy/Caddyfile

				
			

Here’s a simple example of a Caddyfile configuration to serve a static website:

				
					yourdomain.com {
    root * /var/www/yourdomain
    file_server
}

				
			

Explanation:

 

  • yourdomain.com: The domain that Caddy will serve (replace with your actual domain).
  • root: Specifies the directory where your website files are stored. In this case, it’s /var/www/yourdomain.
  • file_server: Instructs Caddy to serve files from the directory.

 

After saving your configuration, restart Caddy to apply the changes:

				
					sudo systemctl reload caddy

				
			

Serve Static Files with Caddy

Let’s create a simple website that Caddy will serve. Follow these steps:

 

  1. Create a directory for your site:
				
					sudo mkdir -p /var/www/yourdomain

				
			

2. Create an index.html file in that directory:

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

				
			

3. Add some HTML content:

				
					<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to Caddy</title>
</head>
<body>
    <h1>Hello from Caddy Web Server!</h1>
    <p>This is a basic static website served by Caddy.</p>
<script>class RocketElementorAnimation{constructor(){this.deviceMode=document.createElement("span"),this.deviceMode.id="elementor-device-mode-wpr",this.deviceMode.setAttribute("class","elementor-screen-only"),document.body.appendChild(this.deviceMode)}_detectAnimations(){let t=getComputedStyle(this.deviceMode,":after").content.replace(/"/g,"");this.animationSettingKeys=this._listAnimationSettingsKeys(t),document.querySelectorAll(".elementor-invisible[data-settings]").forEach(t=>{const e=t.getBoundingClientRect();if(e.bottom>=0&&e.top<=window.innerHeight)try{this._animateElement(t)}catch(t){}})}_animateElement(t){const e=JSON.parse(t.dataset.settings),i=e._animation_delay||e.animation_delay||0,n=e[this.animationSettingKeys.find(t=>e[t])];if("none"===n)return void t.classList.remove("elementor-invisible");t.classList.remove(n),this.currentAnimation&&t.classList.remove(this.currentAnimation),this.currentAnimation=n;let s=setTimeout(()=>{t.classList.remove("elementor-invisible"),t.classList.add("animated",n),this._removeAnimationSettings(t,e)},i);window.addEventListener("rocket-startLoading",function(){clearTimeout(s)})}_listAnimationSettingsKeys(t="mobile"){const e=[""];switch(t){case"mobile":e.unshift("_mobile");case"tablet":e.unshift("_tablet");case"desktop":e.unshift("_desktop")}const i=[];return["animation","_animation"].forEach(t=>{e.forEach(e=>{i.push(t+e)})}),i}_removeAnimationSettings(t,e){this._listAnimationSettingsKeys().forEach(t=>delete e[t]),t.dataset.settings=JSON.stringify(e)}static run(){const t=new RocketElementorAnimation;requestAnimationFrame(t._detectAnimations.bind(t))}}document.addEventListener("DOMContentLoaded",RocketElementorAnimation.run);</script></body>
</html>

				
			

4. Save and exit the file and reload reload caddy.

				
					sudo systemctl reload caddy

				
			

Now, Caddy will serve this static page when you visit yourdomain.com or the IP address of your server.

Enabling Automatic HTTPS for Caddy

Caddy’s most powerful feature is automatic HTTPS. Caddy automatically handles SSL certificates via Let’s Encrypt for any valid domain you configure.

 

To enable HTTPS, simply provide a valid domain in your Caddyfile. For example:

				
					yourdomain.com {
    root * /var/www/yourdomain
    file_server
}

				
			

Reload Caddy.

				
					sudo systemctl reload caddy

				
			

When Caddy starts, it will:

 

  1. Automatically request a certificate from Let’s Encrypt.
  2. Automatically renew the certificate before it expires.
  3. Redirect HTTP traffic to HTTPS.

 

To check if HTTPS is working, visit https://yourdomain.com in your browser.

Redirect HTTP to HTTPS (Optional)

By default, Caddy automatically redirects HTTP traffic to HTTPS. However, if you want to manually control this behavior or redirect from www to non-www (or vice versa), you can explicitly define redirects in your Caddyfile.

 

For example, redirect www.yourdomain.com to yourdomain.com:

				
					www.yourdomain.com {
    redir https://yourdomain.com{uri}
}

yourdomain.com {
    root * /var/www/yourdomain
    file_server
}

				
			

Adding Multiple Sites

You can use Caddy to serve multiple websites by adding more entries in the Caddyfile. Here’s how you can configure two sites in the same file:

				
					example1.com {
    root * /var/www/example1
    file_server
}

example2.com {
    root * /var/www/example2
    file_server
}

				
			

After saving the changes, reload Caddy:

				
					sudo systemctl reload caddy

				
			

Caddy will now serve both example1.com and example2.com from different directories.

Reverse Proxy with Caddy

Caddy can act as a reverse proxy to route requests to backend servers. This is useful for load balancing or proxying to other web applications.

 

Here’s a simple reverse proxy configuration for yourdomain.com that proxies traffic to an internal service running on localhost:8080:

				
					yourdomain.com {
    reverse_proxy localhost:8080
}

				
			

After saving the configuration, reload Caddy:

				
					sudo systemctl reload caddy

				
			

Caddy will now forward traffic from yourdomain.com to the application running on localhost:8080.

Caddy Web Server Access Logs and Monitoring

To monitor Caddy and check for issues, you can inspect the logs:

				
					sudo journalctl -u caddy

				
			

This will show the recent logs of Caddy, including any errors or certificate-related messages from Let’s Encrypt.

 

You can also enable more detailed logging by configuring Caddy’s access logs in the Caddyfile. Add this line within a site configuration to enable access logging:

				
					log {
    output file /var/log/caddy/access.log
    format json
}

				
			

This will log all requests to /var/log/caddy/access.log.

Manage Caddy Service

Caddy is installed as a systemd service, so you can manage it like any other service. Common commands include:

Start Caddy

				
					sudo systemctl start caddy

				
			

Stop Caddy

				
					sudo systemctl stop caddy

				
			

Restart Caddy

				
					sudo systemctl restart caddy

				
			

Reload Caddy (After making configuration changes)

				
					sudo systemctl reload caddy

				
			

Check Caddy Status

				
					sudo systemctl status caddy

				
			

Updating Caddy Web Server

To update Caddy to the latest version, you can use the following commands:

 

  1. Update the package list:
				
					sudo apt update

				
			

2. Upgrade Caddy Web Server

				
					sudo apt upgrade caddy

				
			

Documentation / Support

For further details and documentation refer to the following:

 

https://caddyserver.com/docs/

 

If you’re having any issues with deployment please contact us for support.

Firewall Ports

Caddy web server uses the following portS.

 

  • TCP 80
  • TCP 443

 

The links below explain how to modify / create firewall rules depending on which cloud platform you are using.

 

To setup AWS firewall rules refer to – AWS Security Groups

To setup Azure firewall rules refer to – Azure Network Security Groups

To setup Google GCP firewall rules refer to – Creating GCP Firewalls

Disclaimer: Caddy® is a registered trademark of Light Code Labs, LLC and is licensed under Apache License 2.0 license. The license comes with a “no warranty” clause, meaning the software is provided “as-is” without any guarantees or liability for issues that may arise.

Avatar for Andrew Fitzgerald
Andrew Fitzgerald

Cloud Solution Architect. Helping customers transform their business to the cloud. 20 years experience working in complex infrastructure environments and a Microsoft Certified Solutions Expert on everything Cloud.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x