Tech

How to Install Caddy Web Server on Ubuntu Server 22.04


If you are looking for a flexible, lightweight web host for static websites, web applications or containers, Caddy might be what you want. Jack Wallen shows you how to install this lightning-fast web host.

caddy-ubuntu-installtutorial
Image: standret / Adobe Stock

Caddy is a powerful open source web server, written in Go, that can be used to host web applications in production environments. Caddy has built-in automatic TLS certificate renewal, OSCP pinning, static file serving, reverse proxies, Kubernetes infiltration, and more. Caddy can be run as a standalone web server, an application server or even in containers.

In this tutorial, I will walk you through the steps of installing Caddy on Ubuntu Server 22.04 and then how to create a simple, static website.

SEE: Over 40 Linux and Open Source Terms you need to know (TechRepublic Premium)

What you need

To get Caddy bay up and running, you’ll need an instance of Ubuntu Server 22.04 and a user with sudo privileges. With the two in place, it’s time to install.

How to install Caddy

Login to your Ubuntu Server instance and add the required dependencies with:

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

Once the installation is complete, add the official Caddy GPG key with:

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

Create the repository file with the command:

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

apt update:

sudo apt-get update

Finally, install Caddy with the command:

sudo apt-get install caddy -y

Start and enable the Caddy service with:

sudo systemctl enable --now caddy

You can now point your browser to http:// SERVER (Where SERVER is your host’s IP address or domain) and see the default Caddy welcome page (Picture A).

Picture A

install-caddy
Image: Jack Wallen / TechRepublic. The Caddy welcome page has been powered by Ubuntu Server 22.04.

How to Create Your First Caddy Website

Also, the Caddy document root directory is /usr/share/caddy but we want to change it to a more standard directory. Before we configure Caddy, let’s create a new document root with:

sudo mkdir -p /var/www/html

Next, let’s create a basic static website file with:

sudo nano /var/ww/html/index.hml

In that file, paste the following contents:

<!DOCTYPE html>

<html>

<head>

<title>Hello, TechRepublic!</title>

</head>

<body>

<h1 style="font-family: sans-serif">Hello, TechRepublic, from the Caddy web server!</h1>

</body>

</html>

Save and close the file.

Open the Caddy config file with:

sudo nano /etc/caddy/Caddyfile

Near the top of that file, you’ll find the following:

:80 {

# Set this path to your site's directory.

root * /usr/share/caddy

Change that to:

:80 {

# Set this path to your site's directory.

root * /var/www/html/

Save and close the file.

Reload the Caddy config with:

sudo systemctl reload caddy

Point your web browser back to http:// SERVER (where SERVER is the IP address or domain of the host) and you will see our new welcome message (Figure BUT)

Figure BUT

install-caddyb
Image: Jack Wallen / TechRepublic. Our new Caddy welcome page says: “Hello” to TechRepublic.

Caddy has another cool trick for static sites. Let’s create another page that will print the message Hello, TechRepublic and then, using curl, upload it to the Caddy server.

Create a new file with:

nano caddy.json

In that file, paste the following:

{

"apps": {

"http": {

"servers": {

"example": {

"listen": [":2015"],

"roads": [

{

"handle": [{

"handler": "static_response",

"body": "Hello, TechRepublic!"

}]

}

]

}

}

}

}

}

Save and close the file.

Upload our caddy.json file to the Caddy server with the command:

curl localhost:2019/load -X POST -H "Content-Type: application/json" -d @caddy.json

The upload will take place immediately. Once done, point your browser to http://SERVER:2015 (Where SERVER is your host’s IP address or domain) and you’ll see Welcome, TechRepublic! message is printed (SIZE).

SIZE

install-caddyc
Image: Jack Wallen / TechRepublic. Our new static site has been pushed to the server via curl.

Congratulations, you now have the lightning-fast, lightweight Caddy web server up and running. We’ll come back to this later to learn more ways to leverage this platform.

Subscribe to TechRepublic’s How to make technology work on YouTube for all the latest tech advice for business professionals from Jack Wallen.



Source link

news7g

News7g: Update the world's latest breaking news online of the day, breaking news, politics, society today, international mainstream news .Updated news 24/7: Entertainment, Sports...at the World everyday world. Hot news, images, video clips that are updated quickly and reliably

Related Articles

Back to top button