How to Create Your Own Caddy Webserver in Linux

by Marlon Altenwerth
21 minutes read

How to Create Your Own Caddy Webserver in Linux

A photograph of a computer filter on top of a table with a green backlight.

Caddy is a vibrant, easy-to-capitalize internet internet server for Linux. It jobs by centralizing the procedure of gaining landmark arrangement documents and also SSL credentials. This compose-upward will guide you wearing the procedure of installing Caddy on an Ubuntu Linux internet server. Even more, it will moreover illustrate you how to capitalize Caddy to release a simplified internet internet page as seamlessly as an SSL rescind proxy.

Textile
  • Why Utilise Caddy as a Webserver?
  • Placing Caddy
  • Sprinting Your Initially Caddy Internet site
  • Unleashing an SSL Reverse Proxy with Caddy
  • Deploying Multiple Websites and also Services gaining capitalize of Caddy

Why Utilise Caddy as a Webserver?

While Nginx and also Apache are strong internet internet server daemons, they can be unwieldy and also intricate for a brand-neoteric user. Caddy cuts that intricacy by letting loose the “Caddyfile”, a single flat document with a simplified syntax that is easy to locate out even for amateurs.

A incurable confirming an instance Caddyfile.

Another selling time of Caddy is that it lends HTTPS for your internet internet server right out of the box. This renders it handy for users that locate prepping SSL for their site discouraging and also taxing. As such, Caddy is faultless if you’re gazing for a “no-hardship” internet internet server in Linux that’s both easy to preserve and also capitalize.

Fantastic to realize: locate out how you can recover the “404 Not Detected” error in Nginx.

Placing Caddy

The initially interfere installing Caddy in Ubuntu Linux is to administer sure that you have the devices to import its repository primes and also details:

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

Fetch the repository finalizing approach for Caddy from the programmer’s site:

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 also preserve the Caddy project’s repository document to your machine’s “sources.list.d” magazine:

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

Reload your contraption’s proper databases by running the working together with:

sudo apt update && sudo apt upgrade

Elbowroom the Caddy unit for your contraption gaining capitalize of apt install:

sudo apt install caddy

Sprinting Your Initially Caddy Internet site

To rushed your initially site, inflict your landmark’s origin folder in your residence magazine:

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

Develop an index.html document gaining capitalize of your favorite text editor:

nano ./index.html

Paste the working together with block of code inside your brand-neoteric HTML document:

</span> > > > Hello World! > > >   

>

Hello World! >

>

Hello MakeTechEasier! > > >

Save your index.html document, after that rushed the working together with command:

caddy file-server --listen :8080

Attest that your webserver is kneading accordingly by searching to it gaining capitalize of your browser.

A screenshot confirming a instance site running on localhost:8080.

Unleashing a Internet site Making capitalize of Caddyfiles

While the CLI tool is exhilarating for offering simplified webpages, Caddy moreover lends an easy-to-capitalize “Caddyfile” for much more intricate setups. To embark, inflict a brand-neoteric Caddyfile under “/etc/caddy” gaining capitalize of your favorite text editor:

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

Paste the working together with block of code inside your brand-neoteric Caddyfile:

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

Note: You can moreover host a LAN-single site gaining capitalize of Caddyfiles by replacing “your-domain name.com” with “localhost.”

Photocopy the index.html document from your residence magazine to your contraption’s “/var/www”:

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

Attend your DNS registrar and also administer sure that your origin and also www subdomains has either an A or AAAA paper aiming to your machine’s IPv4 and also IPv6 addresses.

A screenshot confirming 2 A records aiming to the IP address of the machine.

Note: You wear’t ultimatum to tweak any kind of DNS positions for LAN-single portals because Caddy will accumulate a self-signed credentials for it.

Allow the erupted-in Caddy solution on your machine:

sudo systemctl enable --now caddy

Check if your site is kneading accordingly by searching to your domain name name.

A screenshot confirming the instance site kneading with an outward domain name name.

Unleashing an SSL Reverse Proxy with Caddy

Simply like Nginx and also Apache, you can moreover capitalize Caddy as a rescind proxy for an inner solution on your machine. To do this, amenable your contraption’s Caddyfile:

