How to install the Caddy site, service, and app server on Ubuntu

May 26, 2020 by No Comments

Caddy is a new way to serve up apps and services. Learn how to install Caddy and serve up a basic website.

Developer young woman

Image: Getty Images/iStockphoto

In today’s world, there are so many ways to serve up sites, apps, and services. You could go the standard route or the container route. You could also go the virtual machine route to then head back down either one of those paths or mix and match.

What if I told you there was yet another way to serve up those sites, services, and apps? That’s right network admins, you have yet another platform to choose from. Said platform is Caddy.

Caddy is an extensible platform, written in Go, that allows you to serve up your sites, services, and apps. In fact, Caddy can do quite a bit. You can use it as a:

  • Web server

  • Standard proxy

  • Reverse proxy

  • Sidecar proxy

  • Load balancer

  • API gateway

  • Ingress controller

  • System manager

  • Process supervisor

  • Task scheduler

I want to walk you through the process of installing Caddy on Ubuntu Server 18.04, and then demonstrate how to serve up a basic website with it.

What you’ll need

SEE: Windows 10 Start menu hacks (TechRepublic Premium)

How to install Caddy

Caddy cannot be installed from the standard repositories so we must then add the necessary repo to Ubuntu. Open a terminal window and issue the command:

echo "deb [trusted=yes] https://apt.fury.io/caddy/ /"     | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list

Once the repository is added, update apt with the command:

sudo apt-get update

Finally, install Caddy with the command:

sudo apt-get install caddy -y

When the installation completes, you can verify it by issuing the command:

caddy version

The above command should print out the complete version string for Caddy (Figure A).

Figure A

caddy-version.jpg

The installation of Caddy was a success.

How to deploy a simple website with Caddy

Now that Caddy is installed, let’s create a simple website. Issue the command:

curl localhost:2019/config/

You should see the text:

Moved Permanently.

We’re going to create a very simple page and upload it to the Caddy server. Caddy configurations are all written in JSON. So create a new file with the command:

nano caddy.json

In that new file, paste the following:

{
        "apps": {
                "http": {
                        "servers": {
                                "example": {
                                        "listen": [":2015"],
                                        "routes": [
                                                {
                                                        "handle": [{
                                                                "handler": "static_response",
                                                                "body": "Hello, TechRepublic!"
                                                        }]
                                                }
                                        ]
                                }
                        }
                }
        }
}

Save and close the file. Start Caddy with the command:

caddy run

You won’t get your prompt back, so log in to the server with another instance and upload the file with the command:

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

Issue the curl command:

curl localhost:2019

You should see the printout “Hello, TechRepublic!” (Figure B).

Figure B

caddy-curl.jpg

Our Caddy configuration file, as seen through the eyes of curl.

You can also point a browser to http://SERVER_IP:2015 (where SERVER_IP is the IP address of the server hosting Caddy) and you should see the message printed out as well (Figure C).

Figure C

caddy-web.jpg

Caddy welcomes TechRepublic from within Firefox.

Of course, you don’t want to have to write a JSON file, to print out “Hello, TechRepublic.” Fortunately, the developers have included the ability to use a Caddyfile for deployment. Instead of writing out the brace intensive JSON, you could create a new file with the command:

nano Caddyfile

In that file, paste the following:

:2015

respond "Hello, TechRepublic!"

Save and close the file. 

Stop Caddy with the Ctrl+C key combination and then load the new configuration file and restart Caddy with the command:

caddy adapt

The above command must be run from within the same directory that houses your Caddyfile. If not, you could issue the command as:

caddy adapt --config /path/to/Caddyfile

Where /path/to/Caddyfile is the complete path to the Caddyfile.

Point your browser to http://SERVER_IP:2015 (where SERVER_IP is the IP address of the hosting server) and you should see the “Hello, TechRepublic!” message again.

How to serve up a static website with Caddy

At this point, you’ve served up a single file. What if you have a static website you want to serve? House all of the necessary files in a single directory (say ~/website), which includes an index.html file, and issue the command:

caddy file-server --browse --root ~/website --listen :2015

With that command running, point your browser to http://SERVER_IP:2015 (where SERVER_IP is the IP address of the server hosting Caddy) and you should see the index.html page in the browser (Figure D).

Figure D

caddy-site.jpg

A static (yet still basic) site served up with Caddy.

And that’s the gist of Caddy. Of course, all we’ve done is serve up a very basic website. There’s much more that Caddy can do. Next time we revisit Caddy, we’ll dig a bit deeper and even serve up multiple sites from a single Caddyfile.

Also see

Leave a Comment

Your email address will not be published. Required fields are marked *