Skip to content

Commit

Permalink
docs: embedding Laravel apps
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Apr 27, 2024
1 parent dd5fb4e commit 48bb810
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
4 changes: 3 additions & 1 deletion docs/embed.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Thanks to this feature, PHP applications can be distributed as standalone binari

Learn more about this feature [in the presentation made by Kévin at SymfonyCon 2023](https://dunglas.dev/2023/12/php-and-symfony-apps-as-standalone-binaries/).

For embedding Laravel applications, [read this specific documentation entry](laravel.md#laravel-apps-as-standalone-binaries).

## Preparing Your App

Before creating the self-contained binary be sure that your app is ready for embedding.
Expand Down Expand Up @@ -44,7 +46,7 @@ composer dump-env prod

The easiest way to create a Linux binary is to use the Docker-based builder we provide.

1. Create a file named `static-build.Dockerfile` in the repository of your app (don't forget to commit it):
1. Create a file named `static-build.Dockerfile` in the repository of your app:

```dockerfile
FROM --platform=linux/amd64 dunglas/frankenphp:static-builder
Expand Down
89 changes: 52 additions & 37 deletions docs/laravel.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,56 +76,43 @@ Learn more about [Laravel Octane in its official documentation](https://laravel.

## Laravel Apps As Standalone Binaries

### Preparing Your App
Using [FrankenPHP's application embedding feature](embed.md), it's possible to distribute Laravel
apps as standalone binaries.

1. Create a file named `static-build.Dockerfile` in the repository of your app (don't forget to commit it):
Follow these steps to package your Laravel app as a standalone binary for Linux:

```dockerfile
FROM --platform=linux/amd64 dunglas/frankenphp:static-builder

# Copy your app
WORKDIR /go/src/app/dist/app
COPY . .

# Build the static binary, be sure to select only the PHP extensions you want
WORKDIR /go/src/app/
RUN EMBED=dist/app/ ./build-static.sh
```

> [!CAUTION]
>
> Some `.dockerignore` files
> will ignore the `vendor/` directory and `.env` files. Be sure to adjust or remove the `.dockerignore` file before the build.
1. Create a file named `static-build.Dockerfile` in the repository of your app:

1. Prepare your app for production:
```dockerfile
FROM --platform=linux/amd64 dunglas/frankenphp:static-builder

```console
# Export the project to get rid of .git/, etc
mkdir $TMPDIR/my-laravel-app
git archive HEAD | tar -x -C $TMPDIR/my-laravel-app
cd $TMPDIR/my-laravel-app
# Copy your app
WORKDIR /go/src/app/dist/app
COPY . .

# Remove the tests and other unneeded files to save space
# Alternatively, add these files with the export-ignore attribute in your .gitattributes file
rm -Rf tests/
# Alternatively, add these files to a .dockerignore file
RUN rm -Rf tests/

# Copy .env file
cp .env.example .env
RUN cp .env.example .env
# Change APP_ENV and APP_DEBUG to be production ready
sed -i '' -e 's/^APP_ENV=.*/APP_ENV=production/' -e 's/^APP_DEBUG=.*/APP_DEBUG=false/' .env
# If needed, edit .env to change other environment variables (don't store sensitive values in this file, use env:encrypt if necessary)
RUN sed -i'' -e 's/^APP_ENV=.*/APP_ENV=production/' -e 's/^APP_DEBUG=.*/APP_DEBUG=false/' .env

# Install the dependencies
composer install --ignore-platform-reqs --no-dev -a
# Make other changes to your .env file if needed

# Prepare the DB
php artisan migrate
# Install the dependencies
RUN composer install --ignore-platform-reqs --no-dev -a

# Warm the caches
php artisan optimize
```
# Build the static binary
WORKDIR /go/src/app/
RUN EMBED=dist/app/ ./build-static.sh
```

2.
> [!CAUTION]
>
> Some `.dockerignore` files
> will ignore the `vendor/` directory and `.env` files. Be sure to adjust or remove the `.dockerignore` file before the build.

2. Build:

Expand All @@ -139,3 +126,31 @@ Learn more about [Laravel Octane in its official documentation](https://laravel.
docker cp $(docker create --name static-laravel-app-tmp static-laravel-app):/go/src/app/dist/frankenphp-linux-x86_64 my-laravel-app ; docker rm static-laravel-app-tmp
```

4. Populate caches:

```console
./my-laravel-app php-cli artisan optimize
```

5. Run database migrations (if any):

```console
./my-laravel-app php-cli artisan migrate
````

6. Generate app's secret key
```console
./my-laravel-app php-cli artisan key:generate
```
5. Start the server:
```console
./app/my-laravel-app php-server
```
Your app is now ready!
Learn more about the options available and how to build binaries for other OSes in the [applications embedding](embed.md)
documentation.

0 comments on commit 48bb810

Please sign in to comment.