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~
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.
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:
// 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.