As we all know, Web server is an indispensable basic service in Web development, which is often used in development. Well-known open source Web servers include Apache and Nginx. And the open source project we are going to introduce today is a Web server compiled by Go “up-and-comer” : Caddy, which has features such as download without installation and HTTPS with zero configuration, thus occupies a place in the strong Web server like the cloud.
In an era when HTTPS is mandatory, Caddy builds HTTPS sites in minutes with no extra configuration required, making it the preferred server for small and medium-sized Web services. Caddy is beloved by open source enthusiasts and has received 36,000 stars since it opened in 2014.
Project address: github.com/caddyserver…
Why Caddy?
Caddy is a powerful, extensible platform for servicing your site, services, and applications.
Caddy allows you to quickly deploy HTTP (S) sites or reverse proxy services on Linux, Mac, and Windows. Support:
- HTTP / 1.1 and HTTP / 2
- HTTPS can be automatically issued and manually managed
- Virtual host (multiple sites working on a single port)
- Native IPv4 and IPv6 support
- Static file distribution
- Smooth restart/reload
- Reverse proxy (HTTP or WebSocket)
- Load balancing and health check
- Markdown rendering
- Document Browsing service
- , etc.
Compared to traditional Nginx or Apache, Caddy has only one executable file, which is easy to install and not prone to strange dependencies. The configuration file structure is clear and the syntax is simple and easy to get started. Depending on the modular architecture, you can use Go to develop extension modules quickly.
Now let’s get our hands dirty.
Second, the installation
System environment: Ubuntu 20.04 LTS
Caddy can be installed as a system service with the command:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
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
sudo apt update
sudo apt install caddy
Copy the code
Note: after installation in this way, the service will be started automatically.
You can also download an executable file to use.
- Download it at caddyserver.com/download
- Select your own OPERATING system at the top of the page and click the blue Download button on the right
- The downloaded files cannot be run directly. You need to add permissions for them
$ mv caddy_linux_amd64 caddy # Rename the downloaded file to facilitate the following command input, according to the different system file name is different $ sudo chmod a+x caddy Add executable permissions to Caddy $ mv caddy /bin/caddy Copy Caddy to the bin directory so it can be used at any time on the command line Copy the code
Caddy version: caddy version: caddy version
V2.4.6 h1: HGkGICFGvyrodcqOOclHKfvJC0qTU7vny/fhyp9hnw = 7Copy the code
The installation is successful!
Complete HTTPS site from scratch
Here’s how to complete the HTTPS site with Caddy. It only takes 4 steps:
The domain name used for Tips is caddy.hellogithub.com.
Step 1: Install, using the commands described above
Step 2: set up the domain name resolution address, set up in the purchase domain name website. The diagram below:
It takes several minutes for the Settings to take effect. Run the following command to check whether the Settings take effect:
curl "https://cloudflare-dns.com/dns-query?name=caddy.hellogithub.com&type=A" -H "accept: application/dns-json"
Copy the code
The domain name Settings take effect and the following information is displayed:
{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"caddy.hellogithub.com","type":1}], "Answer" : [{" name ":" caddy.hellogithub.com ", "type" : 1, "TTL" : 592, the "data" : "107.150.122.176}]}Copy the code
Step 3: Create the configuration file Caddyfile, without additional Settings to enable HTTPS:
Caddy.hellogithub.com respond "Hello HTTPS!Copy the code
- The domain name
- Access the returned content
Step 4: Start by executing sudo caddy run in the same directory
Caddy uses the configuration in the Caddyfile file in the command directory by default.
Finally, check out caddy.hellogithub.com!
Four, start the tutorial
We’ll start with Caddy commands, move on to configuration, and finally show you how to use Caddy to configure and launch a web site with a practical example.
4.1 Common Commands
4.2 Configuration File
Caddy’s native configuration file uses JSON format. However, it provides Caddyfile as an interface for users to quickly configure site information, and Caddy automatically converts the Caddyfile configuration information into JSON configuration files at runtime.
Caddyfile is not as powerful as JSON configuration files, but it is sufficient for users who do not need complex configuration.
The format of the Caddyfile configuration command is as follows:
directive [<matcher>]<args... > {# matcher stands for matcher. If provided, this directive will only respond to the resources described by Matcher
subdirective [<args...>] Son # instructions
}
Copy the code
The file structure of Caddyfile is shown as follows:
4.3 presentation
Here’s how to set up a site with Caddy to deepen your understanding of configuration formats and command usage.
Directory structure:
.├ ── ├─ index.html ├─ hg.htmlCopy the code
The contents of the two page files index. HTML and hg.html are as follows:
<! -- index.html -->
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>Hello world!</body>
</html>
Copy the code
<! -- HG.html -->
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HelloGitHub</title>
</head>
<body>
HelloGitHub
</body>
</html>
Copy the code
The Caddyfile configuration is as follows:
Comment this section of the site configuration if you don't have wordpress
#:80 {# Deploy a wordpress site
# root * /var/www/wordpress
# php_fastcgi Unix //run/ PHP /php-version-fpm.sock
# file_server # Configure a file service
#}
http://localhost:3000 {
basicauth /public/* {
Localhost :3000/public/*
HG JDJhJDE0JGEySk9janFMdHlBY2Y0aVdQZklQak9HcmwzNDZhNFg0N3V5Ny9EZkZMZHB1Nkt4ZE5BNGJt
The password HelloGitHub is generated using the caddy hash-passowrd command
}
root * ./ Set the root directory of the current site to the current folder, * to match all requests
templates
file_server {
# configure the current site as a static file server, which can be used to build the blog system
hide .git Hide all.git files
}
}
:4000 {
reverse_proxy /public/* http://localhost:3000 Configure the reverse proxy
# will only match locahost:4000/public, forward to localhost:3000/public/
}
Copy the code
Enter in the current directory:
$ caddy run # start Caddy fixings
Copy the code
Finally, the effect is as follows:
-
Visit: http://localhost:3000 to see the page showing “Hello World!”
-
Visit: http://localhost:3000/public/HG.html prompts for a user name and password, to see page after verify correct.
-
If you visit http://localhost:4000, you are automatically redirected to port 3000
4.4 Interface management
In addition to the easy-to-understand configuration file approach, Caddy provides an interface to manage configuration. Through these interfaces, advanced functions such as Web server management automation and integrated publishing can be easily realized.
Interfaces and functions:
- POST/Load sets or replaces the active configuration
- POST /stop Stops the active configuration and exits the process
- GET /config/[path] Exports the configuration of the specified path
- POST /config/[path] Sets or replaces objects; Append to array
- PUT /config/[path] Creates a new object or inserts an array
- PATCH /config/[path] replaces existing object or array elements
- DELETE /config/[path] Deletes the value of the specified path
- Easily traverse configuration structures in JSON using @ID
- GET /reverse_proxy/upstreams Returns the current status upstream of the configured proxy
I won’t do the demo here because of space, but if you want to learn more, you can read Caddy’s official documentation.
Five, the summary
You should know something about Caddy. Do you have a feeling that you’ve never met before?
While its performance isn’t as good as Nginx’s, Caddy beats Nginx in terms of ease of use! The main thing is that HTTPS by default is so sweet, you don’t have to do anything to get HTTPS on your site for free, fast, and easy.
Finally, if you want to set up a static blog or site, give Caddy a try! Sweet is not sweet only “eat” to know ~
Here is HelloGitHub “Explain open source projects” series, for you to open the door of open source projects.