Install Caddy Web Server on Ubuntu 22.04

Now that you have landed here, let me show you how to install Caddy Web Server on Ubuntu 22.04. Caddy is a powerful and open-source web server written in Go programming language. It can can used use to host simple applications to complex apps in production environment. It has great security features with automatic HTTPS and fast than other web servers utilizes the power of CPUs.

Having familiarized yourself with the software, continue with the rest of the tutorial to install Caddy Web Server on Ubuntu 22.04.

Install Caddy Web Server on Ubuntu 22.04

There are many ways to install Caddy Web Server on Ubuntu 22.04. They include:

  • Through Docker
  • Official repositories
  • Compiling from source

You will learn how to install Caddy Web Server on Ubuntu 22.04 using the official repositories.

Install Caddy Web Server on Ubuntu 22.04 using the official repositories

Update system packages to avoid dependency problems:

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

Install Caddy repository GPG key

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
> /etc/apt/trusted.gpg.d/caddy-stable.asc

Add Caddy package repository key to Ubuntu.

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

Update system package cache once more:

sudo apt update

Install Caddy Web Server on Ubuntu 22.04

sudo apt install caddy

Caddy web server will be up and running after installation.

Run the command below to check the version of Caddy validating that is is installed:

caddy version

To check the status, run the command below.

sudo systemctl status caddy

You can always start Caddy web server service by:

sudo systemctl start caddy

Stop Caddy web server by the command:

sudo systemctl stop caddy

Enable Caddy web server service by executing the command:

sudo systemctl enable caddy

Restart;

sudo systemctl restart caddy

Test Caddy Web Server

Open your browser and type in a new tab localhost. If you see the screen below, that means Caddy running properly:

You have managed to install Caddy Web Server on Ubuntu 22.04.

To allow non-root user to bind to port 80 and 443, run the following command in your terminal:

sudo ufw allow proto tcp from any to any port 80,443

Serving your site using Caddy Web Server

Assuming our site will be stored under the folder /var/www/html/caddy or create the directory.

sudo mkdir -p /var/www/html/caddy

Assign your site files to caddy group”

sudo chown -R caddy:caddy /var/www/html/caddy

Now create a simple HTML named index.html under you website directory i.e. /var/www/html/caddy

sudo nano /var/www/html/caddy/index.html

Copy the content below into the file and save it.

<title>Hello from Caddy!</title>

<p style="font-family: sans-serif">
This page tests that Caddy Web Server works. Congratulations, It works! </p>
<h2>Thank you for visiting Itnixpro.com</h2>

Configure the Caddyfile

Open the Caddyfile configuration file you created earlier for editing:

sudo nano /etc/caddy/Caddyfile

Add the following lines. You can replace localhost with the domain name of your site.

localhost {
    root * /var/www/html/caddy
    encode gzip
    file_server 
}

This file tells Caddy to direct all HTTP traffic to your server and be served with files (file_server) from /var/www and compressed using gzip to reduce page loading times on the client side.

Now reload the restart Caddy Web server:

sudo systemctl reload caddy

Visit your browser and type in a new tab: localhost

The page you created loads:

You can also setup you site using a CMS like WordPress, Drupal etc.

Conclusion

That is the end of the guide on how to install Caddy Web Server on Ubuntu 22.04.

Read more on Caddy Server official website and see some tutorials of using Caddy Web Server in Caddy Web Server documentation.

More similar interesting tutorials

Install NFS Server on Ubuntu 22.04

Install Apache HTTP Server on Debian 11

Install Cacti on Ubuntu 22.04

Install Nginx on Debian 11

Android Developer | Linux | Technical Writer | Backend Developer

Leave a Comment