Skip to content
Closed
6 changes: 5 additions & 1 deletion caddy/php-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func cmdPHPServer(fs caddycmd.Flags) (int, error) {
if _, err := os.Stat("php.ini"); err == nil {
iniScanDir := os.Getenv("PHP_INI_SCAN_DIR")

if err := os.Setenv("PHP_INI_SCAN_DIR", iniScanDir+":"+frankenphp.EmbeddedAppPath); err != nil {
newDir := frankenphp.EmbeddedAppPath
if iniScanDir != "" {
newDir = iniScanDir + ":" + newDir
}
if err := os.Setenv("PHP_INI_SCAN_DIR", newDir); err != nil {
return caddy.ExitCodeFailedStartup, err
}
}
Expand Down
83 changes: 81 additions & 2 deletions docs/cn/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ localhost {
# ...
```

Worker 块也可以在 `php` 或 `php_server` 块内定义。在这种情况下,worker 继承父指令的环境变量和根路径,并且只能由该特定域访问:

```caddyfile
{
frankenphp
}
example.com {
root /path/to/app
php_server {
root <path>
worker {
file <path, 可以相对于 root>
num <num>
env <key> <value>
watch <path>
name <name>
}
}
}
```

如果在同一服务器上运行多个应用,还可以定义多个 worker:

```caddyfile
Expand All @@ -98,6 +119,7 @@ other.example.com {
worker index.php <num>
}
}

# ...
```

Expand Down Expand Up @@ -131,8 +153,8 @@ route {

```caddyfile
php_server [<matcher>] {
root <directory> # 设置站点的根目录。默认值:`root` 指令。
split_path <delim...> # 设置用于将 URI 拆分为两部分的子字符串。第一个匹配的子字符串将用于从路径中拆分"路径信息"。第一个部分以匹配的子字符串为后缀,并将假定为实际资源(CGI 脚本)名称。第二部分将设置为PATH_INFO,供脚本使用。默认值:`.php`
root <directory> # 设置站点的根目录。默认值:`root` 指令。如果未指定
split_path <delim...> # 设置用于将 URI 拆分为两部分的子字符串。第一个匹配的子字符串将用于从路径中拆分路径信息。第一个部分以匹配的子字符串为后缀,并将假定为实际资源(CGI 脚本)名称。第二部分将设置为PATH_INFO,供脚本使用。默认值:`.php`
resolve_root_symlink false # 禁用将 `root` 目录在符号链接时将其解析为实际值(默认启用)。
env <key> <value> # 设置额外的环境变量,可以设置多个环境变量。
file_server off # 禁用内置的 file_server 指令。
Expand All @@ -157,10 +179,67 @@ php_server [<matcher>] {

## PHP 配置

您还可以使用 `frankenphp` 块中的 `php_ini` 指令更改 PHP 配置:

```caddyfile
{
frankenphp {
php_ini memory_limit 256M

# 或者

php_ini {
memory_limit 256M
max_execution_time 15
}
}
}
```

要加载 [其他 PHP INI 配置文件](https://www.php.net/manual/en/configuration.file.php#configuration.file.scan),
可以使用 `PHP_INI_SCAN_DIR` 环境变量。
设置后,PHP 将加载给定目录中存在 `.ini` 扩展名的所有文件。

## php-server 命令

`php-server` 命令是启动生产就绪 PHP 服务器的便捷方式。它特别适用于快速部署、演示、开发或运行[嵌入式应用](embed.md)。

```console
frankenphp php-server [--domain <example.com>] [--root <path>] [--listen <addr>] [--worker /path/to/worker.php<,nb-workers>] [--watch <paths...>] [--access-log] [--debug] [--no-compress] [--mercure]
```

### 选项

- `--domain`, `-d`: 提供文件的域名。如果指定,服务器将使用 HTTPS 并自动获取 Let's Encrypt 证书。
- `--root`, `-r`: 站点根目录的路径。如果未指定并使用嵌入式应用,默认将使用 embedded_app/public 目录。
- `--listen`, `-l`: 绑定监听器的地址。默认为 `:80`,如果指定了域名则为 `:443`。
- `--worker`, `-w`: 要运行的 worker 脚本。可以多次指定以运行多个 worker。
- `--watch`: 监视文件更改的目录。可以多次指定以监视多个目录。
- `--access-log`, `-a`: 启用访问日志。
- `--debug`, `-v`: 启用详细调试日志。
- `--mercure`, `-m`: 启用内置的 Mercure.rocks hub。
- `--no-compress`: 禁用 Zstandard、Brotli 和 Gzip 压缩。

### 示例

使用当前目录作为文档根目录启动服务器:

```console
frankenphp php-server --root ./
```

启动启用 HTTPS 的服务器:

```console
frankenphp php-server --domain example.com
```

启动带有 worker 的服务器:

```console
frankenphp php-server --worker public/index.php
```

## 启用调试模式

使用 Docker 镜像时,将 `CADDY_GLOBAL_OPTIONS` 环境变量设置为 `debug` 以启用调试模式:
Expand Down
13 changes: 13 additions & 0 deletions docs/cn/embed.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ composer install --ignore-platform-reqs --no-dev -a
composer dump-env prod
```

### 自定义配置

要自定义[配置](config.md),您可以在要嵌入的应用程序的主目录中放置 `Caddyfile` 和 `php.ini` 文件。
如果嵌入了带有 Caddyfile 的应用程序,运行二进制文件时将自动使用该 Caddyfile 的配置。

如果您想直接从嵌入式应用程序目录提供文件,请指定 "root ./",因为不指定 root 选项将默认使用 embedded_app/public。

## 创建 Linux 二进制文件

创建 Linux 二进制文件的最简单方法是使用我们提供的基于 Docker 的构建器。
Expand Down Expand Up @@ -102,6 +109,12 @@ EMBED=/path/to/your/app \
./my-app php-server
```

默认情况下,如果未指定 root 选项,它将使用 embedded_app/public 目录作为文档根目录。如果您想直接从嵌入式应用程序目录提供服务,请指定 root 选项:

```console
./my-app php-server --root ./
```

如果您的应用包含 [worker 脚本](worker.md),请使用如下命令启动 worker:

```console
Expand Down
39 changes: 39 additions & 0 deletions docs/cn/worker.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@
启动应用程序一次并将其保存在内存中。
FrankenPHP 将在几毫秒内处理传入的请求。

## Caddyfile 配置

Worker 块可以通过两种方式定义:

在全局 frankenphp 块中,如[配置文档](config.md#caddyfile-config)中所述:

```caddyfile
{
frankenphp {
worker {
file <路径>
num <数量>
env <键> <值>
watch <路径>
name <名称>
}
}
}
```

在 php 或 php_server 块内:

```caddyfile
example.com {
root /path/to/app
php_server {
worker {
file <路径>
num <数量>
env <键> <值>
watch <路径>
name <名称>
}
}
}
```

当在 php 或 php_server 块内定义时,worker 会继承父指令的环境变量和根路径,并且只能由该特定的 php 或 php_server 访问。

## 启动 Worker 脚本

### Docker
Expand Down
79 changes: 70 additions & 9 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ Alternatively, you may use the one-line short form of the `worker` option:
# ...
```

Worker blocks can also be defined inside a `php` or `php_server` block. In this case, the worker inherits environment variables and root path from the parent directive and is only accessible by that specific domain:

```caddyfile
{
frankenphp
}
example.com {
root /path/to/app
php_server {
root <path>
worker {
file <path, can be relative to root>
num <num>
env <key> <value>
watch <path>
name <name>
}
}
}
```

You can also define multiple workers if you serve multiple apps on the same server:

```caddyfile
Expand Down Expand Up @@ -213,7 +234,7 @@ This is an opt-in configuration that needs to be added to the global options in
```caddyfile
{
servers {
enable_full_duplex
enable_full_duplex
}
}
```
Expand Down Expand Up @@ -253,19 +274,59 @@ You can also change the PHP configuration using the `php_ini` directive in the `

```caddyfile
{
frankenphp {
php_ini memory_limit 256M
frankenphp {
php_ini memory_limit 256M

# or
# or

php_ini {
memory_limit 256M
max_execution_time 15
}
}
php_ini {
memory_limit 256M
max_execution_time 15
}
}
}
```

## The php-server Command

The `php-server` command is a convenient way to start a production-ready PHP server. It's especially useful for quick deployments, demos, development, or to run an [embedded app](embed.md).

```console
frankenphp php-server [--domain <example.com>] [--root <path>] [--listen <addr>] [--worker /path/to/worker.php<,nb-workers>] [--watch <paths...>] [--access-log] [--debug] [--no-compress] [--mercure]
```

### Options

- `--domain`, `-d`: Domain name at which to serve the files. If specified, the server will use HTTPS and automatically obtain a Let's Encrypt certificate.
- `--root`, `-r`: The path to the root of the site. If not specified and using an embedded app, it will use the embedded_app/public directory by default.
- `--listen`, `-l`: The address to which to bind the listener. Default is `:80` or `:443` if a domain is specified.
- `--worker`, `-w`: Worker script to run. Can be specified multiple times for multiple workers.
- `--watch`: Directory to watch for file changes. Can be specified multiple times for multiple directories.
- `--access-log`, `-a`: Enable the access log.
- `--debug`, `-v`: Enable verbose debug logs.
- `--mercure`, `-m`: Enable the built-in Mercure.rocks hub.
- `--no-compress`: Disable Zstandard, Brotli and Gzip compression.

### Examples

Start a server with the current directory as the document root:

```console
frankenphp php-server --root ./
```

Start a server with HTTPS enabled:

```console
frankenphp php-server --domain example.com
```

Start a server with a worker:

```console
frankenphp php-server --worker public/index.php
```

## Enable the Debug Mode

When using the Docker image, set the `CADDY_GLOBAL_OPTIONS` environment variable to `debug` to enable the debug mode:
Expand Down
10 changes: 10 additions & 0 deletions docs/embed.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ composer dump-env prod
To customize [the configuration](config.md), you can put a `Caddyfile` as well as a `php.ini` file
in the main directory of the app to be embedded (`$TMPDIR/my-prepared-app` in the previous example).

If an app with a Caddyfile in the directory is embedded, it will use that Caddyfile's configuration automatically when running the binary.

If you want to serve files from the embedded app directly, specify "root ./", as not specifying the root option will serve embedded_app/public by default.

## Creating a Linux Binary

The easiest way to create a Linux binary is to use the Docker-based builder we provide.
Expand Down Expand Up @@ -106,6 +110,12 @@ To start the web app run:
./my-app php-server
```

By default, if no root option is specified, it will use the embedded_app/public directory as the document root. If you want to serve directly from the embedded app directory, specify the root option:

```console
./my-app php-server --root ./
```

If your app contains a [worker script](worker.md), start the worker with something like:

```console
Expand Down
Loading