介绍 (Introduction)
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.
Caddy是一款基于简单性和安全性而设计的Web服务器,具有许多可用于托管网站的功能。 例如,它可以从Let's Encrypt自动获取和管理TLS证书以启用HTTPS,并包括对HTTP / 2的支持。 HTTPS是一种用于保护用户与服务器之间的流量的系统,并且正Swift成为生产环境中运行的任何网站的基本要求-如果没有HTTPS,Chrome和Firefox将警告用户如果您尝试提交登录名,则您的网站“不安全”信息。
Previously, the recommended method for installing Caddy was to download pre-built binaries from the Caddy project website. However, changes in how Caddy’s licensing works means that you’re no longer allowed to use these pre-built binaries for commercial purposes unless you pay a license fee, even if you’re just using Caddy internally within a business. Luckily, the Caddy source code is still fully open-source and you can build Caddy yourself to avoid running into licensing issues.
以前,推荐的安装Caddy的方法是从Caddy项目网站下载预构建的二进制文件。 但是,Caddy许可工作方式的变化意味着,除非您支付许可费,否则就不再允许将这些预制的二进制文件用于商业目的,即使您只是在企业内部使用Caddy也不例外。 幸运的是,Caddy源代码仍然是完全开源的,您可以自己构建Caddy以避免遇到许可问题。
In this tutorial, you’ll build Caddy from source and use it to host a website secured with HTTPS. This entails compiling it, configuring it using a Caddyfile
and installing plugins. In the end, you’ll learn how to upgrade your installation when a new version is released.
在本教程中,您将从源代码构建Caddy,并将其用于托管使用HTTPS保护的网站。 这需要对其进行编译,使用Caddyfile
对其进行配置并安装插件。 最后,您将学习在发布新版本时如何升级安装。
先决条件 (Prerequisites)
An Ubuntu 18.04 server with root privileges, and a secondary, non-root account. You can set this up by following our Initial Server Setup Guide for Ubuntu 18.04. For this tutorial the non-root user is
sammy
.具有root用户特权的Ubuntu 18.04服务器和一个非root用户辅助帐户。 您可以按照我们的Ubuntu 18.04初始服务器安装指南进行设置 。 在本教程中,非root用户是
sammy
。A fully registered domain name. This tutorial will use
your_domain
throughout. You can purchase a domain name on Namecheap, get one for free on Freenom, or use the domain registrar of your choice.完全注册的域名。 本教程将整个使用
your_domain
。 你可以购买一个域名Namecheap ,免费获得一个在Freenom ,或使用你选择的域名注册商。An A DNS record with
your_domain
pointing to your server’s public IP address. You can follow this introduction to DigitalOcean DNS for details on how to add them.一个DNS记录,其中
your_domain
指向服务器的公共IP地址。 您可以按照DigitalOcean DNS简介进行操作,以获取有关如何添加它们的详细信息。The Go language toolchain installed on your server. Follow our guide on How To Install Go and Set Up a Local Programming Environment on Ubuntu 18.04 to set up Go. You don’t need to create any example projects.
服务器上安装的Go语言工具链。 请遵循我们的有关如何在Ubuntu 18.04上安装Go并设置本地编程环境的指南来设置Go。 您无需创建任何示例项目。
A personal access token (API key) with read and write permissions for your DigitalOcean account. Visit How to Create a Personal Access Token to create one.
具有对DigitalOcean帐户的读写权限的个人访问令牌(API密钥)。 访问如何创建个人访问令牌以创建一个。
第1步-建立球童 (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.
在此步骤中,您将从源代码构建Caddy,并具有以后添加插件的能力,而无需更改Caddy的源代码。
For the purposes of this tutorial, you’ll store the source code under ~/caddy
. Create that directory by running the following command:
就本教程而言,您会将源代码存储在~/caddy
。 通过运行以下命令来创建该目录:
- mkdir ~/caddy 麦克迪尔〜/ caddy
Navigate to it:
导航到它:
- cd ~/caddy cd〜/球童
You’ll store the source code for running and customizing Caddy in a file named caddy.go
. Create it using the following command:
您会将用于运行和自定义Caddy的源代码存储在名为caddy.go
的文件中。 使用以下命令创建它:
- nano caddy.go 纳米球童
Add the following lines:
添加以下行:
package main
import (
"github.com/caddyserver/caddy/caddy/caddymain"
)
func main() {
// caddymain.EnableTelemetry = false
caddymain.Run()
}
This code imports Caddy directly from Github (using Git) and starts it from the entrance main
function. If you wish to enable telemetry, uncomment the caddymain.EnableTelemetry
line and set the value to true
. When you are done, save and close the file.
该代码直接从Github导入Caddy(使用Git),并从入口main
函数启动它。 如果要启用遥测,请取消注释caddymain.EnableTelemetry
行并将其值设置为true
。 完成后,保存并关闭文件。
For caddy.go
to be able to use the imported dependencies, you’ll need to initialize it as a module:
为了使caddy.go
能够使用导入的依赖项,您需要将其初始化为模块:
- go mod init caddy 去mod初始化球童
Output
go: creating new go.mod: module caddy
At this point, you’re all set to build the stock version of Caddy from the above source code by running:
至此,您都可以通过运行以下代码从上述源代码构建Caddy的普通版:
- go install 去安装
There will be a lot of output, detailing what libraries Go downloaded as dependencies necessary for compiling. The resulting executable is stored under $GOPATH/bin
, as explained in the prerequisites.
将有很多输出,详细说明了哪些库要下载作为编译所需的依赖项。 如前提条件中所述,生成的可执行文件存储在$GOPATH/bin
下。
When it finishes, try running Caddy:
完成后,请尝试运行Caddy:
- caddy 球童
You’ll see output similar to the following:
您将看到类似于以下内容的输出:
Output
Activating privacy features... do