There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

How to Host a Website with Caddy on Ubuntu 18.04

Caddy also known as a Caddy web server is an open-source web server written in Go language. By default, Caddy downloads and installs TLS certificates for your sites automatically. You can use Caddy to serve your static site with compression, template evaluation, Markdown rendering, and more. It is a modern, enterprise-ready and multi-platform web server that supports Virtual hosting, HTTP/2, WebSockets, FastCGI, Markdown and more. It comes with a lot of plugins that help you to extend the functionality of Caddy.

In this tutorial, we will explain how to host a website with a Caddy web server on Ubuntu 18.04.

Prerequisites

  • A server running Ubuntu 18.04.
  • A valid domain name pointed with your server IP. In this tutorial, we will use caddy.linuxbuz.com.
  • A root password is configured on your server.

Getting Started

Before starting, it is a good idea to update your systems package to the latest version. You can update all the packages with the following command:

apt-get update -y
apt-get upgrade -y

Once all the packages are updated, restart your system to apply the changes.

Install Caddy

By default, Caddy is not available in the Ubuntu 18.04 default repository. So you can install it by downloading the Caddy installation script provided by Caddy.

First, install the curl package with the following command:

apt-get install curl -y

Once installed, run the following command to install the Caddy web server in your system.

curl https://getcaddy.com | bash -s personal hook.service

Once the installation has been completed successfully, you should get the following output:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7380  100  7380    0     0   266k      0 --:--:-- --:--:-- --:--:--  277k
Downloading Caddy for linux/amd64 (personal license)...
Download verification OK
Extracting...
Putting caddy in /usr/local/bin (may require password)
v1.0.4
Successfully installed

Where:

The option -s personal is used to specify a personal license and hook.service is a plugin that allows you to access the systemd unit file that you can use to manage Caddy as a systemd service.

Next, you can see the installed version of Caddy with the following command:

caddy -version

You should see the following output:

v1.0.4

Next, install Caddy as a systemd service using the following command:

caddy -service install

Next, start the Caddy service and enable it to start at boot with the following command:

systemctl start caddy
systemctl enable caddy

Next, verify the status of Caddy service with the following command:

systemctl status caddy

You should get the following output:

? caddy.service - Caddy's service
   Loaded: loaded (/etc/systemd/system/caddy.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-04-20 05:49:04 UTC; 7s ago
 Main PID: 1722 (caddy)
    Tasks: 10 (limit: 2359)
   CGroup: /system.slice/caddy.service
           ??1722 /usr/local/bin/caddy

Apr 20 05:49:04 ubuntu1804 systemd[1]: Started Caddy's service.
Apr 20 05:49:04 ubuntu1804 caddy[1722]: Activating privacy features... done.
Apr 20 05:49:04 ubuntu1804 caddy[1722]: Serving HTTP on port 2015
Apr 20 05:49:04 ubuntu1804 caddy[1722]: http://:2015

Create a Website Directory

Next, you will need to create a directory structure and sample index file for your website.

First, create a document root directory with the following command:

mkdir -p /var/www/caddy.linuxbuz.com

Next, create an index.html file with the following command:

nano /var/www/caddy.linuxbuz.com/index.html

Add the following contents:

<!doctype html>
<head><title>Caddy Sample Page</title></head>
<body>
<h1>Welcome to Caddy web server</h1>
</body>
</html>

Save and close the file when you are finished. Then, provide proper permission to the web root directory:

chown -R www-data:www-data /var/www/caddy.linuxbuz.com

Configure Caddy Webserver

Next, you will need to create a virtual host configuration file to serve your website files to the internet.

First, create a directory to store Caddy’s configuration files:

mkdir -p /etc/caddy

Next, change the ownership of the caddy to the www-data with the following command:

chown -R www-data:www-data /etc/caddy

Next, create a Caddy's main configuration file using the following command:

nano /etc/caddy/Caddyfile

Add the following lines:

caddy.linuxbuz.com {
    root /var/www/caddy.linuxbuz.com
}

Save and close the file when you are finished. Then, run the following command to serve your site over HTTPS using Let’s Encrypt:

caddy -agree -conf /etc/caddy/Caddyfile -email [email protected]

You should get the following output:

Activating privacy features... 2020/04/20 05:57:36 [INFO] acme: Registering account for [email protected]
2020/04/20 05:57:37 [INFO] [caddy.linuxbuz.com] acme: Obtaining bundled SAN certificate
2020/04/20 05:57:37 [INFO] [caddy.linuxbuz.com] AuthURL: https://acme-v02.api.letsencrypt.org/acme/authz-v3/4067652622
2020/04/20 05:57:37 [INFO] [caddy.linuxbuz.com] acme: Could not find solver for: tls-alpn-01
2020/04/20 05:57:37 [INFO] [caddy.linuxbuz.com] acme: use http-01 solver
2020/04/20 05:57:37 [INFO] [caddy.linuxbuz.com] acme: Trying to solve HTTP-01
2020/04/20 05:57:37 [INFO] [caddy.linuxbuz.com] Served key authentication
2020/04/20 05:57:37 [INFO] [caddy.linuxbuz.com] Served key authentication
2020/04/20 05:57:37 [INFO] [caddy.linuxbuz.com] Served key authentication
2020/04/20 05:57:37 [INFO] [caddy.linuxbuz.com] Served key authentication
2020/04/20 05:57:44 [INFO] [caddy.linuxbuz.com] The server validated our request
2020/04/20 05:57:44 [INFO] [caddy.linuxbuz.com] acme: Validations succeeded; requesting certificates
2020/04/20 05:57:45 [INFO] [caddy.linuxbuz.com] Server responded with a certificate.
done.

Serving HTTPS on port 443 
https://caddy.linuxbuz.com


Serving HTTP on port 80 
http://caddy.linuxbuz.com

At this point, Caddy is configured and secured with Let's Encrypt. You can now proceed to access the Caddy web interface.

Access Caddy Web Interface

Now, open your web browser and type the URL https://caddy.linuxbuz.com. You will be redirected to the Caddy page in the following screen:

Caddy web server

Conclusion

In this guide, we learned how to host a website with a Caddy web server. We also learned how to secure it with Let's Encrypt SSL. I hope you can now easily use Caddy as an alternate web server.

Share this page:

Suggested articles

0 Comment(s)

Add comment