Collecting Moments

Live a life that you will remember

Install Caddy 2 web server on Ubuntu 20.04 Guide
Read Time:2 Minute, 42 Second

Install Caddy 2 web server on Ubuntu 20.04 Guide

0 0

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.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “Install Caddy 2 web server on Ubuntu 20.04 Guide

  1. […] Given Caddy 2 is a fairly young web server, the documentation around update existing the Caddy 2 installation is not very easy to find, especially when Caddy was not installed via any package manager. This guide aims to show you how to update Caddy to the latest version. If you have not installed Caddy, you can follow this guide to install Caddy. […]

Leave a Reply

Next Post

How to set up WordPress with Caddy 2

Fri Jun 12 , 2020
This step-by-step guide will show you how to set up a WordPress blog based on the Caddy 2 web server, on Ubuntu 20.04 LTS. In this guide, WordPress will be put inside the /wordpress folder of the website root. Caddy is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go. Caddy […]