How to Set Up the Caddy Web Server to Do All the Work for You (Well, Almost)

Caddy

How to Set Up the Caddy Web Server to Do All the Work for You (Well, Almost)

Ever wanted to set up a web server but got lost in the labyrinth of configuration files? Enter Caddy. This server is more than just code; it’s practically your automated assistant. It handles everything you can possibly automate, from HTTPS to easy configurations. So, let’s get Caddy set up, sit back, and maybe even put the kettle on.

Step 1: Installing Caddy on Arch Linux, Fedora, or Ubuntu

Arch Linux

Arch Linux and pacman are like tea and biscuits. Open the terminal and simply type:

sudo pacman -S caddy

A few seconds, and boom! Caddy is standing by, ready to go.

Fedora

For Fedora, it’s just as simple. Type:

sudo dnf install caddy

And there you go! Caddy is installed—let’s move on.

Ubuntu

For Ubuntu, there are a few prep steps, but don’t worry. First, let’s add the Caddy repository:

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-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

Voilà! Caddy is installed and waiting for action.

Step 2: Making a Simple HTML Page So Caddy Has Something to Show

Before we test Caddy, let’s whip up a quick HTML page. Nothing fancy, just a “hello world.”

  1. Create a directory for your site—let’s call it my_website, then move into it:

    mkdir my_website
    cd my_website
    
  2. Inside this folder, create an index.html file—our masterpiece on a couple of lines:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Welcome to My Site!</title>
    </head>
    <body>
        <h1>Hello, World!</h1>
        <p>This is a simple site running through Caddy.</p>
    </body>
    </html>
    

There we have it! A page to test with. Now, let’s fire up Caddy to display it.

Step 3: Launching Caddy

Now, let’s give Caddy a whirl and see our HTML brilliance.

  1. Make sure you’re in the my_website folder, then start Caddy with:

    caddy file-server --listen :8080
    
  2. Open your browser, go to http://localhost:8080, and behold our “Hello, World!” If you can see it, then everything’s going according to plan.

Step 4: Setting Up Caddyfile for a Bit of Class

The Caddyfile is Caddy’s configuration file, which tells it exactly how to behave. Think of it as a set of polite instructions—no need to fuss over too many details.

  1. In your website’s root folder (e.g., my_website), create a Caddyfile:

    cd my_website
    touch Caddyfile
    
  2. Open the Caddyfile and add some simple commands:

    localhost:8080 {
        root * .
        file_server
    }
    

    Here’s the rundown: we’re telling Caddy the root folder is the current one (.) and to turn on the file server.

  3. Want to use a custom domain with HTTPS? Change the settings like so:

    yourdomain.com {
        root * /path/to/your/site
        file_server
    }
    

Caddy will automatically renew certificates, sparing you from wrestling with configurations.

Step 5: Running Caddy as a System Service So It’s Always On

Now, if you’d like Caddy to automatically start when your server boots up, you can set it up as a system service with systemd.

  1. Enable the service:

    sudo systemctl enable caddy
    
  2. Start it up:

    sudo systemctl start caddy
    
  3. Check if it’s running smoothly:

    sudo systemctl status caddy
    

    If the status says “active (running),” Caddy is on duty.

Step 6: Moving Caddyfile for systemd

By default, Caddy looks for the Caddyfile in /etc/caddy/, so let’s move it there:

sudo cp /path/to/your/Caddyfile /etc/caddy/Caddyfile

Then, restart Caddy to load the new settings:

sudo systemctl restart caddy

Step 7: Checking the Logs – Just in Case

To check the logs and make sure Caddy isn’t muttering complaints, use:

sudo journalctl -u caddy -f

Conclusion

That’s it! Caddy is now set up, running in the background, and will renew certificates on its own. You’re free as a bird to do other things while Caddy takes care of your site. If you’d like more tips or configuration ideas, definitely check out the official Caddy documentation.


See also