mirror of
https://github.com/caddyserver/website.git
synced 2025-05-06 11:47:12 -04:00
Replace original Getting Started for now
This commit is contained in:
parent
1965a6b120
commit
9b230cd2ac
3 changed files with 215 additions and 492 deletions
|
@ -2,275 +2,276 @@
|
|||
title: "Getting Started"
|
||||
---
|
||||
|
||||
# Getting Started
|
||||
# Introduction
|
||||
|
||||
This guide will help you get a site up and running with Caddy on **Linux** in a matter of minutes, but it is _not the only way_ to do it. There are many ways you can [download](/download), [install](/docs/install), [configure](/docs/introduction), and [run](/docs/running) Caddy. **If you are already comfortable with setting up services using systemd, jump to [Introduction](/docs/introduction).**
|
||||
|
||||
You can jump out of this tutorial at any time when you feel like you know what to do next.
|
||||
Welcome to Caddy! This tutorial will explore the basics of using Caddy and help you get familiar with it at a high level.
|
||||
|
||||
**Objectives:**
|
||||
- 🔲 Install Caddy as a service
|
||||
- 🔲 Discover the unit configuration
|
||||
- 🔲 Prepare your site
|
||||
- 🔲 Serve your site over HTTPS
|
||||
- 🔲 Add a reverse proxy
|
||||
- 🔲 Learn how to troubleshoot problems
|
||||
- 🔲 Run the daemon
|
||||
- 🔲 Try the API
|
||||
- 🔲 Give Caddy a config
|
||||
- 🔲 Test config
|
||||
- 🔲 Make a Caddyfile
|
||||
- 🔲 Use the config adapter
|
||||
- 🔲 Start with an initial config
|
||||
- 🔲 Compare JSON and Caddyfile
|
||||
- 🔲 Compare API and config files
|
||||
- 🔲 Run in the background
|
||||
- 🔲 Zero-downtime config reload
|
||||
|
||||
**Prerequisites:**
|
||||
- A computer where you have administrator, root, or sudo privileges
|
||||
- Know how to use a terminal / command line
|
||||
- Familiarity with Unix permissions
|
||||
- Be comfortable editing text files
|
||||
- A registered domain name
|
||||
- Basic terminal / command line skills
|
||||
- Basic text editor skills
|
||||
- `caddy` and `curl` in your PATH
|
||||
|
||||
---
|
||||
|
||||
First, ensure no other web servers are running on your machine (to avoid port-binding conflicts).
|
||||
**If you [installed Caddy](/docs/install) from a package manager, Caddy might already be running as a service. If so, please stop the service before doing this tutorial.**
|
||||
|
||||
**[Install Caddy](/docs/install) by following the instructions for your system.** For example, if you're on Ubuntu, follow the steps that use `apt`; on Fedora, use `dnf`; etc. If a package isn't available for your distro, you can also [manually install Caddy as a service](/docs/running) on any Linux machine that has systemd.
|
||||
Let's start by running it:
|
||||
|
||||
<pre><code class="cmd bash">caddy</code></pre>
|
||||
|
||||
Oops; without a subcommand, the `caddy` command only displays help text. You can use this any time you forget what to do.
|
||||
|
||||
To start Caddy as a daemon, use the `run` subcommand:
|
||||
|
||||
<pre><code class="cmd bash">caddy run</code></pre>
|
||||
|
||||
<aside class="complete">Run the daemon</aside>
|
||||
|
||||
This blocks forever, but what is it doing? At the moment... nothing. By default, Caddy's configuration ("config") is blank. We can verify this using the [admin API](/docs/api) in another terminal:
|
||||
|
||||
<pre><code class="cmd bash">curl localhost:2019/config/</code></pre>
|
||||
|
||||
<aside class="tip">
|
||||
|
||||
All you really need to run Caddy is the executable file itself. "Installing" Caddy could be defined simply as putting Caddy in your PATH. But installing Caddy _as a service_ is best practice for production systems because generally, a service keeps the process running after reboots, implements a tighter permissions/security model, and centralizes logging.
|
||||
This is **not** your website: the administration endpoint at localhost:2019 is used for controlling Caddy and is restricted to localhost by default.
|
||||
|
||||
</aside>
|
||||
|
||||
Verify that the Caddy service is running:
|
||||
|
||||
<pre><code class="cmd bash">systemctl status caddy</code></pre>
|
||||
<aside class="complete">Try the API</aside>
|
||||
|
||||
You should see output like this:
|
||||
We can make Caddy useful by giving it a config. This can be done many ways, but we'll start by making a POST request to the [/load](/docs/api#post-load) endpoint using `curl` in the next section.
|
||||
|
||||
```plain
|
||||
● caddy.service - Caddy
|
||||
Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
|
||||
Active: active (running) since Tue 2022-09-06 21:15:31 MDT; 1 day 1h ago
|
||||
...
|
||||
```
|
||||
|
||||
Ensure it says "enabled" and "active (running)" -- these are crucial for production. (Enabled means the service will be started automatically after a reboot.)
|
||||
|
||||
If Caddy failed to start, the most likely cause is you have another web server running. The output will also show the most recent log lines which you can inspect for error messages. After resolving the issue, run `sudo systemctl start caddy` to try again.
|
||||
## Your first config
|
||||
|
||||
<aside class="complete">Install Caddy as a service</aside>
|
||||
To prepare our request, we need to make a config. At its core, Caddy's configuration is simply a [JSON document](/docs/json/).
|
||||
|
||||
How is your web server's service configured? A unit file (ending with `.service`) contains the service configuration. The [default unit file we provide](https://github.com/caddyserver/dist/blob/master/init/caddy.service) runs Caddy with a configuration file. It tells you the precise `caddy` command, the location of the Caddy config, special permissions, the process' working directory, and any environment variables passed to the process.
|
||||
Save this to a JSON file (e.g. `caddy.json`):
|
||||
|
||||
Notice the output above shows the location of the `caddy.service` file. Let's print its contents:
|
||||
|
||||
<pre><code class="cmd bash">cat /lib/systemd/system/caddy.service</code></pre>
|
||||
|
||||
You will see the `ExecStart=` line, which defines the command to execute. The `ExecReload=` line the command that is executed to reload the configuration when you do `systemctl reload caddy`. The `--config` flag in the `caddy` commands is the location of the Caddy configuration file. Take note of this, as its location varies by platform (but it is often `/etc/caddy/Caddyfile`).
|
||||
|
||||
<aside class="complete">Discover the unit configuration</aside>
|
||||
|
||||
- **If you are running Caddy locally on your own computer:** you can go to [http://localhost](http://localhost) in your browser and you should see a slanted page telling you how to get rid of the slanted page. (It's our default "welcome" page to let you know the server is working. 🙃) You can follow those instructions if you want; they're similar to this tutorial but more succinct.
|
||||
|
||||
- **If you are setting up a remote server:** you can either run `curl localhost` on that machine, or you can navigate to the IP address of your server in your web browser or run `curl <ip>` locally.
|
||||
|
||||
## Your first site
|
||||
|
||||
Copy your site's static files (HTML, CSS, JS, images, etc.) into a folder that is accessible to the `caddy` user/group. This folder is called the _site root_. In production, it's often `/var/www/html` and we'll use that path in this tutorial. Expect that everything in this directory will become publicly accessible.
|
||||
|
||||
Once your site files are in place, make sure Caddy can access them; for example:
|
||||
|
||||
<pre><code class="cmd bash">chown -R caddy:caddy /var/www/html</code></pre>
|
||||
|
||||
Next, we need to tell Caddy where to find the site root. Open the configuration file you noted above (e.g. `/etc/caddy/Caddyfile`) and change its `root` directive to point to your site's directory:
|
||||
|
||||
```caddy
|
||||
:80 {
|
||||
root * /var/www/html
|
||||
file_server
|
||||
}
|
||||
```
|
||||
|
||||
<aside class="tip">
|
||||
|
||||
The permissions on this Caddyfile are restricted to prevent tampering and accidental changes. To save changes to this Caddyfile, you may need to open it as root (`sudo`).
|
||||
|
||||
</aside>
|
||||
|
||||
Be sure to save the changes.
|
||||
|
||||
Since we changed the Caddyfile, we need to load the new config into Caddy:
|
||||
|
||||
<pre><code class="cmd bash">sudo systemctl reload caddy</code></pre>
|
||||
|
||||
- **If that failed:** double-check your Caddyfile. Spaces are significant; make sure it looks tidy. Check file and folder permissions. Ensure the path is correct.
|
||||
|
||||
- **If that succeeded:** open your browser to [http://localhost](http://localhost) again and you should see your site! If you don't, make sure the file permissions are correct and that you have an index file in your site root (e.g. `index.html`).
|
||||
|
||||
<aside class="tip">
|
||||
|
||||
Advanced sites often have additional configuration in production to [set headers](/docs/caddyfile/directives/header), enable [compression](/docs/caddyfile/directives/encode) or use compressed [sidecar files](/docs/caddyfile/directives/file_server), and [enable HTTP request logging](/docs/caddyfile/directives/log).
|
||||
|
||||
</aside>
|
||||
|
||||
<aside class="complete">Prepare your site</aside>
|
||||
|
||||
|
||||
## HTTPS
|
||||
|
||||
Given a domain name, Caddy will obtain a TLS certificate for your site and keep it renewed while it stays running. It's all [automatic](/docs/automatic-https)!
|
||||
|
||||
Before continuing, **point your domain to your server's IP address.** This means setting the value of your domain's A/AAAA record(s) to the public IP address of your server.
|
||||
|
||||
Usually this means logging into your DNS provider and creating or changing the A (IPv4) and/or AAAA (IPv6) record for your domain (it can be a subdomain). We'll use `example.com` for this tutorial. Verify it has been set by running `dig example.com`.
|
||||
|
||||
Then, **verify that your server's IP is publicly routable on the standard Web ports (80 and 443).** Ensure there are no firewalls or routers blocking these ports. On a home network, you may need to forward those ports to your machine (just be aware that your machine will become publicly accessible on those ports).
|
||||
|
||||
Once your DNS and network infrastructure are properly configured, **all you need to do is replace `:80` in the Caddyfile with your domain name:**
|
||||
|
||||
```caddy
|
||||
example.com {
|
||||
root * /var/www/html
|
||||
file_server
|
||||
}
|
||||
```
|
||||
|
||||
Then reload the config once again:
|
||||
|
||||
<pre><code class="cmd bash">sudo systemctl reload caddy</code></pre>
|
||||
|
||||
Watch the logs to make sure it works:
|
||||
|
||||
<pre><code class="cmd bash">journalctl -u caddy -f</code></pre>
|
||||
|
||||
- **If it succeeds:** navigate to your site in your browser and see your site served over HTTPS, just like that!
|
||||
- **If it fails:** refer to the [troubleshooting tips](#troubleshooting) below.
|
||||
|
||||
As you have just seen, Caddy serves sites over HTTPS automatically and by default (unless you explicitly configure `http://` or the HTTP port). As long as you keep your network and DNS properly configured, Caddy will keep your certificates renewed automatically.
|
||||
|
||||
Caddy is the only server that works like this!
|
||||
|
||||
<aside class="complete">Serve your site over HTTPS</aside>
|
||||
|
||||
If you don't want to use a public domain name or are running this internally or locally instead, you can easily have Caddy use [fully-managed self-signed certificates](/docs/automatic-https#local-https) by specifying either:
|
||||
|
||||
- your local/internal IP address,
|
||||
- the hostname `localhost`,
|
||||
- or any hostname that ends in `.localhost`
|
||||
|
||||
instead of a registered public domain name.
|
||||
|
||||
|
||||
## Reverse proxy
|
||||
|
||||
Oftentimes, your site consists of a backend application, but you want to put Caddy in front to handle TLS, routing, and other network-related details. Caddy's proxy is easy to use and extremely powerful.
|
||||
|
||||
For example, if we have a backend that provides the site's API endpoints, we can easily proxy those with just 1 additional line of configuration:
|
||||
|
||||
```caddy
|
||||
example.com {
|
||||
root * /var/www/html
|
||||
file_server
|
||||
reverse_proxy /api/* 127.0.0.1:9000
|
||||
}
|
||||
```
|
||||
|
||||
Notice the `reverse_proxy` directive. The first argument, `/api/*`, is a [path matcher](/docs/caddyfile/matchers) which filters only requests within `/api/`. Then it proxies those to the backend app listening on :9000.
|
||||
|
||||
If your backend is a PHP app, simply replace the `reverse_proxy` directive with the `php_fastcgi` directive:
|
||||
|
||||
```caddy
|
||||
example.com {
|
||||
root * /var/www/html
|
||||
file_server
|
||||
php_fastcgi /api/* 127.0.0.1:9000
|
||||
}
|
||||
```
|
||||
|
||||
Make sure the address is the same as your php-fpm listener.
|
||||
|
||||
Note that you don't have to enable a file server or set a site root if you _only_ want to proxy requests. You can enable a proxy by itself:
|
||||
|
||||
|
||||
```caddy
|
||||
example.com {
|
||||
reverse_proxy 127.0.0.1:9000
|
||||
}
|
||||
```
|
||||
|
||||
That config terminates TLS and proxies everything to port 9000.
|
||||
|
||||
<aside class="complete">Add a reverse proxy</aside>
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
The most important task when trying to fix a problem is to first get the error message(s) and/or logs.
|
||||
|
||||
### Debug logs
|
||||
|
||||
Enable debug logging if you haven't already. Put this at the top of your Caddyfile:
|
||||
|
||||
```caddy
|
||||
```json
|
||||
{
|
||||
debug
|
||||
"apps": {
|
||||
"http": {
|
||||
"servers": {
|
||||
"example": {
|
||||
"listen": [":2015"],
|
||||
"routes": [
|
||||
{
|
||||
"handle": [{
|
||||
"handler": "static_response",
|
||||
"body": "Hello, world!"
|
||||
}]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
A block at the very top of the file without any name is called a [global options block](/docs/caddyfile/options). If you already have a global options block, simply add the `debug` option to it; you can't have two global option blocks.
|
||||
<aside class="tip">
|
||||
|
||||
Reload your Caddy configuration and you will observe DEBUG-level logs which can give helpful insights!
|
||||
You do not have to use config files, but we are for this tutorial. Caddy's [admin API](/docs/api) is designed for use by other programs or scripts.
|
||||
|
||||
</aside>
|
||||
|
||||
|
||||
### Request logs
|
||||
Then upload it:
|
||||
|
||||
Caddy can also log all the HTTP requests it receives (sometimes known as "access logs"). Simply add the `log` directive within your site block. For example:
|
||||
<pre><code class="cmd bash">curl localhost:2019/load \
|
||||
-H "Content-Type: application/json" \
|
||||
-d @caddy.json
|
||||
</code></pre>
|
||||
|
||||
<aside class="complete">Give Caddy a config</aside>
|
||||
|
||||
We can verify that Caddy applied our new config with another GET request:
|
||||
|
||||
<pre><code class="cmd bash">curl localhost:2019/config/</code></pre>
|
||||
|
||||
Test that it works by going to [localhost:2015](http://localhost:2015) in your browser or use `curl`:
|
||||
|
||||
<pre><code class="cmd"><span class="bash">curl localhost:2015</span>
|
||||
Hello, world!</code></pre>
|
||||
|
||||
If you see _Hello, world!_, then congrats -- it's working! It's always a good idea to make sure your config works as you expect, especially before deploying into production.
|
||||
|
||||
<aside class="complete">Test config</aside>
|
||||
|
||||
|
||||
## Your first Caddyfile
|
||||
|
||||
That was _kind of a lot of work_ just for Hello World.
|
||||
|
||||
Another way to configure Caddy is with the [**Caddyfile**](/docs/caddyfile). The same config we wrote in JSON above can be expressed simply as:
|
||||
|
||||
```caddy
|
||||
example.com {
|
||||
root * /var/www/html
|
||||
file_server
|
||||
log
|
||||
}
|
||||
:2015
|
||||
|
||||
respond "Hello, world!"
|
||||
```
|
||||
|
||||
These logs are printed to the same place as Caddy's runtime or process logs (stderr), but have the name `http.log.access` so you can tell them apart. Access logs show you great detail about HTTP requests and responses.
|
||||
|
||||
### curl
|
||||
Save that to a file named `Caddyfile` (no extension) in the current directory.
|
||||
|
||||
If your site isn't working the way you expect, avoid using a web browser unless you know what you're doing. Browser behavior is often overly magical, misleading, inconsistent, and frustrating, as it hides or obfuscates the underlying technical details you need to debug your site.
|
||||
<aside class="complete">Make a Caddyfile</aside>
|
||||
|
||||
Use `curl -v` instead. The `-v` option prints HTTP information including the header which is vital to knowing what is happening. For example:
|
||||
Stop Caddy if it is already running (Ctrl+C), then run:
|
||||
|
||||
<pre><code class="cmd bash">curl -v https://example.com/</code></pre>
|
||||
<pre><code class="cmd bash">caddy adapt</code></pre>
|
||||
|
||||
Or if you stored the Caddyfile somewhere else or named it something other than `Caddyfile`:
|
||||
|
||||
<pre><code class="cmd bash">caddy adapt --config /path/to/Caddyfile</code></pre>
|
||||
|
||||
You will see JSON output! What happened here?
|
||||
|
||||
We just used a [_config adapter_](/docs/config-adapters) to convert our Caddyfile to Caddy's native JSON structure.
|
||||
|
||||
<aside class="complete">Use the config adapter</aside>
|
||||
|
||||
While we could take that output and make another API request, we can skip all those steps because the `caddy` command can do it for us. If there is a file called Caddyfile in the current directory and no other config is specified, Caddy will load the Caddyfile, adapt it for us, and run it right away.
|
||||
|
||||
Now that there is a Caddyfile in the current folder, let's do `caddy run` again:
|
||||
|
||||
<pre><code class="cmd bash">caddy run</code></pre>
|
||||
|
||||
Or if your Caddyfile is somewhere else:
|
||||
|
||||
<pre><code class="cmd bash">caddy run --config /path/to/Caddyfile</code></pre>
|
||||
|
||||
(If it is called something else that doesn't start with "Caddyfile", you will need to specify `--adapter caddyfile`.)
|
||||
|
||||
You can now try loading your site again and you will see that it is working!
|
||||
|
||||
<aside class="complete">Start with an initial config</aside>
|
||||
|
||||
As you can see, there are several ways you can start Caddy with an initial config:
|
||||
|
||||
- A file named Caddyfile in the current directory
|
||||
- The `--config` flag (optionally with the `--adapter` flag)
|
||||
- The `--resume` flag (if a config was loaded previously)
|
||||
|
||||
|
||||
## JSON vs. Caddyfile
|
||||
|
||||
Now you know that the Caddyfile is just converted to JSON for you.
|
||||
|
||||
The Caddyfile seems easier than JSON, but should you always use it? There are pros and cons to each approach. The answer depends on your requirements and use case.
|
||||
|
||||
JSON | Caddyfile
|
||||
-----|----------
|
||||
Easy to generate | Easy to craft by hand
|
||||
Easily programmable | Awkward to automate
|
||||
Extremely expressive | Moderately expressive
|
||||
Full range of Caddy functionality | Most of Caddy functionality
|
||||
Allows config traversal | Cannot traverse within Caddyfile
|
||||
Partial config changes | Whole config changes only
|
||||
Can be exported | Cannot be exported
|
||||
Compatible with all API endpoints | Compatible with some API endpoints
|
||||
Documentation generated automatically | Documentation is hand-written
|
||||
Ubiquitous | Niche
|
||||
More efficient | More computational
|
||||
Kind of boring | Kind of fun
|
||||
**Learn more: [JSON structure](/docs/json/)** | **Learn more: [Caddyfile docs](/docs/caddyfile)**
|
||||
|
||||
You will need to decide which is best for your use case.
|
||||
|
||||
It is important to note that both JSON and the Caddyfile (and [any other supported config adapter](/docs/config-adapters)) can be used with [Caddy's API](/docs/api). However, you get the full range of Caddy's functionality and API features if you use JSON. If using a config adapter, the only way to load or change the config with the API is the [/load endpoint](/docs/api#post-load).
|
||||
|
||||
<aside class="complete">Compare JSON and Caddyfile</aside>
|
||||
|
||||
|
||||
## API vs. Config files
|
||||
|
||||
<aside class="tip">
|
||||
|
||||
To perform the exact same HTTP request as your browser, open its dev tools, go to the Network tab, and right-click the request. There should be an option like "Copy as curl". Then paste that into your terminal.
|
||||
Under the hood, even config files go through Caddy's API endpoints; the `caddy` command just wraps up those API calls for you.
|
||||
|
||||
</aside>
|
||||
|
||||
Obviously, change the URL to the one you are trying to debug. You will see curl establish a TLS connection (if HTTPS), make an HTTP request, then print the resulting status code, response headers, and body. It does not follow redirects, enable compression, cache anything, think it is smarter than you, or do anything unexpected. The `curl` command is your true friend and ally in the war against errors.
|
||||
|
||||
Combined with server logs, curl requests are quite a powerful way to gain insights to what is happening.
|
||||
You will also want to decide whether your workflow is API-based or CLI-based. (You _can_ use both the API and config files on the same server, but we don't recommend it: best to have one source of truth.)
|
||||
|
||||
|
||||
### Certificates
|
||||
|
||||
If Caddy is having trouble getting certificates, leave Caddy running while you double-check your network and DNS configurations. These cause the _vast majority_ of problems.
|
||||
API | Config files
|
||||
----|-------------
|
||||
Make config changes with HTTP requests | Make config changes with shell commands
|
||||
Easy to scale | Difficult to scale
|
||||
Difficult to manage by hand | Easy to manage by hand
|
||||
Really fun | Also fun
|
||||
**Learn more: [API tutorial](/docs/api-tutorial)** | **Learn more: [Caddyfile tutorial](/docs/caddyfile-tutorial)**
|
||||
|
||||
<aside class="tip">
|
||||
|
||||
Caddy has special programming for handling certificate automation errors. It will retry with other CAs, gradually back off, and use test CAs (if available) until success. It's usually OK to leave it running while you fix any problems.
|
||||
|
||||
Manually managing a server's configuration with the API is totally doable with proper tools, for example: any REST client application.
|
||||
</aside>
|
||||
|
||||
Check the error messages: Caddy prints the errors as returned by the CA, so they can be quite helpful. For example, a connection timeout indicates the CA couldn't connect to your server, suggesting a problem with your network configuration or DNS records pointing to the wrong network.
|
||||
The choice of API or config file workflow is orthogonal to the use of config adapters: you can use JSON but store it in a file and use the command line interface; conversely, you can also use the Caddyfile with the API.
|
||||
|
||||
- Make sure your A/AAAA records are correct.
|
||||
- Make sure ports 80 and 443 are publicly accessible
|
||||
- Make sure Caddy—not another server—is on the receiving end of ports 80 and 443
|
||||
But most people will use JSON+API or Caddyfile+CLI combinations.
|
||||
|
||||
### Simplify
|
||||
As you can see, Caddy is well-suited for a wide variety of use cases and deployments!
|
||||
|
||||
Many times, configuration files contain more than is needed or relevant to troubleshoot a problem. Try removing everything in your config file except the absolute minimum needed to make the site function. For example, you could disable compression or remove headers added in the reverse proxy. Incrementally making changes to your config will tell you lots about what is causing the problem.
|
||||
<aside class="complete">Compare API and config files</aside>
|
||||
|
||||
And if nothing undesireable happens or breaks when you remove some config, then you removed config that was unnecessary. Congrats!
|
||||
|
||||
If you're behind a CDN like Cloudflare, consider disabling it temporarily while you troubleshoot. If the problem goes away, you can know it is related to your CDN configuration.
|
||||
|
||||
<aside class="complete">Learn how to troubleshoot problems</aside>
|
||||
## Start, stop, run
|
||||
|
||||
Since Caddy is a server, it runs indefinitely. That means your terminal won't unblock after you execute `caddy run` until the process is terminated (usually with Ctrl+C).
|
||||
|
||||
Although `caddy run` is the most common and is usually recommended (especially when making a system service!), you can alternatively use `caddy start` to start Caddy and have it run in the background:
|
||||
|
||||
<pre><code class="cmd bash">caddy start</code></pre>
|
||||
|
||||
This will let you use your terminal again, which is convenient in some interactive headless environments.
|
||||
|
||||
You will then have to stop the process yourself, since Ctrl+C won't stop it for you:
|
||||
|
||||
<pre><code class="cmd bash">caddy stop</code></pre>
|
||||
|
||||
Or use [the /stop endpoint](/docs/api#post-stop) of the API.
|
||||
|
||||
<aside class="complete">Run in the background</aside>
|
||||
|
||||
|
||||
## Reloading config
|
||||
|
||||
Your server can perform zero-downtime config reloads/changes.
|
||||
|
||||
All [API endpoints](/docs/api) that load or change config are graceful with zero downtime.
|
||||
|
||||
When using the command line, however, it may be tempting to use Ctrl+C to stop your server and then restart it again to pick up the new configuration. Don't do this: stopping and starting the server is orthogonal to config changes, and will result in downtime.
|
||||
|
||||
<aside class="tip">
|
||||
Stopping your server will cause the server to go down.
|
||||
</aside>
|
||||
|
||||
Instead, use the [`caddy reload`](/docs/command-line#caddy-reload) command for a graceful config change:
|
||||
|
||||
<pre><code class="cmd bash">caddy reload</code></pre>
|
||||
|
||||
This actually just uses the API under the hood. It will load and, if necessary, adapt your config file to JSON, then gracefully replace the active configuration without downtime.
|
||||
|
||||
If there are any errors loading the new config, Caddy rolls back to the last working config.
|
||||
|
||||
<aside class="tip">
|
||||
Technically, the new config is started before the old config is stopped, so for a brief time, both configs are running! If the new config fails, it aborts with an error, while the old one is simply not stopped.
|
||||
</aside>
|
||||
|
||||
<aside class="complete">Zero-downtime config reload</aside>
|
||||
|
|
|
@ -1,277 +0,0 @@
|
|||
---
|
||||
title: "Introduction to Caddy"
|
||||
---
|
||||
|
||||
# Introduction
|
||||
|
||||
Welcome to Caddy! This tutorial will explore the basics of using Caddy and help you get familiar with it at a high level.
|
||||
|
||||
**Objectives:**
|
||||
- 🔲 Run the daemon
|
||||
- 🔲 Try the API
|
||||
- 🔲 Give Caddy a config
|
||||
- 🔲 Test config
|
||||
- 🔲 Make a Caddyfile
|
||||
- 🔲 Use the config adapter
|
||||
- 🔲 Start with an initial config
|
||||
- 🔲 Compare JSON and Caddyfile
|
||||
- 🔲 Compare API and config files
|
||||
- 🔲 Run in the background
|
||||
- 🔲 Zero-downtime config reload
|
||||
|
||||
**Prerequisites:**
|
||||
- Basic terminal / command line skills
|
||||
- Basic text editor skills
|
||||
- `caddy` and `curl` in your PATH
|
||||
|
||||
---
|
||||
|
||||
**If you [installed Caddy](/docs/install) from a package manager, Caddy might already be running as a service. If so, please stop the service before doing this tutorial.**
|
||||
|
||||
Let's start by running it:
|
||||
|
||||
<pre><code class="cmd bash">caddy</code></pre>
|
||||
|
||||
Oops; without a subcommand, the `caddy` command only displays help text. You can use this any time you forget what to do.
|
||||
|
||||
To start Caddy as a daemon, use the `run` subcommand:
|
||||
|
||||
<pre><code class="cmd bash">caddy run</code></pre>
|
||||
|
||||
<aside class="complete">Run the daemon</aside>
|
||||
|
||||
This blocks forever, but what is it doing? At the moment... nothing. By default, Caddy's configuration ("config") is blank. We can verify this using the [admin API](/docs/api) in another terminal:
|
||||
|
||||
<pre><code class="cmd bash">curl localhost:2019/config/</code></pre>
|
||||
|
||||
<aside class="tip">
|
||||
|
||||
This is **not** your website: the administration endpoint at localhost:2019 is used for controlling Caddy and is restricted to localhost by default.
|
||||
|
||||
</aside>
|
||||
|
||||
|
||||
<aside class="complete">Try the API</aside>
|
||||
|
||||
We can make Caddy useful by giving it a config. This can be done many ways, but we'll start by making a POST request to the [/load](/docs/api#post-load) endpoint using `curl` in the next section.
|
||||
|
||||
|
||||
|
||||
## Your first config
|
||||
|
||||
To prepare our request, we need to make a config. At its core, Caddy's configuration is simply a [JSON document](/docs/json/).
|
||||
|
||||
Save this to a JSON file (e.g. `caddy.json`):
|
||||
|
||||
```json
|
||||
{
|
||||
"apps": {
|
||||
"http": {
|
||||
"servers": {
|
||||
"example": {
|
||||
"listen": [":2015"],
|
||||
"routes": [
|
||||
{
|
||||
"handle": [{
|
||||
"handler": "static_response",
|
||||
"body": "Hello, world!"
|
||||
}]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<aside class="tip">
|
||||
|
||||
You do not have to use config files, but we are for this tutorial. Caddy's [admin API](/docs/api) is designed for use by other programs or scripts.
|
||||
|
||||
</aside>
|
||||
|
||||
|
||||
Then upload it:
|
||||
|
||||
<pre><code class="cmd bash">curl localhost:2019/load \
|
||||
-H "Content-Type: application/json" \
|
||||
-d @caddy.json
|
||||
</code></pre>
|
||||
|
||||
<aside class="complete">Give Caddy a config</aside>
|
||||
|
||||
We can verify that Caddy applied our new config with another GET request:
|
||||
|
||||
<pre><code class="cmd bash">curl localhost:2019/config/</code></pre>
|
||||
|
||||
Test that it works by going to [localhost:2015](http://localhost:2015) in your browser or use `curl`:
|
||||
|
||||
<pre><code class="cmd"><span class="bash">curl localhost:2015</span>
|
||||
Hello, world!</code></pre>
|
||||
|
||||
If you see _Hello, world!_, then congrats -- it's working! It's always a good idea to make sure your config works as you expect, especially before deploying into production.
|
||||
|
||||
<aside class="complete">Test config</aside>
|
||||
|
||||
|
||||
## Your first Caddyfile
|
||||
|
||||
That was _kind of a lot of work_ just for Hello World.
|
||||
|
||||
Another way to configure Caddy is with the [**Caddyfile**](/docs/caddyfile). The same config we wrote in JSON above can be expressed simply as:
|
||||
|
||||
```caddy
|
||||
:2015
|
||||
|
||||
respond "Hello, world!"
|
||||
```
|
||||
|
||||
|
||||
Save that to a file named `Caddyfile` (no extension) in the current directory.
|
||||
|
||||
<aside class="complete">Make a Caddyfile</aside>
|
||||
|
||||
Stop Caddy if it is already running (Ctrl+C), then run:
|
||||
|
||||
<pre><code class="cmd bash">caddy adapt</code></pre>
|
||||
|
||||
Or if you stored the Caddyfile somewhere else or named it something other than `Caddyfile`:
|
||||
|
||||
<pre><code class="cmd bash">caddy adapt --config /path/to/Caddyfile</code></pre>
|
||||
|
||||
You will see JSON output! What happened here?
|
||||
|
||||
We just used a [_config adapter_](/docs/config-adapters) to convert our Caddyfile to Caddy's native JSON structure.
|
||||
|
||||
<aside class="complete">Use the config adapter</aside>
|
||||
|
||||
While we could take that output and make another API request, we can skip all those steps because the `caddy` command can do it for us. If there is a file called Caddyfile in the current directory and no other config is specified, Caddy will load the Caddyfile, adapt it for us, and run it right away.
|
||||
|
||||
Now that there is a Caddyfile in the current folder, let's do `caddy run` again:
|
||||
|
||||
<pre><code class="cmd bash">caddy run</code></pre>
|
||||
|
||||
Or if your Caddyfile is somewhere else:
|
||||
|
||||
<pre><code class="cmd bash">caddy run --config /path/to/Caddyfile</code></pre>
|
||||
|
||||
(If it is called something else that doesn't start with "Caddyfile", you will need to specify `--adapter caddyfile`.)
|
||||
|
||||
You can now try loading your site again and you will see that it is working!
|
||||
|
||||
<aside class="complete">Start with an initial config</aside>
|
||||
|
||||
As you can see, there are several ways you can start Caddy with an initial config:
|
||||
|
||||
- A file named Caddyfile in the current directory
|
||||
- The `--config` flag (optionally with the `--adapter` flag)
|
||||
- The `--resume` flag (if a config was loaded previously)
|
||||
|
||||
|
||||
## JSON vs. Caddyfile
|
||||
|
||||
Now you know that the Caddyfile is just converted to JSON for you.
|
||||
|
||||
The Caddyfile seems easier than JSON, but should you always use it? There are pros and cons to each approach. The answer depends on your requirements and use case.
|
||||
|
||||
JSON | Caddyfile
|
||||
-----|----------
|
||||
Easy to generate | Easy to craft by hand
|
||||
Easily programmable | Awkward to automate
|
||||
Extremely expressive | Moderately expressive
|
||||
Full range of Caddy functionality | Most of Caddy functionality
|
||||
Allows config traversal | Cannot traverse within Caddyfile
|
||||
Partial config changes | Whole config changes only
|
||||
Can be exported | Cannot be exported
|
||||
Compatible with all API endpoints | Compatible with some API endpoints
|
||||
Documentation generated automatically | Documentation is hand-written
|
||||
Ubiquitous | Niche
|
||||
More efficient | More computational
|
||||
Kind of boring | Kind of fun
|
||||
**Learn more: [JSON structure](/docs/json/)** | **Learn more: [Caddyfile docs](/docs/caddyfile)**
|
||||
|
||||
You will need to decide which is best for your use case.
|
||||
|
||||
It is important to note that both JSON and the Caddyfile (and [any other supported config adapter](/docs/config-adapters)) can be used with [Caddy's API](/docs/api). However, you get the full range of Caddy's functionality and API features if you use JSON. If using a config adapter, the only way to load or change the config with the API is the [/load endpoint](/docs/api#post-load).
|
||||
|
||||
<aside class="complete">Compare JSON and Caddyfile</aside>
|
||||
|
||||
|
||||
## API vs. Config files
|
||||
|
||||
<aside class="tip">
|
||||
|
||||
Under the hood, even config files go through Caddy's API endpoints; the `caddy` command just wraps up those API calls for you.
|
||||
|
||||
</aside>
|
||||
|
||||
|
||||
You will also want to decide whether your workflow is API-based or CLI-based. (You _can_ use both the API and config files on the same server, but we don't recommend it: best to have one source of truth.)
|
||||
|
||||
API | Config files
|
||||
----|-------------
|
||||
Make config changes with HTTP requests | Make config changes with shell commands
|
||||
Easy to scale | Difficult to scale
|
||||
Difficult to manage by hand | Easy to manage by hand
|
||||
Really fun | Also fun
|
||||
**Learn more: [API tutorial](/docs/api-tutorial)** | **Learn more: [Caddyfile tutorial](/docs/caddyfile-tutorial)**
|
||||
|
||||
<aside class="tip">
|
||||
Manually managing a server's configuration with the API is totally doable with proper tools, for example: any REST client application.
|
||||
</aside>
|
||||
|
||||
The choice of API or config file workflow is orthogonal to the use of config adapters: you can use JSON but store it in a file and use the command line interface; conversely, you can also use the Caddyfile with the API.
|
||||
|
||||
But most people will use JSON+API or Caddyfile+CLI combinations.
|
||||
|
||||
As you can see, Caddy is well-suited for a wide variety of use cases and deployments!
|
||||
|
||||
<aside class="complete">Compare API and config files</aside>
|
||||
|
||||
|
||||
|
||||
## Start, stop, run
|
||||
|
||||
Since Caddy is a server, it runs indefinitely. That means your terminal won't unblock after you execute `caddy run` until the process is terminated (usually with Ctrl+C).
|
||||
|
||||
Although `caddy run` is the most common and is usually recommended (especially when making a system service!), you can alternatively use `caddy start` to start Caddy and have it run in the background:
|
||||
|
||||
<pre><code class="cmd bash">caddy start</code></pre>
|
||||
|
||||
This will let you use your terminal again, which is convenient in some interactive headless environments.
|
||||
|
||||
You will then have to stop the process yourself, since Ctrl+C won't stop it for you:
|
||||
|
||||
<pre><code class="cmd bash">caddy stop</code></pre>
|
||||
|
||||
Or use [the /stop endpoint](/docs/api#post-stop) of the API.
|
||||
|
||||
<aside class="complete">Run in the background</aside>
|
||||
|
||||
|
||||
## Reloading config
|
||||
|
||||
Your server can perform zero-downtime config reloads/changes.
|
||||
|
||||
All [API endpoints](/docs/api) that load or change config are graceful with zero downtime.
|
||||
|
||||
When using the command line, however, it may be tempting to use Ctrl+C to stop your server and then restart it again to pick up the new configuration. Don't do this: stopping and starting the server is orthogonal to config changes, and will result in downtime.
|
||||
|
||||
<aside class="tip">
|
||||
Stopping your server will cause the server to go down.
|
||||
</aside>
|
||||
|
||||
Instead, use the [`caddy reload`](/docs/command-line#caddy-reload) command for a graceful config change:
|
||||
|
||||
<pre><code class="cmd bash">caddy reload</code></pre>
|
||||
|
||||
This actually just uses the API under the hood. It will load and, if necessary, adapt your config file to JSON, then gracefully replace the active configuration without downtime.
|
||||
|
||||
If there are any errors loading the new config, Caddy rolls back to the last working config.
|
||||
|
||||
<aside class="tip">
|
||||
Technically, the new config is started before the old config is stopped, so for a brief time, both configs are running! If the new config fails, it aborts with an error, while the old one is simply not stopped.
|
||||
</aside>
|
||||
|
||||
<aside class="complete">Zero-downtime config reload</aside>
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
<li class="heading">Tutorials</li>
|
||||
<li><a href="/docs/getting-started">Getting Started</a></li>
|
||||
<li><a href="/docs/introduction">Introduction</a></li>
|
||||
<li>
|
||||
<a href="/docs/quick-starts">Quick-starts</a>
|
||||
<ul>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue