I wanted to write this article for a long time, but I never had the opportunity. I took this opportunity to write by the way. The system image of this article is Debian 11.1. Please do not refer to this tutorial for other system environments (PS: especially for newbies~)

About Caddy

A simple, lightweight, and novice-friendly web server that can even automatically deploy SSL certificates for you, making it relatively friendly for novice users.

PS:But I don’t use it anymore. The current version of Caddy has some problems with HTTP/3 support, so I use Nginx-quic now, and I will write another tutorial someday~

Install Caddy

Configuring Software Sources

Install required dependencies:

1
apt install -y debian-keyring debian-archive-keyring apt-transport-https

Installation key and mirror source:

1
2
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list

Finally, update the software source and install Caddy:

1
2
apt update
apt install caddy

When you see the following prompt, you are done:
安装完成

Configure Caddy

Create website and certificate directory

Create a certificate directory

In fact, Caddy can automatically apply for a certificate, but if you want to use your own certificate, you need to create a folder to put the certificate~

1
2
cd /etc/ssl
mkdir caddy

Create a website directory

1
2
3
4
5
cd /
mkdir www
cd www
mkdir wwwroot
mkdir log

Among them, wwwroot is used to place the root directory of the website, and log is used to store website logs.

Grant permissions

1
2
sudo chown -R www-data:root /www
sudo chmod 0777 /www/log

Edit the Caddy configuration file

Caddy’s configuration file, Caddyfile, is in /etc/caddy. It can be downloaded and edited and then uploaded or edited directly with vim. Depending on personal preference, the left side of the Mobaxterm software is actually a file browser, where you can browse and edit files,Upload and download, the following is a sample file, please refer to official document for more usage methods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// The configuration here is used to support HTTP3, if you don't need to delete it
{
servers {
protocol {
experimental_http3
}
}
}
// A common website example, replace r2wind.com with your own domain name
r2wind.com {
// Configure the website root directory here, please upload the page file to the website root directory
root * /www/wwwroot/r2wind.com/public
// Configure the SSL certificate path. If you don't configure it, Caddy will automatically apply and match it for you.
tls /etc/ssl/caddy/r2wind.cn.crt /etc/ssl/caddy/r2wind.cn.key
// Customize the error page file, if you don't need to delete it
handle_errors {
rewrite * /{http.error.status_code}.html
file_server
}
// The log storage path, if you do not need to save the access log, you can delete it
log {
output file /www/log/r2wind_com.log
}
// Enable Gzip compression, delete it if you don't need it
encode gzip
file_server
// used to add response headers
header {
// Disables the client's MIME type sniffing behavior, remove it if you don't need it
X-content-type-tptions nosniff
// Refuse to embed other websites, please delete if not needed
X-frame-options DENY
// HSTS response header, delete if not needed
Strict-Transport-Security max-age=63072000;includeSubDomains;preload
}
}
// A sample reverse proxy configuration
yjz.hk {
// Specify the proxy webpage access addresshttps://xx.r2w.dev
reverse_proxy https://xx.r2w.dev {
// Specify the request domain name:hk.r2w.dev
header_up Host {hk.r2w.dev}
}
// The following configuration has been introduced above, and will not be repeated here.
tls /etc/ssl/caddy/r2wind.cn.crt /etc/ssl/caddy/r2wind.cn.key
handle_errors {
rewrite * /{http.error.status_code}.html
file_server
}
log {
output file /www/log/yjz_hk.log
}
encode gzip
file_server
header {
X-content-type-tptions nosniff
x-xss-protection: 1; mode=block
Strict-Transport-Security max-age=63072000;includeSubDomains;preload
}
}
// A redirect example configuration
www.yjz.hk {
// Specify the redirected website address and carry relevant parameters
redir https://yjz.hk{uri}
}
// A multi-domain redirection example configuration, remember to separate multiple domain names with ",", remember to enter a space after the comma and then enter the domain name
dnstest.cc, www.dnstest.cc, r2wind.net, www.r2wind.net {
redir https://r2wind.cn
}
// An example configuration of a multi-domain website is the same as that of a normal website, but with a few more domain names
r2wind.com,r2wind.net, r2wind.cn {
// Configure the website root directory here, please upload the page file to the website root directory
root * /www/wwwroot/r2wind.com/public
// Configure the SSL certificate path. If you do not configure it, Caddy will automatically apply and match it for you. Note: It is best not to specify an SSL certificate here, unless your certificate has multiple domain names.
tls /etc/ssl/caddy/r2wind.cn.crt /etc/ssl/caddy/r2wind.cn.key
// Customize the error page file, if you don't need to delete it
handle_errors {
rewrite * /{http.error.status_code}.html
file_server
}
// The log storage path, if you do not need to save the access log, you can delete it
log {
output file /www/log/r2wind_com.log
}
// Enable Gzip compression, delete it if you don't need it
encode gzip
file_server
// used to add response headers
header {
// Disables the client's MIME type sniffing behavior, remove it if you don't need it
X-content-type-tptions nosniff
// Refuse to embed other websites, please delete if not needed
X-frame-options DENY
// HSTS response header, delete if not needed
Strict-Transport-Security max-age=63072000;includeSubDomains;preload
}
}

Reload the configuration file

1
systemctl reload caddy

If the configuration is correct, no prompt will appear after pressing Enter

Effect display

The effect will not be shown here. Just upload the website file to the root directory of the website, modify the configuration file and reload Caddy, and then configure the domain name resolution to access the corresponding domain name.