|
| 1 | +# Symfony |
| 2 | + |
| 3 | +## Docker |
| 4 | + |
| 5 | +For [Symfony](https://symfony.com) projects, we recommend using [Symfony Docker](https://github.com/dunglas/symfony-docker), the official Symfony Docker setup maintained by FrankenPHP's author. It provides a complete Docker-based environment with FrankenPHP, automatic HTTPS, HTTP/2, HTTP/3, and worker mode support out of the box. |
| 6 | + |
| 7 | +## Local Installation |
| 8 | + |
| 9 | +Alternatively, you can run your Symfony projects with FrankenPHP from your local machine: |
| 10 | + |
| 11 | +1. [Download the binary corresponding to your system](../README.md#standalone-binary) |
| 12 | +2. Add the following configuration to a file named `Caddyfile` in the root directory of your Symfony project: |
| 13 | + |
| 14 | + ```caddyfile |
| 15 | + # The domain name of your server |
| 16 | + localhost { |
| 17 | + # Set the webroot to the public/ directory |
| 18 | + root public/ |
| 19 | + # Enable compression (optional) |
| 20 | + encode zstd br gzip |
| 21 | +
|
| 22 | +
|
| 23 | + # Rewrite requests to non-existing files to the front controller |
| 24 | + @phpRoute { |
| 25 | + not file {path} |
| 26 | + } |
| 27 | + rewrite @phpRoute index.php |
| 28 | +
|
| 29 | + @frontController path index.php |
| 30 | + php @frontController { |
| 31 | + # Optional: Enable worker mode for better performance |
| 32 | + worker { |
| 33 | + file ./public/index.php |
| 34 | + } |
| 35 | + } |
| 36 | +
|
| 37 | + file_server { |
| 38 | + hide *.php |
| 39 | + } |
| 40 | + } |
| 41 | + ``` |
| 42 | + |
| 43 | + See the [performance documentation](performance.md) for further optimizations. |
| 44 | + |
| 45 | +3. Start FrankenPHP from the root directory of your Symfony project: `frankenphp run` |
| 46 | + |
| 47 | +## Worker Mode |
| 48 | + |
| 49 | +Since Symfony 7.4, FrankenPHP worker mode is natively supported. |
| 50 | + |
| 51 | +For older versions, install the FrankenPHP package of [PHP Runtime](https://github.com/php-runtime/runtime): |
| 52 | + |
| 53 | +```console |
| 54 | +composer require runtime/frankenphp-symfony |
| 55 | +``` |
| 56 | + |
| 57 | +Start your app server by defining the `APP_RUNTIME` environment variable to use the FrankenPHP Symfony Runtime: |
| 58 | + |
| 59 | +```console |
| 60 | +docker run \ |
| 61 | + -e FRANKENPHP_CONFIG="worker ./public/index.php" \ |
| 62 | + -e APP_RUNTIME=Runtime\\FrankenPhpSymfony\\Runtime \ |
| 63 | + -v $PWD:/app \ |
| 64 | + -p 80:80 -p 443:443 -p 443:443/udp \ |
| 65 | + dunglas/frankenphp |
| 66 | +``` |
| 67 | + |
| 68 | +Learn more about [the worker mode](worker.md). |
| 69 | + |
| 70 | +## Hot Reload |
| 71 | + |
| 72 | +Hot reloading is enabled by default in [Symfony Docker](https://github.com/dunglas/symfony-docker). |
| 73 | + |
| 74 | +To use the [hot reload](hot-reload.md) feature without Symfony Docker, enable [Mercure](mercure.md) and add the `hot_reload` sub-directive to the `php` directive in your `Caddyfile`: |
| 75 | + |
| 76 | +```caddyfile |
| 77 | +localhost |
| 78 | +
|
| 79 | +mercure { |
| 80 | + anonymous |
| 81 | +} |
| 82 | +
|
| 83 | +root public/ |
| 84 | +
|
| 85 | +@phpRoute { |
| 86 | + not path /.well-known/mercure* |
| 87 | + not file {path} |
| 88 | +} |
| 89 | +rewrite @phpRoute index.php |
| 90 | +
|
| 91 | +@frontController path index.php |
| 92 | +php @frontController { |
| 93 | + hot_reload |
| 94 | + # Optional: enable worker in watch mode for better performance |
| 95 | + worker { |
| 96 | + file ./public/index.php |
| 97 | + watch |
| 98 | + } |
| 99 | +} |
| 100 | +
|
| 101 | +file_server { |
| 102 | + hide *.php |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Then, add the following code to your `templates/base.html.twig` file: |
| 107 | + |
| 108 | +```twig |
| 109 | +{% if app.request.server.has('FRANKENPHP_HOT_RELOAD') %} |
| 110 | + <meta name="frankenphp-hot-reload:url" content="{{ app.request.server.get('FRANKENPHP_HOT_RELOAD') }}"> |
| 111 | + <script src="https://cdn.jsdelivr.net/npm/idiomorph"></script> |
| 112 | + <script src="https://cdn.jsdelivr.net/npm/frankenphp-hot-reload/+esm" type="module"></script> |
| 113 | +{% endif %} |
| 114 | +``` |
| 115 | + |
| 116 | +Finally, run `frankenphp run` from the root directory of your Symfony project. |
| 117 | + |
| 118 | +## Pre-Compressing Assets |
| 119 | + |
| 120 | +Symfony's [AssetMapper component](https://symfony.com/doc/current/frontend/asset_mapper.html) can pre-compress assets with Brotli and Zstandard during deployment. FrankenPHP (through Caddy's `file_server`) can serve these pre-compressed files directly, avoiding on-the-fly compression overhead. |
| 121 | + |
| 122 | +1. Compile and compress your assets: |
| 123 | + |
| 124 | + ```console |
| 125 | + php bin/console asset-map:compile |
| 126 | + ``` |
| 127 | + |
| 128 | +2. Update your `Caddyfile` to serve pre-compressed assets: |
| 129 | + |
| 130 | + ```caddyfile |
| 131 | + { |
| 132 | + frankenphp |
| 133 | + } |
| 134 | +
|
| 135 | + localhost { |
| 136 | + root public/ |
| 137 | +
|
| 138 | + @phpRoute { |
| 139 | + not file {path} |
| 140 | + } |
| 141 | + rewrite @phpRoute index.php |
| 142 | +
|
| 143 | + @frontController path index.php |
| 144 | + php @frontController { |
| 145 | + # Optional: Enable worker mode for better performance |
| 146 | + worker { |
| 147 | + file ./public/index.php |
| 148 | + } |
| 149 | + } |
| 150 | +
|
| 151 | + @assets path /assets/* |
| 152 | + file_server @assets { |
| 153 | + precompressed zstd br gzip |
| 154 | + } |
| 155 | +
|
| 156 | + file_server { |
| 157 | + hide *.php |
| 158 | + } |
| 159 | + } |
| 160 | + ``` |
| 161 | + |
| 162 | +The `precompressed` directive tells Caddy to look for pre-compressed versions of the requested file (e.g., `app.css.zst`, `app.css.br`) and serve them directly if the client supports it. |
| 163 | + |
| 164 | +## Symfony Apps As Standalone Binaries |
| 165 | + |
| 166 | +Using [FrankenPHP's application embedding feature](embed.md), it's possible to distribute Symfony |
| 167 | +apps as standalone binaries. |
| 168 | + |
| 169 | +Follow these steps to prepare and package your Symfony app: |
| 170 | + |
| 171 | +1. Prepare your app: |
| 172 | + |
| 173 | + ```console |
| 174 | + # Export the project to get rid of .git/, etc |
| 175 | + mkdir $TMPDIR/my-prepared-app |
| 176 | + git archive HEAD | tar -x -C $TMPDIR/my-prepared-app |
| 177 | + cd $TMPDIR/my-prepared-app |
| 178 | + |
| 179 | + # Set proper environment variables |
| 180 | + echo APP_ENV=prod > .env.local |
| 181 | + echo APP_DEBUG=0 >> .env.local |
| 182 | + |
| 183 | + # Remove the tests and other unneeded files to save space |
| 184 | + # Alternatively, add these files with the export-ignore attribute in your .gitattributes file |
| 185 | + rm -Rf tests/ |
| 186 | + |
| 187 | + # Install the dependencies |
| 188 | + composer install --ignore-platform-reqs --no-dev -a |
| 189 | + |
| 190 | + # Optimize .env |
| 191 | + composer dump-env prod |
| 192 | + ``` |
| 193 | + |
| 194 | +2. Create a file named `static-build.Dockerfile` in the repository of your app: |
| 195 | + |
| 196 | + ```dockerfile |
| 197 | + FROM --platform=linux/amd64 dunglas/frankenphp:static-builder-gnu |
| 198 | + # If you intend to run the binary on musl-libc systems, use static-builder-musl instead |
| 199 | + |
| 200 | + # Copy your app |
| 201 | + WORKDIR /go/src/app/dist/app |
| 202 | + COPY . . |
| 203 | + |
| 204 | + # Build the static binary |
| 205 | + WORKDIR /go/src/app/ |
| 206 | + RUN EMBED=dist/app/ ./build-static.sh |
| 207 | + ``` |
| 208 | + |
| 209 | + > [!CAUTION] |
| 210 | + > |
| 211 | + > Some `.dockerignore` files (e.g. default [Symfony Docker `.dockerignore`](https://github.com/dunglas/symfony-docker/blob/main/.dockerignore)) |
| 212 | + > will ignore the `vendor/` directory and `.env` files. Be sure to adjust or remove the `.dockerignore` file before the build. |
| 213 | +
|
| 214 | +3. Build: |
| 215 | + |
| 216 | + ```console |
| 217 | + docker build -t static-symfony-app -f static-build.Dockerfile . |
| 218 | + ``` |
| 219 | + |
| 220 | +4. Extract the binary: |
| 221 | + |
| 222 | + ```console |
| 223 | + docker cp $(docker create --name static-symfony-app-tmp static-symfony-app):/go/src/app/dist/frankenphp-linux-x86_64 my-app ; docker rm static-symfony-app-tmp |
| 224 | + ``` |
| 225 | + |
| 226 | +5. Start the server: |
| 227 | + |
| 228 | + ```console |
| 229 | + ./my-app php-server |
| 230 | + ``` |
| 231 | + |
| 232 | +Learn more about the options available and how to build binaries for other OSes in the [applications embedding](embed.md) |
| 233 | +documentation. |
0 commit comments