How to deploy a single webpage to Caddy server from a Caddyfile

Image: fullvector/Adobe Stock

Caddy is a brilliant, if slightly specialized, web server that’s just as comfortable serving web applications as it is static pages. One of the really nifty things you can do with Caddy is to generate static sites from something called a Caddyfile. With the help of curl, you can even automate this process to create a unique and efficient system for publishing information or even single-page applications.

I’ve already walked you through the Caddy installation process, and now I’m going to show you how to deploy a single webpage using a Caddyfile. It’s easy and the possibilities are limitless.

SEE: Recruitment Kit: Python Developer (TechRepublic Premium)

What you will need

In order to deploy a page on Caddy, you will need the web server to be up and running. And that’s all. Let’s deploy our first page.

How to create a Caddyfile

The first thing we need to do is create our Caddyfile. Although you can use JSON to create pages to deploy to Caddy, the easiest method is to create a Caddyfile. Caddyfiles use a very simple format and are simple to create.

Let’s start with the famous “Hello, world!” application. Connect to your Caddy server and create the file with the command:

nano Caddyfile

In this file, we will add two lines:

:2015

respond: "Hello, TechRepublic!"

That’s all we can say about it. Save and close the file.

We will now use the caddy command to convert the file to JSON with:

caddy adapt

The above command should be run in the same directory that contains the Caddyfile. If you store your Caddyfiles in a different location, the command would be:

caddy adapt --config /PATH/Caddyfile

Where PATH is the exact path to the Caddyfile.

Next, we will deploy the page with the command:

caddy run

If you receive an error that port 2019 is already in use, the solution is simple. Open the Caddyfile for editing and add the following to the top of the page:

{
admin 0.0.0.0:2020
}

Open a web browser and point it to http://SERVER:2015 (Where SERVER is the IP address of your Caddy server). You should see the following printed in the web page:

Hello, TechRepublic!

Another debugging tip: if you receive a warning that there is a problem with the formatting of your Caddyfile, you can always run the following command to fix the problems:

caddy fmt

This will print corrected formatting, so you can copy and paste it into your Caddyfile.

Let’s do something a little more useful than creating a “Hello, world!” page. This time we will create a file browser from the directory containing the Caddyfile. This can be useful if you need to give on-the-fly access to a directory from your server.

Create the new file with:

nano Caddyfile

If you still have content from the previous Caddyfile, delete it. In this new file, add the following:

{
admin 0.0.0.0:2020
}

:2015

file_server browse

Save and close the file. Adapt the file with:

caddy adapt

Now start the Caddy server with the new Caddyfile using the command:

caddy run

As you can see (Figure A), we serve the current working directory from which the Caddyfile was used.

Figure A

Image: Jack Wallen/TechRepublic.  A handy file server deployed by Caddy.
Image: Jack Wallen/TechRepublic. A handy file server deployed by Caddy.

Let’s make this even more useful. Now that we have a working file server, let’s add a basic HTML file with a twist. This new file will print the IP address of the hosting server. We create this HTML file in the same directory hosting the Caddyfile. Before doing this, let’s stop the Caddy server with the keyboard combination CTRL + C and then open the Caddyfile with:

nano Caddyfile

We are going to add the templates directive so that we can use the powerful Caddy template system. The file will then look like:

{
admin 0.0.0.0:2020
}

:2015

templates
file_server browse

Save and close the file. Create the html file with:

nano caddy.html

In this file, paste the following:




TechRepublic Caddy How to


What is my IP Address: {{.RemoteIP}

The important line is:

What is my IP Address: {{.RemoteIP}}

The {{.RemoteIP}} prints the IP address of the hosting server to Caddy. Save and close the file.

Run Caddy again with:

caddy run

In the web browser, click on caddy.html and you should see a page that prints something like:

What is my IP Address: 192.168.1.62

There are many other templates you can use. Take a look at the Caddy models documentation for more information.

Congratulations, you have just deployed a single webpage using a Caddyfile. Next time we will see how to automate Caddyfile deployment.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech tips for professionals from Jack Wallen.

Comments are closed.