|
| 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. [Install FrankenPHP](../#getting-started) |
| 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 | +
|
| 18 | + root public/ |
| 19 | + php_server { |
| 20 | + # Optional: Enable worker mode for better performance |
| 21 | + worker ./public/index.php |
| 22 | + } |
| 23 | + ``` |
| 24 | + |
| 25 | + See the [performance documentation](performance.md) for further optimizations. |
| 26 | + |
| 27 | +3. Start FrankenPHP from the root directory of your Symfony project: `frankenphp run` |
| 28 | + |
| 29 | +## Worker Mode |
| 30 | + |
| 31 | +Since Symfony 7.4, FrankenPHP worker mode is natively supported. |
| 32 | + |
| 33 | +For older versions, install the FrankenPHP package of [PHP Runtime](https://github.com/php-runtime/runtime): |
| 34 | + |
| 35 | +```console |
| 36 | +composer require runtime/frankenphp-symfony |
| 37 | +``` |
| 38 | + |
| 39 | +Start your app server by defining the `APP_RUNTIME` environment variable to use the FrankenPHP Symfony Runtime: |
| 40 | + |
| 41 | +```console |
| 42 | +docker run \ |
| 43 | + -e FRANKENPHP_CONFIG="worker ./public/index.php" \ |
| 44 | + -e APP_RUNTIME=Runtime\\FrankenPhpSymfony\\Runtime \ |
| 45 | + -v $PWD:/app \ |
| 46 | + -p 80:80 -p 443:443 -p 443:443/udp \ |
| 47 | + dunglas/frankenphp |
| 48 | +``` |
| 49 | + |
| 50 | +Learn more about [the worker mode](worker.md). |
| 51 | + |
| 52 | +## Hot Reload |
| 53 | + |
| 54 | +Hot reloading is enabled by default in [Symfony Docker](https://github.com/dunglas/symfony-docker). |
| 55 | + |
| 56 | +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_server` directive in your `Caddyfile`: |
| 57 | + |
| 58 | +```caddyfile |
| 59 | +localhost |
| 60 | +
|
| 61 | +mercure { |
| 62 | + anonymous |
| 63 | +} |
| 64 | +
|
| 65 | +root public/ |
| 66 | +php_server { |
| 67 | + hot_reload |
| 68 | + worker ./public/index.php |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +Then, add the following code to your `templates/base.html.twig` file: |
| 73 | + |
| 74 | +```twig |
| 75 | +{% if app.request.server.has('FRANKENPHP_HOT_RELOAD') %} |
| 76 | + <meta name="frankenphp-hot-reload:url" content="{{ app.request.server.get('FRANKENPHP_HOT_RELOAD') }}"> |
| 77 | + <script src="https://cdn.jsdelivr.net/npm/idiomorph"></script> |
| 78 | + <script src="https://cdn.jsdelivr.net/npm/frankenphp-hot-reload/+esm" type="module"></script> |
| 79 | +{% endif %} |
| 80 | +``` |
| 81 | + |
| 82 | +Finally, run `frankenphp run` from the root directory of your Symfony project. |
| 83 | + |
| 84 | +## Pre-Compressing Assets |
| 85 | + |
| 86 | +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. |
| 87 | + |
| 88 | +1. Compile and compress your assets: |
| 89 | + |
| 90 | + ```console |
| 91 | + php bin/console asset-map:compile |
| 92 | + ``` |
| 93 | + |
| 94 | +2. Update your `Caddyfile` to serve pre-compressed assets: |
| 95 | + |
| 96 | + ```caddyfile |
| 97 | + localhost |
| 98 | +
|
| 99 | + @assets path /assets/* |
| 100 | + file_server @assets { |
| 101 | + precompressed zstd br gzip |
| 102 | + } |
| 103 | +
|
| 104 | + root public/ |
| 105 | + php_server { |
| 106 | + worker ./public/index.php |
| 107 | + } |
| 108 | + ``` |
| 109 | + |
| 110 | +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. |
| 111 | + |
| 112 | +## Serving Large Static Files (`X-Sendfile`) |
| 113 | + |
| 114 | +FrankenPHP supports [efficiently serving large static files](x-sendfile.md) after executing PHP code (for access control, statistics, etc.). |
| 115 | + |
| 116 | +Symfony HttpFoundation [natively supports this feature](https://symfony.com/doc/current/components/http_foundation.html#serving-files). |
| 117 | +After [configuring your `Caddyfile`](x-sendfile.md#configuration), it will automatically determine the correct value for the `X-Accel-Redirect` header and add it to the response: |
| 118 | + |
| 119 | +```php |
| 120 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 121 | + |
| 122 | +BinaryFileResponse::trustXSendfileTypeHeader(); |
| 123 | +$response = new BinaryFileResponse(__DIR__.'/../private-files/file.txt'); |
| 124 | + |
| 125 | +// ... |
| 126 | +``` |
| 127 | + |
| 128 | +## Symfony Apps As Standalone Binaries |
| 129 | + |
| 130 | +Using [FrankenPHP's application embedding feature](embed.md), it's possible to distribute Symfony |
| 131 | +apps as standalone binaries. |
| 132 | + |
| 133 | +Follow these steps to prepare and package your Symfony app: |
| 134 | + |
| 135 | +1. Prepare your app: |
| 136 | + |
| 137 | + ```console |
| 138 | + # Export the project to get rid of .git/, etc |
| 139 | + mkdir $TMPDIR/my-prepared-app |
| 140 | + git archive HEAD | tar -x -C $TMPDIR/my-prepared-app |
| 141 | + cd $TMPDIR/my-prepared-app |
| 142 | + |
| 143 | + # Set proper environment variables |
| 144 | + echo APP_ENV=prod > .env.local |
| 145 | + echo APP_DEBUG=0 >> .env.local |
| 146 | + |
| 147 | + # Remove the tests and other unneeded files to save space |
| 148 | + # Alternatively, add these files with the export-ignore attribute in your .gitattributes file |
| 149 | + rm -Rf tests/ |
| 150 | + |
| 151 | + # Install the dependencies |
| 152 | + composer install --ignore-platform-reqs --no-dev -a |
| 153 | + |
| 154 | + # Optimize .env |
| 155 | + composer dump-env prod |
| 156 | + ``` |
| 157 | + |
| 158 | +2. Create a file named `static-build.Dockerfile` in the repository of your app: |
| 159 | + |
| 160 | + ```dockerfile |
| 161 | + FROM --platform=linux/amd64 dunglas/frankenphp:static-builder-gnu |
| 162 | + # If you intend to run the binary on musl-libc systems, use static-builder-musl instead |
| 163 | + |
| 164 | + # Copy your app |
| 165 | + WORKDIR /go/src/app/dist/app |
| 166 | + COPY . . |
| 167 | + |
| 168 | + # Build the static binary |
| 169 | + WORKDIR /go/src/app/ |
| 170 | + RUN EMBED=dist/app/ ./build-static.sh |
| 171 | + ``` |
| 172 | + |
| 173 | + > [!CAUTION] |
| 174 | + > |
| 175 | + > Some `.dockerignore` files (e.g. default [Symfony Docker `.dockerignore`](https://github.com/dunglas/symfony-docker/blob/main/.dockerignore)) |
| 176 | + > will ignore the `vendor/` directory and `.env` files. Be sure to adjust or remove the `.dockerignore` file before the build. |
| 177 | +
|
| 178 | +3. Build: |
| 179 | + |
| 180 | + ```console |
| 181 | + docker build -t static-symfony-app -f static-build.Dockerfile . |
| 182 | + ``` |
| 183 | + |
| 184 | +4. Extract the binary: |
| 185 | + |
| 186 | + ```console |
| 187 | + 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 |
| 188 | + ``` |
| 189 | + |
| 190 | +5. Start the server: |
| 191 | + |
| 192 | + ```console |
| 193 | + ./my-app php-server |
| 194 | + ``` |
| 195 | + |
| 196 | +Learn more about the options available and how to build binaries for other OSes in the [applications embedding](embed.md) |
| 197 | +documentation. |
0 commit comments