Khám phá cách Host Website bằng Caddy trên Ubuntu 22.04

Cách Host một trang web bằng Caddy trên Ubuntu 22.04


Caddy là một máy chủ web được thiết kế với sự đơn giản và an toàn, đi kèm với một số tính năng hữu ích cho việc lưu trữ website. Ví dụ, nó có thể tự động lấy và quản lý chứng chỉ TLS từ Let’s Encrypt để kích hoạt HTTPS, và bao gồm hỗ trợ cho HTTP/2. HTTPS là hệ thống bảo mật dữ liệu giữa người dùng của bạn và máy chủ, và nhanh chóng trở thành một điều kiện cần thiết cho bất kỳ website chạy trong môi trường thực — nếu không có, Chrome và Firefox sẽ cảnh báo rằng website của bạn “Không An Toàn” nếu người dùng cố gắng gửi thông tin đăng nhập. Trong hướng dẫn này, bạn sẽ xây dựng Caddy từ mã nguồn sử dụng xcaddy, một công cụ xây dựng Caddy tùy chỉnh, và sử dụng nó để lưu trữ một website được bảo mật bằng HTTPS. Điều này bao gồm việc biên dịch nó, cấu hình nó bằng Caddyfile và cài đặt một plugin. Cuối cùng, tên miền của bạn sẽ phục vụ các trang tĩnh, trong khi được bảo mật bằng chứng chỉ TLS miễn phí từ Let’s Encrypt.

Yêu cầu

Một máy chủ Ubuntu 22.04 với quyền root, có ít nhất 2 GB RAM và một tài khoản phụ, không phải là root. Bạn có thể thiết lập điều này bằng cách làm theo Hướng dẫn Thiết lập Ban Đầu cho Ubuntu 22.04 của chúng tôi. Trong hướng dẫn này, tài khoản không phải root là sammy.

Bộ công cụ ngôn ngữ Go đã được cài đặt trên máy chủ của bạn.

Một tên miền đã đăng ký đầy đủ. Hướng dẫn này sẽ sử dụng your_domain. Bạn có thể mua tên miền trên Namecheap, nhận miễn phí trên Freenom hoặc sử dụng trang web đăng ký tên miền mà bạn chọn.

Một mã token truy cập cá nhân (API key) có quyền đọc và ghi cho tài khoản DigitalOcean của bạn.

Bước 1 — Xây dựng Caddy

Ở bước này, bạn sẽ xây dựng Caddy từ mã nguồn với khả năng sau này thêm các plugin, mà không cần thay đổi mã nguồn Caddy. Bạn sẽ sử dụng xcaddy để tải xuống và xây dựng Caddy và các plugin của nó theo nhu cầu của bạn. Truy cập trang phát hành xcaddy và sao chép liên kết của bản phát hành mới nhất cho nền tảng linux_amd64. Trước khi tải xuống, điều hướng đến /tmp bằng cách chạy lệnh sau:

Sau đó, tải xuống bản phát hành mới nhất bằng wget:

wget https://github.com/caddyserver/xcaddy/releases/download/v0.4.0/xcaddy_0.4.0_linux_amd64.tar.gz

Sau khi tải xuống, giải nén chỉ nhị phân:

tar xvf xcaddy_0.4.0_linux_amd64.tar.gz xcaddy

Cuối cùng, di chuyển tập lênh xcaddy sang /usr/bin, làm cho nó có thể truy cập trên toàn hệ thống:

Giờ đây xcaddy đã được cài đặt, bạn sẽ xây dựng Caddy. Để mục đích đó, tạo một thư mục riêng để lưu trữ:

Điều hướng đến thư mục đó bằng cách chạy lệnh sau:

Để xây dựng phiên bản mới nhất của Caddy mà không có bất kỳ plugin của bên thứ ba nào, hãy chạy lệnh sau:

Lệnh này sẽ mất một thời gian để hoàn thành, nhưng nó sẽ in ra một output tương tự như sau:

