Skip to content

Commit 5235cb9

Browse files
committed
docs: embed
1 parent e3361c2 commit 5235cb9

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ You can also run command-line scripts with:
5454
* [Real-time](docs/mercure.md)
5555
* [Configuration](docs/config.md)
5656
* [Docker images](docs/docker.md)
57-
* [Compile from sources](docs/compile.md)
57+
* [Create **standalone**, self-executable PHP apps](docs/embed.md)
5858
* [Create static binaries](docs/static.md)
59+
* [Compile from sources](docs/compile.md)
5960
* [Known issues](docs/known-issues.md)
6061
* [Demo app (Symfony) and benchmarks](https://github.com/dunglas/frankenphp-demo)
6162
* [Go library documentation](https://pkg.go.dev/github.com/dunglas/frankenphp)

docs/embed.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# PHP Apps As Standalone Binaries
2+
3+
FrankenPHP has the ability to embed the source code and assets of PHP applications in a static, self-contained binary.
4+
5+
Thanks to this feature, PHP applications can be distributed as standalone binaries that include the application itself, the PHP interpreter and Caddy, a production-level web server.
6+
7+
## Preparing Your App
8+
9+
Before creating the self-contained binary be sure that your app is ready for embedding.
10+
11+
For instance you likely want to:
12+
13+
* Install the production dependencies of the app
14+
* Dump the autoloader
15+
* Enable the production mode of your application (if any)
16+
* Strip uneeded files such as `.git` or tests to reduce the size of your final binary
17+
18+
For instance, for a Symfony app, you can use the following commands:
19+
20+
```console
21+
# Export the project to get rid of .git/, etc
22+
mkdir $TMPDIR/my-prepared-app
23+
git archive HEAD | tar -x -C $TMPDIR/my-prepared-app
24+
cd $TMPDIR/my-prepared-app
25+
26+
# Set proper environment variables
27+
echo APP_ENV=prod > .env.local
28+
echo APP_DEBUG=0 >> .env.local
29+
30+
# Remove the tests
31+
rm -Rf tests/
32+
33+
# Install the dependencies
34+
composer install --ignore-platform-reqs --no-dev -a
35+
36+
# Optimize .env
37+
composer dump-env prod
38+
```
39+
40+
## Creating a Linux Binary
41+
42+
The easiest way to create a Linux binary is to use the Docker-based builder we provide.
43+
44+
1. Create a file named `static-build.Dockerfile` in the repository of your prepared app:
45+
46+
```dockerfile
47+
FROM --platform=linux/amd64 dunglas/frankenphp:static-builder
48+
49+
# Copy your app
50+
WORKDIR /go/src/app/dist/app
51+
COPY . .
52+
53+
# Build the static binary, be sure to select only the PHP extensions you want
54+
WORKDIR /go/src/app/
55+
RUN EMBED=dist/app/ \
56+
PHP_EXTENSIONS=ctype,iconv,pdo_sqlite \
57+
./build-static.sh
58+
```
59+
60+
2. Build:
61+
62+
```console
63+
docker build -t static-app -f static-build.Dockerfile .
64+
```
65+
66+
3. Extract the binary
67+
68+
```console
69+
docker cp $(docker create --name static-app-tmp static-app):/go/src/app/dist/frankenphp-linux-x86_64 my-app ; docker rm static-app-tmp
70+
```
71+
72+
The resulting binary is the file named `my-app` in the current directory.
73+
74+
## Creating a Binary for Other OSes
75+
76+
If you don't want to use Docker, or want to build a macOS binary, use the shell script we provide:
77+
78+
```console
79+
git clone https://github.com/dunglas/frankenphp
80+
cd frankenphp
81+
RUN EMBED=/path/to/your/app \
82+
PHP_EXTENSIONS=ctype,iconv,pdo_sqlite \
83+
./build-static.sh
84+
```
85+
86+
The resulting binary is the file named `frankenphp-<os>-<arch>` in the `dist/` directory.
87+
88+
## Using The Binary
89+
90+
This is it! The `my-app` file contains your self-contained app!
91+
92+
To start the web app run:
93+
94+
```console
95+
./my-app php-server
96+
```
97+
98+
If your app contains a [worker script](worker.md), start the worker with something like:
99+
100+
```console
101+
./my-app php-server --worker public/index.php
102+
```
103+
104+
You can also run the PHP CLI scripts embedded in your binary:
105+
106+
```console
107+
./my-app php-cli bin/console
108+
```
109+
110+
## Customizing The Build
111+
112+
[Read the static build documentation](static.md) to see how to customize the binary (extensions, PHP version...).
113+
114+
## Distributing The Binary
115+
116+
The created binary isn't compressed.
117+
To reduce the size of the file before sending it, you can compress it.
118+
119+
We recommend `xz`.

docs/static.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ it's possible to create a static build of FrankenPHP thanks to the great [static
55

66
With this method, a single, portable, binary will contain the PHP interpreter, the Caddy web server and FrankenPHP!
77

8+
FrankenPHP also supports [embedding the PHP app in the static binary](embed.md).
9+
810
## Linux
911

1012
We provide a Docker image to build a Linux static binary:

0 commit comments

Comments
 (0)