How to Install Caddy in Ubuntu

This brief tutorial shows students and new users how to install and configure Caddy server in Ubuntu 20.04 | 18.04 LTS.

Caddy is an open source, powerful, extensible platform written in Go that can be used to serve your sites, services, and apps. It is not a simple web server. It can be used as a reverse proxy, load balancer, gateway, ingress controller, task scheduler and more.

Caddy is lightweight and operates primarily at L4 (transport layer) and L7 (application layer) of the OSI model.

Whether you’re hosting a simple website or running a production ready web apps, Caddy is a great please to start when looking for alternative to your current configurations.

For student or new user looking for a Linux system use, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for beginners and folks looking for easier Linux distribution to use.

Ubuntu is an open source Linux operating systems that runs on desktops, laptops, server and other devices.

To get started with install Caddy on Ubuntu, follow the steps below:

Install from official Repository

There are many ways to install Caddy in Ubuntu. One way is to install Caddy is from its official repository. To do that, use the steps below:

First, run the commands below to install supported packages.

sudo apt update
sudo apt install debian-keyring debian-archive-keyring apt-transport-https curl

After that, add Caddy package repository key to Ubuntu by running the commands below:

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo apt-key add -

When you’re done, create a repository file by running the commands below:

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list

Finally, run the commands below to update Ubuntu package lists and install Caddy.

sudo apt update 
sudo apt install caddy

Installing this package automatically starts and runs Caddy for you as a systemd service named caddy using our official caddy.service unit file.

After installing Caddy, the commands below can be used to stop, start and enable Caddy service to automatically start up when the server boots.

sudo systemctl stop caddy
sudo systemctl start caddy 
sudo systemctl enable caddy

To verify that Caddy is running, run the commands below:

sudo systemctl status caddy

That should display similar lines as shown below:

● caddy.service - Caddy
     Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset:>
     Active: active (running) since Thu 2021-05-20 13:38:08 CDT; 13s ago
       Docs: https://caddyserver.com/docs/
   Main PID: 4136 (caddy)
      Tasks: 6 (limit: 4653)
     Memory: 9.9M
     CGroup: /system.slice/caddy.service
             └─4136 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

May 20 13:38:08 ubuntu2004 caddy[4136]: JOURNAL_STREAM=8:55448
May 20 13:38:08 ubuntu2004 caddy[4136]: {"level":"info","ts":1621535888.2488842>
May 20 13:38:08 ubuntu2004 caddy[4136]: {"level":"info","ts":1621535888.2514222>

Caddy is running the functioning.

Open the browser on the server and browse to the server hostname or IP address. You should see Caddy default web page message.

Configuring Caddy

To serve web pages via Caddy, you must define the root directory to serve the files from.

By default Caddy configuration file is located at /etc/caddy/Caddyfile.

Run the commands below to open Caddy default configuration file.

sudo nano /etc/caddy/Caddyfile

Then add the lines below to reference the website files you want to serve on the server.

http://.example.com {
    root * /var/www/html
    encode gzip
    file_server
}

This is a basic Caddy config, and declares that all HTTP traffic to your server should be served with files file_server from /var/www and compressed using gzip to reduce page loading times on the client side.

Save the file and exit.

Reload Caddy service and browse to the domain to test.

That should do it!

Conclusion:

This post showed you how to install Caddy server on Ubuntu. If you find any error above, please use the comment form below to report.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.