Khi hoàn thành, bạn sẽ có tệp thực thi caddy có sẵn trong thư mục hiện tại. Di chuyển nó đến /usr/bin để cài đặt:

Bạn có thể thử chạy caddy để kiểm tra xem nó đã được cài đặt đúng cách hay không:

Output sẽ chứa phiên bản Caddy bạn vừa biên dịch:

bạn đã xây dựng và chạy Caddy. Ở bước tiếp theo, bạn sẽ cài đặt Caddy dưới dạng một dịch vụ để nó tự động khởi động khi khởi động hệ thống, và sau đó bạn sẽ điều chỉnh cài đặt quyền sở hữu và quyền truy cập để đảm bảo an ninh của máy chủ.

Bước 2 — Cài đặt Caddy

Sau khi đã xác nhận bạn có thể xây dựng và chạy Caddy, bạn có thể cấu hình một dịch vụ systemd để Caddy sẽ tự động khởi chạy khi hệ thống khởi động. Caddy yêu cầu có người dùng và nhóm riêng để chạy như một dịch vụ systemd. Tạo nhóm bằng lệnh sau: sudo groupadd –system caddy

Sau đó, tạo một người dùng mới gọi là caddy thuộc nhóm caddy: sudo useradd –system \ –gid caddy \ –create-home \ –home-dir /var/lib/caddy \ –shell /usr/sbin/nologin \ –comment “Web server Caddy” \ caddy Người dùng cần caddy sẽ có thư mục nhà riêng. Vì shell của nó được thiết lập là nologin, không thể đăng nhập dưới tên caddy. Thay đổi sở hữu của tệp nhị phân Caddy sang người dùng root: sudo chown root:root /usr/bin/caddy Sự thay đổi này sẽ ngăn người dùng khác thay đổi tập lệnh được thực thi. Tuy nhiên, trong khi người dùng root sẽ sở hữu Caddy, bạn nên thực thi nó bằng các tài khoản không phải root khác có trên hệ thống, như trường hợp với dịch vụ systemd. Chạy các lệnh từ một tài khoản không phải root đảm bảo rằng kẻ tấn công sẽ không thể thay đổi tệp nhị phân hoặc thực thi lệnh dưới dạng root trong trường hợp Caddy (hoặc chương trình khác) bị xâm nhập. Tiếp theo, thiết lập quyền truy cập của tập lệnh nhị phân thành 755, làm cho root có quyền đọc/viết/thực thi đầy đủ cho tệp, trong khi người dùng khác chỉ có thể đọc và thực thi: sudo chmod 755 /usr/bin/caddy Bạn đã hoàn tất thiết lập tệp nhị phân Caddy và sẵn sàng bắt đầu cấu hình Caddy. Tạo một thư mục để lưu trữ các tệp cấu hình của Caddy: sudo mkdir /etc/caddy Sau đó thiết lập quyền của người dùng và nhóm cho nó: sudo chown -R root:caddy /etc/caddy Đặt người dùng là root và nhóm là caddy đảm bảo rằng Caddy sẽ có quyền đọc và ghi vào thư mục (thông qua nhóm caddy) và chỉ có tài khoản quản trị hệ thống mới có quyền đọc và sửa đổi.

Trong bước tiếp theo, bạn sẽ kích hoạt cấp chứng chỉ TLS tự động từ Let’s Encrypt. Chuẩn bị cho điều đó, tạo một thư mục để lưu trữ bất kỳ chứng chỉ TLS nào mà Caddy sẽ nhận được và cung cấp cùng quy tắc sở hữu như thư mục /etc/caddy: sudo mkdir /etc/ssl/caddy sudo chown -R root:caddy /etc/ssl/caddy Caddy phải có thể ghi chứng chỉ vào thư mục này và đọc chúng để mã hóa yêu cầu. Vì lý do này, hãy sửa đổi quyền cho thư mục /etc/ssl/caddy để nó chỉ có thể truy cập bởi root và caddy: sudo chmod 0770 /etc/ssl/caddy Tiếp theo tạo một thư mục để lưu trữ các tệp mà Caddy sẽ lưu trữ: sau đó đặt chủ sở hữu và nhóm của thư mục thành caddy: sudo chown caddy:caddy /var/www Để cài đặt dịch vụ Caddy, tải xuống tệp đơn vị systemd từ kho lưu trữ GitHub Caddy đến /etc/systemd/system bằng lệnh: sudo sh -c ‘curl https://raw.githubusercontent.com/caddyserver/dist/master/init/caddy.service > /etc/systemd/system/caddy.service’

