Install Caddy 2 web server on Ubuntu 20.04 Guide

This guide is intended to show the steps of how to install and set up Caddy 2 on Ubuntu 20.04.

Why Caddy?

From CaddyServer.com,

Caddy simplifies your infrastructure. It takes care of TLS certificate renewals, OCSP stapling, static file serving, reverse proxying, Kubernetes ingress, and more. Caddy is the only web server to use HTTPS automatically and by default.

Caddy is much easier to configure and set up than Apache, or ngix, especially if you want to set up a website with HTTPS in minutes.

Download Caddy 2 on Ubuntu

The download is pretty straightforward.

$ wget https://github.com/caddyserver/caddy/releases/download/v2.0.0/caddy_2.0.0_linux_amd64.tar.gz
$ tar zxvf caddy_2.0.0_linux_amd64.tar.gz
$ rm caddy_2.0.0_linux_amd64.tar.gz | rm LICENSE | rm README.md
$ sudo mv caddy /usr/bin/

Create User and Group for Caddy

The goal is to allow the web server to be ran by a dedicate user, named caddy, within the group, also called caddy. This way, the web server’s permission can be separate than the current user.

$ sudo groupadd --system caddy
$ sudo useradd --system \
--gid caddy \
--create-home \
--home-dir /var/lib/caddy \
--shell /usr/sbin/nologin \
--comment "Caddy web server" \
caddy

Set up website directory

After setting up the user, we can create a folder to host files our website.

$ sudo cp -R html /var/www/
$ sudo chown -R caddy:caddy /var/www/html
$ sudo chmod -R 555 /var/www/html

Create Caddyfile for Caddy v2

Note that in Caddy v2, a lot of syntax has changed from Caddy v1. I will provide a few sample Caddyfile in other blog posts, based on the usage (e.g. a WordPress server, a V2Ray proxy, etc.).

A Caddyfile can be as simple as below, that enables a web server to serve files from the /var/www/html directory.

yourdomain.com {
    encode zstd gzip
    root * /var/www/html
    
    # Enable the static file server.
    file_server 
}	
$ sudo mkdir /etc/caddy
$ sudo chown -R root:root /etc/caddy
$ sudo cp /path/to/Caddyfile /etc/caddy/
$ sudo chown root:root /etc/caddy/Caddyfile
$ sudo chmod 644 /etc/caddy/Caddyfile

Register Caddy as system service and auto-start

Since we are going to configure Caddy with Caddyfile, we are going to use the caddy.service.

$ wget https://raw.githubusercontent.com/caddyserver/dist/master/init/caddy.service
$ sudo cp -f caddy.service /etc/systemd/system/
$ sudo chown root:root /etc/systemd/system/caddy.service
$ sudo chmod 644 /etc/systemd/system/caddy.service
$ sudo systemctl daemon-reload
$ sudo systemctl enable caddy
$ sudo systemctl start caddy

Verify Caddy 2 is running

It’s easy to verify is Caddy is successfully running by $ systemctl status caddy

If anything goes wrong, you can check the logs

  • View log output: $ journalctl -u caddy
  • Jump to the latest log location: $ journalctl -f -u caddy.service

Or reload the Caddyfile if there are any changes made for them to be effective by running $ sudo systemctl reload caddy

Also, don’t forget to create proper port rules either in the local firewall or in the cloud, such as port 80 for HTTP, and port 443 for HTTPS.

Yay, cheers!

Any questions? Feel free to leave a comment below.

One Reply to “Install Caddy 2 web server on Ubuntu 20.04 Guide”

Leave a Reply