## Tutorial ##

How To Install Caddy on Ubuntu 22.04

Published on May 05 2023 · Updated on May 05 2023

In this tutorial, we'll explain you how to install Caddy on Ubuntu 22.04. We will guide you with the installation and basic configuration process of the Caddy web server.

The Caddy web server is an open-source web server written in Go. It is designed around simplicity and security that comes with a number of features that are useful for hosting websites. Caddy is both a flexible, efficient static file server and a powerful, scalable reverse proxy

Prerequisites:

  • A Ubuntu 22.04 installed dedicated server or KVM VPS.
  • A root user access or normal user with the sudo privileges.

Install Caddy on Ubuntu

1. Update the server

Run following command to update the Ubuntu server:

apt update && apt upgrade -y

2. Install Dependencies

We need to install some dependencies before we install Caddy.

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

3. Install Caddy web server

First, add GPG key using following command:

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg

Next, add respository and update it.

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

apt update


Finally, install Caddy using following command:

apt install caddy


Now, navigate to your browser and enter your server IP or domain name:

http://Server-IP
OR
http://example.com

 

4. Configure Domain with Caddy

Before moving following, first set up domain’s A/AAAA  DNS record at your registrar or control panel.

Note: Replace hostnextra.com with your domain name

Create a directory for your website files

mkdir -p /var/www/hostnextra.com

Next, if you are using SELinux than you need to change the file security context for web content. (Optional)

chcon -t httpd_sys_content_t /var/www/hostnextra.com -R
chcon -t httpd_sys_rw_content_t /var/www/hostnextra.com -R

Now, open Caddy’s configuration file and add your domain name and change website’s root directory.

nano /etc/caddy/Caddyfile

Replace :80 with your domain name and change the site root to /var/www/hostnextra.com as

Once you done with the changes reload the caddy.service to reflect the changes.

systemctl restart caddy

Now, create a index.html file in /var/www/html/hostnextra.com using following command:

echo '<!doctype html><head><title>Hello from Caddy!</title></head><body><h1 style="font-family: sans-serif">This page is being served via Caddy</h1></body></html>' | sudo tee /var/www/html/hostnextra.com/index.html

Finally, refresh the page in your browser and you will see our newly created index.html.

In this article, we have seen how to install Caddy on Ubuntu 22.04.

Related Tutorials

How To Install Maven on Ubuntu 22.04 - HostnExtra

We'll explain how to install Maven on Ubuntu 22.04. For this demonstration purpose, we're installing current LTS version JDK 17.

How To Install Elasticsearch on Rocky Linux 9 - HostnExtra

We'll explain you how to install Elasticsearch on Rocky Linux 9. We'll perform CRUD operation using RESTful API. It's a distributed, RESTful and analytics.

How To Install Elasticsearch on AlmaLinux 9 - HostnExtra

We'll explain you how to install Elasticsearch on AlmaLinux 9. We'll perform CRUD operation using RESTful API. It's a distributed, RESTful search and analytics.