Bạn sẽ nhận được một câu trả lời như sau: % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1030 100 1030 0 0 4698 0 –:–:– –:–:– –:–:– 4703

Chỉnh sửa quyền của tệp dịch vụ để nó chỉ có thể được sửa đổi bởi chủ sở hữu, root: sudo chmod 644 /etc/systemd/system/caddy.service

Sau đó, khởi động lại systemd để phát hiện dịch vụ Caddy: sudo systemctl daemon-reload

Kiểm tra xem systemd đã phát hiện dịch vụ Caddy bằng cách chạy systemctl status: sudo systemctl status caddy

Bạn sẽ nhận được một kết quả tương tự:

● caddy.service – Caddy Loaded: loaded (/etc/systemd/system/caddy.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: https://caddyserver.com/docs/

Nếu bạn nhận cùng một kết quả, thì dịch vụ mới đã được phát hiện bởi systemd. Như một phần của việc thiết lập ban đầu máy chủ, bạn đã kích hoạt ufw và cho phép kết nối SSH. Để Caddy phục vụ lưu lượng HTTP và HTTPS từ máy chủ của bạn, bạn cần phải cho phép chúng trong ufw bằng cách chạy lệnh sau: sudo ufw allow proto tcp from any to any port 80,443

Kết quả sẽ là:

Rule added Rule added (v6) Sử dụng ufw status để xác minh các thay đổi của bạn:

Bạn sẽ nhận được kết quả sau:

Status: active To Action From — —— —- OpenSSH ALLOW Anywhere 80,443/tcp ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) 80,443/tcp (v6) ALLOW Anywhere (v6)

Cài đặt Caddy của bạn đã hoàn tất, tuy nhiên nó chưa được cấu hình để phục vụ bất cứ điều gì. Trong bước tiếp theo, bạn sẽ cấu hình Caddy để phục vụ các tệp từ thư mục /var/www.

Bước 3 — Cấu hình Caddy

Trong phần này, bạn sẽ viết cấu hình cơ bản của Caddy để phục vụ các tệp tĩnh từ máy chủ của bạn. Tạo một tệp HTML cơ bản gọi là index.html trong /var/www: sudo nano /var/www/index.html

Thêm các dòng sau:

Hello from Caddy!

This page is being served via Caddy

Khi hiển thị trên trình duyệt web, tệp này sẽ hiển thị một tiêu đề với văn bản This page is being served via Caddy. Lưu và đóng tệp. Caddy đọc cấu hình từ một tệp gọi là Caddyfile, được lưu trữ dưới /etc/caddy. Tạo và mở tệp để chỉnh sửa: sudo nano /etc/caddy/Caddyfile

Thêm các dòng sau:

http:// { root * /var/www encode gzip file_server }

