Updated News Around the World

How to deploy a single web page to the Caddy server from a Caddyfile

Computer technology isometric icon, server room, digital device set, element for design, pc laptop, mobile phone with smartwatch, cloud storage, flat vector
Image: fullvector/Adobe Stock

Caddy is a brilliant, albeit slightly niche, web server that is as at home serving up web applications as it is static pages. One of the very nifty things you can do with Caddy is generate static sites from what’s called a Caddyfile. With the help of curl, you can even automate that process to build a unique and efficient system for posting information or even single-page applications.

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

SEE: Hiring kit: Python developer (TechRepublic Premium)

What you’ll need

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

How to create a Caddyfile

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

Let’s start with the tried and true “Hello, world!” application. Log into your Caddy server and create the file with the command:

nano Caddyfile

In that file, we’ll add two lines:

:2015

respond: "Hello, TechRepublic!"

That’s all there is to it. Save and close the file.

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

caddy adapt

The above command must be run in the same directory that holds 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’ll deploy the page with the command:

caddy run

If you receive an error that port 2019 is already in use, the fix 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 debug tip: If you receive a warning that there’s a problem with your Caddyfile formatting, you can always run the following command to resolve the problems:

caddy fmt

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

Let’s do something a bit more useful than creating a “Hello, world!” page. This time around, we’ll create a file browser from the directory housing the Caddyfile. This can be helpful if you need to give on-the-fly access to a directory from within 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, run the Caddy server with the new Caddyfile using the command:

caddy run

As you can see (Figure A), we’re serving up the current working directory from where 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 file server up and running, let’s add a basic HTML file with a twist. This new file will print out the IP address of the hosting server. We create this HTML file in the same directory housing the Caddyfile. Before we do that, let’s stop the Caddy server with the CTRL + C keyboard combination and then open the Caddyfile with:

nano Caddyfile

We’re going to add the templates directive so we can make use of the powerful Caddy templating 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 that file, paste the following:

<!DOCTYPE html>
<html>
<head>
<title>TechRepublic Caddy How to</title>
</head>
<body>
What is my IP Address: {{.RemoteIP}
</body>
</html>

The important line is:

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

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

Run Caddy again with:

caddy run

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

What is my IP Address: 192.168.1.62

There are plenty of other templates you can make use of. Take a look at the Caddy Templates documentation for more information.

Congratulations, you’ve just deployed a single web page using a Caddyfile. Next time around we’ll see about automating the Caddyfile deployment.

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

For all the latest Technology News Click Here 

 For the latest news and updates, follow us on Google News

Read original article here

Denial of responsibility! NewsUpdate is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – [email protected]. The content will be deleted within 24 hours.