How to create your own Caddy web server in Linux

Caddy is a modern, easy-to-use web server for Linux. It works by streamlining the process of creating website configuration files and SSL certificates.

Caddy is a modern, easy-to-use web server for Linux. It works by streamlining the process of creating website configuration files and SSL certificates. This article will guide you through the process of installing Caddy on an Ubuntu Linux server and how to use Caddy to deploy a simple website as well as an SSL reverse proxy.

Why should you use Caddy as a web server?

Although Nginx and Apache are powerful web server daemons, they can be difficult and complicated to use for new users. Caddy cuts that complexity by providing 'Caddyfile', a single flat file with a simple syntax that's easy to learn even for beginners.

How to create your own Caddy web server in Linux Picture 1How to create your own Caddy web server in Linux Picture 1

Another attractive feature of Caddy is that it provides HTTPS to your web server instantly. This helps users who find setting up SSL for their website difficult and complicated. So, Caddy is the perfect choice if you are looking for a 'noisy' web server in Linux that is both easy to maintain and easy to use.

Install Caddy

The first step in installing Caddy on Ubuntu Linux is to ensure that you have the tools to import its repository information and keys:

sudo apt install curl debian-keyring debian-archive-keyring

Fetch the key repository for Caddy from the developer's website:

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

Download and save the Caddy project repository file to the 'sources.list.d' folder on your device:

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

Reload your system's apt repository by running the following command:

sudo apt update && sudo apt upgrade

Install the Caddy package to your system using apt install:

sudo apt install caddy

Launch your first caddy site

To run your first website, create your website's root directory in your home directory:

mkdir ~/my-first-website && cd ~/my-first-website

Create an index.html file using your favorite text editor:

nano ./index.html

Paste the following block of code into your new HTML file:

 Hello World! 

Hello World!

Hello MakeTechEasier!

Save your index.html file, then run the following command:

caddy file-server --listen :8080

Confirm that your web server is working properly by navigating to it using your browser.

How to create your own Caddy web server in Linux Picture 2How to create your own Caddy web server in Linux Picture 2

Create a website using Caddyfile

While the CLI tool is great for serving simple websites, Caddy also offers an easy-to-use 'Caddyfile' for more complex setups. To get started, create a new Caddyfile in '/etc/caddy' using your favorite text editor:

sudo rm /etc/caddy/Caddyfile && sudo nano /etc/caddy/Caddyfile

Paste the following block of code into your new Caddyfile:

your-domain.com { redir https://www.{host}{uri} } www.your-domain.com { root * /var/www/html file_server }

Note : You can also host a LAN-only website using Caddyfile by replacing 'your-domain.com' with 'localhost'.

Copy the index.html file from the home directory to the system's '/var/www' :

sudo mkdir /var/www/html/ sudo cp ~/my-first-website/index.html /var/www/html

Go to your DNS registry and make sure that your www and root subdomains have A or AAAA records pointing to your machine's IPv4 and IPv6 addresses.

How to create your own Caddy web server in Linux Picture 3How to create your own Caddy web server in Linux Picture 3

Note : You do not need to adjust any DNS settings for LAN-only sites because Caddy will generate a self-signed certificate for that site.

Activate the integrated Caddy service on your device:

sudo systemctl enable --now caddy

Check if your site is working properly by navigating to your domain.

How to create your own Caddy web server in Linux Picture 4How to create your own Caddy web server in Linux Picture 4

Create SSL Reverse Proxy with Caddy

Like Nginx and Apache, you can also use Caddy as a reverse proxy for internal services on your machine. To do this, open Caddyfile in your system:

sudo nano /etc/caddy/Caddyfile

Paste the following code block into your Caddyfile:

your-domain.com { reverse_proxy 127.0.0.1:LOCAL-PORT }

Replace 'LOCAL-PORT' with your web application port. In this case, the code will be replaced with 3001 to redirect all traffic to the Kuma Uptime server.

How to create your own Caddy web server in Linux Picture 5How to create your own Caddy web server in Linux Picture 5

Save your Caddyfile, then reload the Caddy service to apply your new settings:

sudo systemctl reload caddy

Check if your reverse proxy is working properly by navigating to your domain using a web browser.

How to create your own Caddy web server in Linux Picture 6How to create your own Caddy web server in Linux Picture 6

4 ★ | 1 Vote