备忘录:Ubuntu 18.04系统的Caddy安装与配置

1 安装Caddy Web服务器

利用官方提供的脚本安装Caddy Web服务器:

$ curl https://getcaddy.com | bash -s personal
$ sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/caddy

如果还没安装curl的,可以通过以下代码进行安装:

$ sudo apt install curl -y

2 创建SSL证书存放目录

$ sudo mkdir /etc/ssl/caddy
$ sudo chown -R www-data:root /etc/ssl/caddy
$ sudo chmod 0770 /etc/ssl/caddy

3 创建网站文件目录

$ sudo mkdir /var/www
$ sudo touch /var/www/index.html
$ sudo nano /var/www/index.html

写入测试网页内容:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <h1 style="font-family: sans-serif">It's working!</h1> 
  </body>
</html>

4 创建Caddy配置文件

$ sudo mkdir /etc/caddy
$ sudo touch /etc/caddy/Caddyfile
$ sudo chown -R root:www-data /etc/caddy
$ sudo nano /etc/caddy/Caddyfile

在配置文件编辑状态下写入以下内容,以“www.abc.com”域名为例,并对特定路径“/abc/”进行端口“1080”转发。其中,“abc@gmail.com”为SSL证书申请邮箱地址:

http://www.abc.com {
  redir https://www.abc.com {url}
}
https://www.abc.com  {
  root /var/www/
  gzip
  index index.html
  tls abc@gmail.com
  proxy /abc/ 127.0.0.1:1080 {
    websocket
    header_upstream -Origin
  }
}

5 设Caddy为系统服务

$ sudo curl -s https://raw.githubusercontent.com/mholt/caddy/master/dist/init/linux-systemd/caddy.service -o /etc/systemd/system/caddy.service
$ sudo systemctl daemon-reload
$ sudo systemctl enable caddy.service
$ sudo systemctl status caddy.service

发表评论