DEV Community

Cover image for Effortless Web Hosting with Caddy: A Beginner’s Guide
Franklin Thaker
Franklin Thaker

Posted on

Effortless Web Hosting with Caddy: A Beginner’s Guide

In the ever-evolving world of web servers, Caddy has emerged as a game-changer. Designed for simplicity and modern workflows, it offers automatic HTTPS, straightforward configuration, and a developer-friendly approach.

What is Caddy Server?

Caddy is a modern, open-source web server written in Go. It’s known for its ability to handle complex web hosting tasks effortlessly. Whether you’re hosting a static site, reverse proxying APIs, or managing secure connections, Caddy simplifies the process with features like:

  • Automatic HTTPS: Get secure connections by default using Let's Encrypt.
  • Readable Configs: Manage sites with a simple Caddyfile—no cryptic syntax required.
  • Built-in Reverse Proxy: Handle API routing and load balancing without extra tools.

Why Choose Caddy?

Compared to traditional servers like Apache and Nginx, Caddy is:

  • Easier to Use: Minimal setup and automatic configurations.
  • Secure by Default: HTTPS is enabled automatically.
  • Lightweight Yet Powerful: Written in Go for speed and scalability.

Quick Setup

  1. Download the version according to your OS -> https://caddyserver.com/download

  2. Create a Caddyfile in your server's directory.

# Example Caddyfile

localhost {
    reverse_proxy localhost:8080
}

Enter fullscreen mode Exit fullscreen mode
  1. Start the Server:
    • sudo caddy start -> to run the server in the background
    • sudo caddy stop -> to stop the background running caddy server
    • sudo caddy run -> to run the server in foreground

When to Use Caddy?

  • Static Websites: Host portfolios, blogs, or landing pages with ease.
  • APIs and Microservices: Use built-in reverse proxying for seamless API management.
  • HTTPS Needs: Say goodbye to manual SSL certificate management.

Quick Example:

// app.js
const express = require('express');
const app = express();
const port = 8080;

app.get('/', (req, res) => {
  res.send('Hello from Express server!');
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

Enter fullscreen mode Exit fullscreen mode
# Caddyfile

localhost {
    reverse_proxy localhost:8080
}

Enter fullscreen mode Exit fullscreen mode

node app.js -> This will start your actual backend server in 8080 PORT
sudo caddy run -> This will start the caddy server and you will be able to handle all requests at localhost:443

Extra tips

  • For Linux, once you get the binary from official site, you can rename that to "caddy" and then move the file to "/usr/local/bin/"
  • Then change the permissions. sudo chmod ugoa+x caddy
  • Then you will be able to access "caddy" via CLI anywhere in your system

Conclusion

Caddy server is perfect for developers looking for a modern, no-hassle web server. Whether you’re a beginner or a seasoned professional, its features and simplicity make it an excellent choice for most projects.

Top comments (0)