Cấu hình cơ bản của Caddy này khai báo rằng tất cả lưu lượng HTTP đến máy chủ của bạn sẽ được phục vụ bằng các tệp (file_server) từ /var/www (là. . .

Caddy is a web server designed around simplicity and security that comes with a number of features that are useful for hosting websites. For example, it can automatically obtain and manage TLS certificates from Let’s Encrypt to enable HTTPS, and includes support for HTTP/2. HTTPS is a system for securing traffic between your users and your server, and is quickly becoming a basic expectation of any website running in production — without it, Chrome and Firefox will warn that your website is “Not Secure” if users try to submit login information.

In this tutorial, you’ll build Caddy from source using xcaddy, a custom Caddy builder tool, and use it to host a website secured with HTTPS. This entails compiling it, configuring it using a Caddyfile and installing a plugin. In the end, your domain will serve static pages, while being secured with free TLS certificates from Let’s Encrypt.

Prerequisites

Step 1 — Building Caddy

In this step, you’ll build Caddy from source with the ability to later add plugins, all without changing Caddy’s source code. You’ll use xcaddy to download and build Caddy and its plugins according to your needs.

Visit the xcaddy releases page and copy the link of the latest release for the linux_amd64 platform. Before downloading it, navigate to /tmp by running the following command:

Then, download the latest release using wget:

wget https://github.com/caddyserver/xcaddy/releases/download/v0.4.0/xcaddy_0.4.0_linux_amd64.tar.gz

Once downloaded, extract only the binary:

tar xvf xcaddy_0.4.0_linux_amd64.tar.gz xcaddy

Finally, move the xcaddy executable to /usr/bin, making it accessible system wide:

Now that xcaddy is installed, you’ll build Caddy. For that purpose, create a separate directory for storing it:

Navigate to it by running the following command:

To build the latest version of Caddy without any third party plugins, run the following command:

This command will take some time to finish, but it will print an output similar to this:

Output
2022/08/10 15:55:18 (INFO) Temporary folder: /tmp/buildenv_2022-08-10-1555.834895411
2022/08/10 15:55:18 (INFO) Writing main module: /tmp/buildenv_2022-08-10-1555.834895411/main.go
package main

import (
        caddycmd "github.com/caddyserver/caddy/v2/cmd"

        // plug in Caddy modules here
        _ "github.com/caddyserver/caddy/v2/modules/standard"
)

func main() {
        caddycmd.Main()
}
2022/08/10 15:55:18 (INFO) Initializing Go module
2022/08/10 15:55:18 (INFO) exec (timeout=10s): /usr/local/go/bin/go mod init caddy
go: creating new go.mod: module caddy
go: to add module requirements and sums:
        go mod tidy
2022/08/10 15:55:18 (INFO) Pinning versions
2022/08/10 15:55:18 (INFO) exec (timeout=0s): /usr/local/go/bin/go get -d -v github.com/caddyserver/caddy/v2
go: downloading github.com/caddyserver/caddy v1.0.5
...
2022/08/10 15:55:49 (INFO) Build environment ready
2022/08/10 15:55:49 (INFO) Building Caddy
2022/08/10 15:55:49 (INFO) exec (timeout=0s): /usr/local/go/bin/go mod tidy
...
2022/08/10 15:55:57 (INFO) exec (timeout=0s): /usr/local/go/bin/go build -o /home/sammy/caddy/caddy -ldflags -w -s -trimpath
2022/08/10 15:58:48 (INFO) Build complete: ./caddy
2022/08/10 15:58:48 (INFO) Cleaning up temporary folder: /tmp/buildenv_2022-08-10-1555.834895411

Once finished, you’ll have the caddy executable available in the current folder. Move it to /usr/bin to install it:

You can try running caddy to check that it’s installed correctly:

The output will contain the version of Caddy you’ve just compiled:

Output
v2.6.1 h1:EDqo59TyYWhXQnfde93Mmv4FJfYe00dO60zMiEt+pzo=

You have now built and executed Caddy. In the next step, you’ll install Caddy as a service so that it starts automatically at boot, and then you’ll adjust ownership and permissions settings to ensure the server’s security.

Step 2 — Installing Caddy

Now that you’ve verified you’re able to build and run Caddy, you can configure a systemd service so that Caddy will be launched automatically on system startup.

Caddy requires its own user and group in order to run as a systemd service. Create the group with the following command:

sudo groupadd --system caddy

Then, create a new user called caddy that belongs to the caddy group:

sudo useradd --system \
    --gid caddy \
    --create-home \
    --home-dir /var/lib/caddy \
    --shell /usr/sbin/nologin \
    --comment "Caddy web server" \
    caddy

The new caddy user will have its own home directory created. Because its shell is set to nologin, it won’t be possible to log in as caddy.

Change ownership of the Caddy binary to the root user:

sudo chown root:root /usr/bin/caddy

This change will prevent other accounts from modifying the executable. However, while the root user will own Caddy, you are advised to execute it using other, non-root accounts present on the system, as will be the case with the systemd service. Running commands from a non-root account ensures that the attacker won’t be able to modify the binary or execute commands as root in the event that Caddy (or another program) is compromised.

Next, set the binary file’s permissions to 755, which will give root full read/write/execute permissions for the file, while other users will only be able to read and execute it:

sudo chmod 755 /usr/bin/caddy

You have now finished setting up the Caddy binary and are ready to start Caddy configuration.

Create a directory where you’ll store Caddy’s configuration files:

Then set the user and group permissions for it:

sudo chown -R root:caddy /etc/caddy

Setting the user as root and the group as caddy ensures that Caddy will have read and write access to the folder (via the caddy group) and that only the superuser account will have the same rights to read and modify.

In a later step, you’ll enable automatic TLS certificate provisioning from Let’s Encrypt. In preparation for that, make a directory to store any TLS certificates that Caddy will obtain and give it the same ownership rules as the /etc/caddy directory:

sudo mkdir /etc/ssl/caddy
sudo chown -R root:caddy /etc/ssl/caddy

Caddy must be able to write certificates to this directory and read from it in order to encrypt requests. For this reason, modify the permissions for the /etc/ssl/caddy directory so that it’s only accessible by root and caddy:

sudo chmod 0770 /etc/ssl/caddy

Next create a directory to store the files that Caddy will host:

Then set the directory’s owner and group to caddy:

sudo chown caddy:caddy /var/www

To install the Caddy service, download the systemd unit file from the Caddy GitHub repository to /etc/systemd/system by running:

sudo sh -c 'curl https://raw.githubusercontent.com/caddyserver/dist/master/init/caddy.service > /etc/systemd/system/caddy.service'

You’ll receive a response like this:

Output
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1030  100  1030    0     0   4698      0 --:--:-- --:--:-- --:--:--  4703
​```

Modify the service file's permissions so it can only be modified by its owner, `root`:

​```command
sudo chmod 644 /etc/systemd/system/caddy.service

Then, reload systemd to detect the Caddy service:

sudo systemctl daemon-reload

Check whether systemd has detected the Caddy service by running systemctl status:

sudo systemctl status caddy

You’ll receive an output similar to:

Output
● caddy.service - Caddy
     Loaded: loaded (/etc/systemd/system/caddy.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://caddyserver.com/docs/

If you have the same output, then the new service was detected by systemd.

As part of the initial server setup, you enabled ufw and allowed SSH connections. For Caddy to serve HTTP and HTTPS traffic from your server, you’ll need to allow them in ufw by running the following command:

sudo ufw allow proto tcp from any to any port 80,443

The output will be:

Output
Rule added
Rule added (v6)

Use ufw status to verify your changes:

You’ll receive the following output:

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
80,443/tcp                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
80,443/tcp (v6)            ALLOW       Anywhere (v6)

Your installation of Caddy is now complete, though it isn’t configured to serve anything. In the next step, you’ll configure Caddy to serve files from the /var/www directory.

Step 3 — Configuring Caddy

In this section, you’ll write basic Caddy configuration for serving static files from your server.

Create a basic HTML file called index.html in /var/www:

sudo nano /var/www/index.html

Add the following lines:

<!DOCTYPE html>
<html>
<head>
<title>Hello from Caddy!</title>
</head>
<body>
<h1 style="font-family: sans-serif">This page is being served via Caddy</h1>
</body>
</html>

When shown in a web browser, this file will display a heading with the text This page is being served via Caddy. Save and close the file.

Caddy reads its configuration from a file called Caddyfile, stored under /etc/caddy. Create and open the file for editing:

sudo nano /etc/caddy/Caddyfile

Add the following lines:

http:// {
    root * /var/www
    encode gzip
    file_server
}

This basic Caddy config declares that all HTTP traffic to your server should be served with files (file_server) from /var/www (which is marked as root) and compressed using gzip to reduce page loading times on the client side.

Caddy has different directives for many use cases. For example, the log directive could be useful for logging all HTTP requests that occur. You can review more options at the official documentation page for directives.

When you are done, save and close the file.

To test that everything is working correctly, start the Caddy service:

sudo systemctl start caddy

Next, run systemctl status to find information about the status of the Caddy service:

sudo systemctl status caddy

You’ll receive the following:

Output
● caddy.service - Caddy
     Loaded: loaded (/etc/systemd/system/caddy.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-08-10 15:02:41 UTC; 2s ago
       Docs: https://caddyserver.com/docs/
   Main PID: 5443 (caddy)
      Tasks: 7 (limit: 1119)
     Memory: 7.5M
        CPU: 30ms
     CGroup: /system.slice/caddy.service
             └─5443 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

You can now visit your server’s IP in a web browser. Your sample web page will display:

This page is being served via Caddy

You have now configured Caddy to serve static files from your server. In the next step, you’ll extend Caddy’s functionality through plugins.

Step 4 — Enabling Automatic TLS with Let’s Encrypt

Plugins can change and extend Caddy’s behavior. Generally, they offer more config directives, according to your use case. In this step, you’ll enable automatic Let’s Encrypt certificate provisioning and renewal, using TXT DNS records for verification. To verify using TXT DNS records, you’ll install the official plugin to interface with the DigitalOcean DNS API.

To add a plugin, you need to recompile Caddy using xcaddy, specifying the repositories of plugins that should be available. Run the following command to compile Caddy with support for DigitalOcean DNS:

xcaddy build --with github.com/caddy-dns/digitalocean@master

The output will be similar to this:

Output
...
2022/08/10 15:03:24 (INFO) exec (timeout=0s): /usr/local/go/bin/go get -d -v github.com/caddy-dns/digitalocean@master github.com/caddyserver/caddy/v2
go: downloading github.com/caddy-dns/digitalocean v0.0.0-20220527005842-9c71e343246b
go: downloading github.com/libdns/digitalocean v0.0.0-20220518195853-a541bc8aa80f
go: downloading github.com/digitalocean/godo v1.41.0
go: downloading github.com/google/go-querystring v1.0.0
go: downloading golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
go: downloading google.golang.org/appengine v1.6.6
go: added github.com/caddy-dns/digitalocean v0.0.0-20220527005842-9c71e343246b
go: added github.com/digitalocean/godo v1.41.0
go: added github.com/libdns/digitalocean v0.0.0-20220518195853-a541bc8aa80f
2022/08/10 15:03:33 (INFO) exec (timeout=0s): /usr/local/go/bin/go get -d -v
go: downloading github.com/antlr/antlr4 v0.0.0-20200503195918-621b933c7a7f
go: downloading github.com/Masterminds/semver v1.4.2
go: downloading github.com/cenkalti/backoff v2.2.1+incompatible
go: downloading github.com/cpuguy83/go-md2man v1.0.10
go: downloading github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220804214150-8b0cc382067f
go: downloading github.com/antlr/antlr4 v4.10.1+incompatible
go: upgraded github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220418222510-f25a4f6275ed => v0.0.0-20220804214150-8b0cc382067f
go: upgraded golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c => v0.0.0-20211104180415-d3ed0bb246c8
go: upgraded google.golang.org/appengine v1.6.6 => v1.6.7
2022/08/10 15:03:39 (INFO) Build environment ready
2022/08/10 15:03:39 (INFO) Building Caddy
2022/08/10 15:03:39 (INFO) exec (timeout=0s): /usr/local/go/bin/go mod tidy
2022/08/10 15:03:40 (INFO) exec (timeout=0s): /usr/local/go/bin/go build -o /home/sammy/caddy/caddy -ldflags -w -s -trimpath
2022/08/10 15:03:56 (INFO) Build complete: ./caddy
2022/08/10 15:03:56 (INFO) Cleaning up temporary folder: /tmp/buildenv_2022-08-10-1503.1377463227

Once the compilation is finished, move the resulting binary to /usr/bin by running:

Then, set appropriate permissions:

sudo chown root:root /usr/bin/caddy
sudo chmod 755 /usr/bin/caddy

Next, you’ll configure Caddy to work with DigitalOcean’s API to set DNS records. Caddy needs to read your API token as an environment variable to configure DigitalOcean’s DNS, so you’ll edit its systemd unit file. Open the file for editing:

sudo nano /etc/systemd/system/caddy.service

Add the highlighted line in the (Service) section, replacing your_token_here with your API token:

...
(Service)
Type=notify
User=caddy
Group=caddy
Environment=DO_AUTH_TOKEN=your_token_here
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE
...

Save and close this file, then reload the systemd daemon to ensure the configuration is updated:

sudo systemctl daemon-reload

Run systemctl restart to check your configuration changes:

sudo systemctl restart caddy

Then run systemctl status to check if it ran correctly:

sudo systemctl status caddy

You will receive the following output:

Output
● caddy.service - Caddy
     Loaded: loaded (/etc/systemd/system/caddy.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-08-10 15:06:01 UTC; 2s ago
       Docs: https://caddyserver.com/docs/
   Main PID: 5620 (caddy)
      Tasks: 7 (limit: 1119)
     Memory: 7.5M
        CPU: 37ms
     CGroup: /system.slice/caddy.service
             └─5620 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

Next, you’ll need to make slight changes to your Caddyfile, so open it for editing:

sudo nano /etc/caddy/Caddyfile

Add the highlighted lines to the Caddyfile, replacing your_domain with your domain (instead of http://) and adding the tls block with the DigitalOcean DNS specified:

your_domain {
    root * /var/www
    encode gzip
    file_server
    
    tls {
        dns digitalocean {env.DO_AUTH_TOKEN}
    }
}

Using a domain rather than a protocol specifier for the hostname will cause Caddy to serve requests over HTTPS. The tls directive configures Caddy’s behavior when using TLS and you order it to use the digitalocean plugin for setting DNS records and requesting the certificates. The dns subdirective specifies that Caddy should use the DigitalOcean DNS system instead of HTTP.

Save and close the file.

With this, your website is ready to be deployed. Restart Caddy with systemctl and then enable it so it runs on boot:

sudo systemctl restart caddy
sudo systemctl enable caddy

When you visit your domain, you’ll be redirected to HTTPS automatically with the same This page is being served via Caddy message.

Your installation of Caddy is now complete and secured, and you can further customize according to your use case.

Conclusion

You now have Caddy installed and configured on your server, serving static pages at your desired domain and secured with free Let’s Encrypt TLS certificates.

A good next step would be to set up notifications for when new versions of Caddy are released. For example, you could use the Atom feed for Caddy releases or a dedicated service such as dependencies.io. You can explore Caddy’s documentation for more information on configuring Caddy.

KẾT LUẬN Để tóm gọn, Caddy là một máy chủ web được thiết kế dựa trên sự đơn giản và bảo mật, đi kèm với nhiều tính năng hữu ích cho việc lưu trữ các trang web. Trong hướng dẫn này, bạn sẽ xây dựng Caddy từ mã nguồn sử dụng xcaddy, một công cụ xây dựng Caddy tùy chỉnh, và sử dụng nó để lưu trữ trang web được bảo vệ bằng HTTPS. Sau đó, bạn cần cài đặt Caddy như một dịch vụ để nó tự động khởi động khi hệ thống khởi động. Cuối cùng, bạn sẽ cấu hình Caddy để phục vụ các tệp từ thư mục /var/www.

Leave a Reply