Skip to content

Commit 953af4d

Browse files
authored
Merge pull request #82 from francislavoie/patch-1
Add Caddy to deployment docs
2 parents bc96f07 + 4ff627c commit 953af4d

1 file changed

Lines changed: 117 additions & 1 deletion

File tree

docs/best-practices/deployment.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ provides even more awesome features with its built-in web server.
1010
No matter what existing PHP stack you're using, X runs anywhere.
1111
This means that if you've already used PHP before, X will *just work*.
1212

13-
* nginx with PHP-FPM
13+
* nginx or Caddy with PHP-FPM
1414
* Apache with PHP-FPM, mod_fcgid, mod_cgi or mod_php
1515
* Any other web server using FastCGI to talk to PHP-FPM
1616
* Linux, Mac and Windows operating systems (<abbrev title="Apache, MySQL or MariaDB, PHP, on Linux, Mac or Windows operating systems">LAMP, MAMP, WAMP</abbrev>)
@@ -100,6 +100,65 @@ $ curl http://localhost/
100100
Hello wörld!
101101
```
102102

103+
### Caddy
104+
105+
Caddy is an extensible, cross-platform, open-source web server written in Go.
106+
Many projects use Caddy because of its ease of use in configuration, and its
107+
headlining feature, Automatic HTTPS, which provisions TLS certificates for your
108+
sites and keeps them renewed.
109+
110+
X supports Caddy out of the box. If you've used Caddy before to run any PHP
111+
application, using Caddy with X is as simple as dropping the project files in
112+
the right directory. Accordingly, this guide assumes you want to process a
113+
number of [dynamic routes](../api/app.md#routing) through X and optionally
114+
include some public assets (such as style sheets and images).
115+
116+
> ℹ️ **PHP-FPM or reverse proxy?**
117+
>
118+
> This section assumes you want to use Caddy with PHP-FPM which is a very common,
119+
> traditional web stack. If you want to get the most out of X, you may also
120+
> want to look into using the built-in web server with
121+
> [Caddy's reverse proxy](#caddy-reverse-proxy).
122+
123+
Assuming you've followed the [quickstart guide](../getting-started/quickstart.md),
124+
all you need to do is to point Caddy's [`root` directive](https://caddyserver.com/docs/caddyfile/directives/root)
125+
to the `public/` directory of your project. On top of this, you'll need
126+
to instruct Caddy to process any dynamic requests through X. This can be
127+
achieved by using an `Caddyfile` configuration with the following contents:
128+
129+
```
130+
example.com {
131+
root * /var/www/html/public
132+
encode gzip
133+
php_fastcgi localhost:9000
134+
file_server
135+
}
136+
```
137+
138+
Caddy's [`php_fastcgi` directive](https://caddyserver.com/docs/caddyfile/directives/php_fastcgi)
139+
is ready out-of-the-box to serve modern PHP sites. This will also automatically
140+
provision a TLS certificate for your domain (e.g. `example.com` – replace it
141+
with your own domain) on startup, assuming your DNS is properly configured to
142+
point to your server, and your server is publicly accessible on ports 80 and 443.
143+
144+
> ℹ️ **New to Caddy?**
145+
>
146+
> A complete `Caddyfile` configuration is out of scope for this guide, so we assume
147+
> you already have Caddy and PHP with PHP-FPM up and running. In this example,
148+
> we're assuming PHP-FPM is already up and running and listens on `localhost:9000`,
149+
> consult your search engine of choice for basic install instructions. Once this
150+
> is set up, the above guide should be everything you need to then use X. We
151+
> recommend using the above Caddy configuration as a starting point if you're
152+
> unsure.
153+
154+
Once done, you can check your web application responds as expected. Use your
155+
favorite web browser or command-line tool:
156+
157+
```bash
158+
$ curl https://example.com/
159+
Hello wörld!
160+
```
161+
103162
### Apache
104163

105164
The Apache HTTP server (httpd) is one of the most popular web servers. In
@@ -342,6 +401,63 @@ $ curl http://localhost/
342401
Hello wörld!
343402
```
344403

404+
### Caddy reverse proxy
405+
406+
If you're using the built-in web server, X will listen on `http://127.0.0.1:8080`
407+
[by default](#listen-address). Instead of using the `X_LISTEN` environment to
408+
change to a publicly accessible listen address, it's usually recommended to use
409+
a reverse proxy instead for production deployments.
410+
411+
By using Caddy as a reverse proxy, we can leverage a high performance web server
412+
to handle static assets (such as style sheets and images) and proxy any
413+
requests to [dynamic routes](../api/app.md#routing) through X. On top of this,
414+
we can configure Caddy to log requests, handle rate limits, and to provide HTTPS
415+
support (TLS termination).
416+
417+
Assuming you've followed the [quickstart guide](../getting-started/quickstart.md),
418+
all you need to do is to point the Caddy's [`root` directive](https://caddyserver.com/docs/caddyfile/directives/root)
419+
to the `public/` directory of your project. On top of this, you'll need
420+
to instruct Caddy to process any dynamic requests through X. This can be
421+
achieved by using a `Caddyfile` configuration with the following contents:
422+
423+
```
424+
example.com {
425+
root * /var/www/html/public;
426+
427+
@static {
428+
file {path} {path}/
429+
not path *.php
430+
}
431+
handle @static {
432+
rewrite * {http.matchers.file.relative}
433+
file_server
434+
}
435+
436+
handle {
437+
reverse_proxy localhost:8080
438+
}
439+
}
440+
```
441+
442+
> ℹ️ **New to Caddy?**
443+
>
444+
> A complete Caddy configuration is out of scope for this guide, so we assume
445+
> you already have Caddy up and running. Unlike [using Caddy with PHP-FPM](#caddy),
446+
> this example does not require a PHP-FPM setup.
447+
>
448+
> We recommend using the above `Caddyfile` configuration as a starting point if you're
449+
> unsure. In this basic form, it instructs Caddy to server any requests for
450+
> files _do_ exist, and proxy everything else to your X server, which processes any
451+
> requests by checking your [registered routes](../api/app.md#routing).
452+
453+
Once done, you can check your web application responds as expected. Use your
454+
favorite web browser or command-line tool:
455+
456+
```bash
457+
$ curl https://example.com/
458+
Hello wörld!
459+
```
460+
345461
### Docker containers
346462

347463
> ⚠️ **Documentation still under construction**

0 commit comments

Comments
 (0)