sudo nano /etc/caddy/Caddyfile

Paste the working together with block of code inside your Caddyfile:

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

Replace “LOCAL-PORT” with the satire of your internet entreaty. In my pill, I will readjust mine with 3001 to reroute unanimously incoming traffic to my Uptime Kuma internet server.

A incurable confirming a switched over Caddy rescind proxy arrangement.

Save your Caddyfile, after that reload the Caddy solution to apply your brand-neoteric positions:

sudo systemctl reload caddy

Test if your rescind proxy is kneading accordingly by searching to your domain name gaining capitalize of your internet browser.

A screenshot confirming an Uptime Kuma instance being proxied wearing Caddy.

Fantastic to realize: locate out much more around internet encryption by gaining your disturbingly own SSL credentials gaining capitalize of OpenSSL.

Deploying Multiple Websites and also Services gaining capitalize of Caddy

You can moreover capitalize the disturbingly same Caddyfile to serve both portals and also proxies under the disturbingly same host. This renders it easy to juggle innumerable different options without cluttering your webserver’s config magazine.

To embark, attend Caddy’s Download internet page after that kind “caddy-dns” on the comb bar.

A screenshot accentuating the caddy-dns comb box on the Caddy Download internet page.

Appearance for the DNS use courier that steals treatment of your domain name name. In my pill, I’m gaining capitalize of DigitalOcean.

Pick your DNS use courier, after that click “Download” on the internet page’s upper right alcove. This will download a custom Caddy binary with the handy part for your DNS use courier.

A screenshot accentuating the Download switch after picking the caddy-dns build for DigitalOcean.

Stop the Caddy solution gaining capitalize of systemctl:

sudo systemctl disable --now caddy.service

Develop a substitution of the original Caddy binary document, after that duplicate the custom binary to your “/usr/bin/” magazine.

cp /usr/bin/caddy ~/caddy.bin.bak sudo rm /usr/bin/caddy sudo cp ~/Downloads/caddy_linux_amd64_custom /usr/bin/caddy sudo chmod +x /usr/bin/caddy

Test if your custom Caddy binary is kneading accordingly by inspecting its indication:

caddy --version

Enabling Caddy SSL for Wildcard Domain names

Open your contraption’s Caddyfile gaining capitalize of your favorite text editor:

sudo nano /etc/caddy/Caddyfile

Paste the working together with block of code inside your Caddyfile:

*.your-domain.com { 	tls { 		dns digitalocean API-KEY     # Replace the value of digitalocean with your DNS provider. 	}   	@www host www.your-domain.com 	handle @www {           root * /var/www/html           file_server 	}   	@subdomain host subdomain.your-domain.com 	handle @uptime { 		reverse_proxy 127.0.0.1:LOCAL-PORT 	}   	handle { 		abort 	} }

Avail an API approach from your DNS use courier. In DigitalOcean, you can administer capitalize this by visiting your DigitalOcean control panel, after that clicking API on the internet page’s disowned sidebar.

A screenshot accentuating the API switch on the DigitalOcean control panel.

Click Amass Modern Token, after that supply the precisions for your API approach.

A screenshot confirming the token's precisions for automated SSL wildcard credentials.

Photocopy your API approach, after that paste it on the “API-KEY” variable in your Caddyfile.

Individualize the Caddyfile for your certain configuration, after that preserve it.

A incurable confirming a switched over multi-landmark Caddy arrangement document.

Earn sure that your domain name name has the handy A and also AAAA records for your domain name and also subdomain.

Restart your diluted Caddy daemon to apply your brand-neoteric positions:

sudo systemctl enable --now caddy

Test if your “multi-solution” configuration jobs accordingly by opening both domains on a internet browser.

A screenshot confirming the 2 options running under SSL with Caddy.

Situating out how to release simplified and also peg internet options gaining capitalize of Caddy is simply the initially interfere touring the remarkable planet of self-arranging in Linux. Dive deeper into this universe by gaining your disturbingly own e-mail pen names internet server with SimpleLogin.

Image credit: Jonathan Ybema via Unsplash and also Wikimedia Commons. With one voice fluctuations and also screenshots by Ramces Red.

Related Posts