Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Caddy webserver configuration and tutorial on Caddy with Cloudflare DNS in proxy mode #535

Merged
merged 16 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
cddd051
Add guide on setting up Caddy as webserver and tutorial for using Cad…
vojtechmares Jul 2, 2023
08c0406
style(caddy snippets): use spaces instead of tabs (4 wide)
vojtechmares Sep 2, 2023
3061cd4
style(creating_ssl_certificates.md): capitalize fist letter in number…
vojtechmares Nov 13, 2023
163c623
fix(caddy): uncomment env HTTPS "on"
vojtechmares Nov 13, 2023
f7751b0
fix(snippets/caddy): simplify logging
vojtechmares Nov 13, 2023
f32dee9
fix(snippets/caddy): explicit comment for disabling https
vojtechmares Nov 13, 2023
9af02fa
refactor(panel/webserver_configuration.md): do not validate caddy, on…
vojtechmares Nov 13, 2023
3c36018
refactor(caddy): use :80 suffix for HTTP configuration
vojtechmares Nov 13, 2023
bad00b7
refactor(webserver_configuration.md): instruct user to delete default…
vojtechmares Nov 28, 2023
4c46daa
refactor(webserver_configuration.md): remove highlight from server di…
vojtechmares Nov 28, 2023
8ffb97d
refactor(webserver_configuration.md): add extra `:` to tabs, for hier…
vojtechmares Nov 28, 2023
0c61c3c
refactor(webserver_configuration.md): than -> then
vojtechmares Nov 29, 2023
02510f0
refactor(webserver_configuration.md): move section describing how to …
vojtechmares Nov 29, 2023
432c6cd
refactor(creating_ssl_certificates.md): move file path out of snippet
vojtechmares Nov 29, 2023
f1ab970
chore(webserver_configuration.md): remove whitespace (new line) after…
vojtechmares Nov 29, 2023
10bf4f1
fix(webserver_configuration.md): typo
vojtechmares Jan 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .snippets/webservers/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
servers :443 {
timeouts {
read_body 120s
}
}
}

<domain> {
root * /var/www/pterodactyl/public

file_server

php_fastcgi unix//run/php/php8.1-fpm.sock {
root /var/www/pterodactyl/public
index index.php

env PHP_VALUE "upload_max_filesize = 100M \n post_max_size = 100M"
env HTTP_PROXY ""
# env HTTPS "on"

read_timeout 300s
dial_timeout 300s
write_timeout 300s
}

header Strict-Transport-Security "max-age=16768000; preload;"
header X-Content-Type-Options "nosniff"
header X-XSS-Protection "1; mode=block;"
header X-Robots-Tag "none"
header Content-Security-Policy "frame-ancestors 'self'"
header X-Frame-Options "DENY"
header Referrer-Policy "same-origin"

request_body {
max_size 100m
}

respond /.ht* 403

log {
output file /var/log/caddy/pterodactyl.app-access.log {
roll_size 100MiB
roll_keep_for 7d
}
level INFO
}

log {
output file /var/log/caddy/pterodcatyl.app-error.log {
roll_size 100MiB
roll_keep_for 7d
}
level ERROR
}
}
56 changes: 56 additions & 0 deletions .snippets/webservers/Caddyfile-nossl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
servers :80 {
timeouts {
read_body 120s
}
}
}

http://<domain> {
vojtechmares marked this conversation as resolved.
Show resolved Hide resolved
root * /var/www/pterodactyl/public

file_server

php_fastcgi unix//run/php/php8.1-fpm.sock {
root /var/www/pterodactyl/public
index index.php

env PHP_VALUE "upload_max_filesize = 100M \n post_max_size = 100M"
env HTTP_PROXY ""
# env HTTPS "on"

read_timeout 300s
dial_timeout 300s
write_timeout 300s
}

header Strict-Transport-Security "max-age=16768000; preload;"
header X-Content-Type-Options "nosniff"
header X-XSS-Protection "1; mode=block;"
header X-Robots-Tag "none"
header Content-Security-Policy "frame-ancestors 'self'"
header X-Frame-Options "DENY"
header Referrer-Policy "same-origin"

request_body {
max_size 100m
}

respond /.ht* 403

log {
output file /var/log/caddy/pterodactyl.app-access.log {
roll_size 100MiB
roll_keep_for 7d
}
level INFO
}

log {
output file /var/log/caddy/pterodcatyl.app-error.log {
roll_size 100MiB
roll_keep_for 7d
}
level ERROR
}
}
48 changes: 48 additions & 0 deletions panel/1.0/webserver_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
When using the SSL configuration you MUST create SSL certificates, otherwise your webserver will fail to start. See the [Creating SSL Certificates](/tutorials/creating_ssl_certificates.html) documentation page to learn how to create these certificates before continuing.
:::

::: tip
If you are using [Caddy With Automatic SSL](#caddy-with-automatic-ssl), you do not have to create SSL certificates manually, Caddy will take care of it automatically.
:::

:::: tabs
::: tab "Nginx With SSL"
First, remove the default NGINX configuration.
Expand Down Expand Up @@ -109,6 +113,50 @@ sudo a2enmod rewrite
sudo systemctl restart apache2
```

:::
::: tab "Caddy With Automatic SSL"

You should paste the contents of the file bellow, replacing `<domain>` with your domain name.
vojtechmares marked this conversation as resolved.
Show resolved Hide resolved

The default config path is `/etc/caddy/Caddyfile`.
vojtechmares marked this conversation as resolved.
Show resolved Hide resolved

<<< @/.snippets/webservers/Caddyfile{9}

::: tip
If you are using Cloudflare DNS in proxy mode, refer to [this tutorial](/tutorials/creating_ssl_certificates.html#method-3:-caddy-(using-cloudflare-api)), to see how to configure Caddy to use DNS challenge for obtaining SSL certificates.
:::

### Enabling Configuration

The final step is to validate configuration and restart Caddy.

```bash
caddy validate --config /etc/caddy/Caddyfile
vojtechmares marked this conversation as resolved.
Show resolved Hide resolved

systemctl restart caddy
```

:::
::: tab "Caddy Without SSL"

You should paste the contents of the file bellow, replacing `<domain>` with your domain name.

The default config path is `/etc/caddy/Caddyfile`.

The only two differences are that we have prefixed the `<domain>` with `http://` (or you can use `:80` suffix behind the `<domain>`) and in the global config at `servers` directive, we have changed the port from `:443` to `:80`.

<<< @/.snippets/webservers/Caddyfile-nossl{2,9}

### Enabling Configuration

The final step is to validate configuration and restart Caddy.

```bash
caddy validate --config /etc/caddy/Caddyfile

systemctl restart caddy
```

:::
::::

Expand Down
83 changes: 83 additions & 0 deletions tutorials/creating_ssl_certificates.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,88 @@ After running the script for the first time, it will be added to the crontab aut
sudo crontab -e
```

:::
::: tab "Method 3: Caddy (using Cloudflare API)"
This is for advanced users, who are running Cloudflare in proxy mode or do not have access to port `80`.

### Installing Caddy with Cloudflare DNS plugin

Caddy does not come by default with Cloudflare DNS plugin, you need to install it yourself.

There are two main methods:

1. using `xcaddy` - CLI tool to build your own Caddy build
2. downloading prebuilt binary from [Caddy's download page](https://caddyserver.com/download).
3. using Ansible to download and install Caddy with plugins. See [caddy-ansible](https://github.com/caddy-ansible/caddy-ansible)
vojtechmares marked this conversation as resolved.
Show resolved Hide resolved

#### Build Caddy using `xcaddy` on your server

Please refer to [Caddy docs on building Caddy](https://caddyserver.com/docs/build#xcaddy).

### Obtaining CloudFlare API Token

After installing acme.sh, we need to fetch a CloudFlare API key. Please make sure that a DNS record (A or CNAME record) is pointing to your target node, and set the cloud to grey (bypassing CloudFlare proxy). Then go to My Profile > API keys and on Global API Key subtab, click on "view", enter your CloudFlare password, and copy the API key to clipboard.

After install Caddy with Cloudflare DNS plugin, we need to fetch a Cloudflare API token. Please make sure that a DNS record (A or CNAME record) is pointing at your target node. Then go to My Profile > API Tokens and on API Tokens click "Create Token". Create API Token > API token templates, at the end of line with "Edit zone DNS", click "Use template". Under **Zone Resources**, select your DNS zone for which you wish to create the API token, click "Continue to summary". Review the API token summary and click "Create Token". And finally copy the API token to clipboard.

### Reconfiguring Caddy to use Cloudflare DNS for obtaining certificates

Create an environment variable file (like `.env`), keep in mind that this file contains secrets and should not be accessed by public.

```bash
# /etc/caddy/.secrets.env
vojtechmares marked this conversation as resolved.
Show resolved Hide resolved
CLOUDFLARE_API_TOKEN=<your cloudflare api token>
```

For security reasons, we recommend setting permissions to `0600` (only owner can read or write to the file).

```bash
# Set ownership of the `.secrets.env` file to `caddy` system user
chown caddy:caddy /etc/caddy/.secrets.env

# Set read-write permissions only to owner - the `caddy` system user
chmod 0600 /etc/caddy/.secrets.env
```

Modify the systemd unit file, to load environment variables from file (add `--envfile /etc/caddy/.secrets.env` flag to `ExecStart`):

```unit{12}
# /etc/systemd/system/caddy.service
vojtechmares marked this conversation as resolved.
Show resolved Hide resolved
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --envfile /etc/caddy/.secrets.env --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target
```

You can add a `tls` block to your `Caddyfile`, under the `<domain>` block of your panel configuration:

```caddyfile{5-7}
# /etc/caddy/Caddyfile
vojtechmares marked this conversation as resolved.
Show resolved Hide resolved
<domain> {
# ...

tls {
dns cloudflare {env.CLOUDFLARE_API_TOKEN}
}
}
```

:::
::::