> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Deployment

> Gids voor configuratie, optimalisatie en beheer bij het deployen van een Laravel-applicatie naar productie

## Aan de slag

Wanneer je een Laravel-applicatie naar een productieomgeving deployt, moet je die zo voorbereiden dat hij zo efficiënt mogelijk draait.
Deze gids behandelt de belangrijkste punten om je productiedeploy betrouwbaar uit te voeren.

## Deployflow

```mermaid theme={null}
flowchart TD
    A["Code pushen"] --> B["php artisan optimize"]
    B --> C["Serveren met Nginx / FrankenPHP"]
    C --> D["php artisan reload"]
    D --> E["Queue-workers<br>Reverb / Octane herstarten"]
    E --> F["Deploy voltooid"]
```

## Serververeisten

Het Laravel-framework heeft de volgende systeemvereisten.
Je hebt **PHP 8.3 of hoger** nodig, plus de onderstaande PHP-extensies.

| Extensie  | Beschrijving                     |
| --------- | -------------------------------- |
| Ctype     | Controle van tekentypen          |
| cURL      | HTTP-communicatie                |
| DOM       | DOM-manipulatie van XML/HTML     |
| Fileinfo  | MIME-type-detectie               |
| Filter    | Filteren van data                |
| Hash      | Hashfuncties                     |
| Mbstring  | Verwerking van multibyte-strings |
| OpenSSL   | Versleuteling                    |
| PCRE      | Reguliere expressies             |
| PDO       | Databaseverbindingen             |
| Session   | Sessiebeheer                     |
| Tokenizer | PHP-tokenanalyse                 |
| XML       | XML-verwerking                   |

## Serverconfiguratie

### Nginx

Als je Nginx gebruikt, neem dan het volgende configuratiebestand als basis.
Het is belangrijk dat **alle requests worden doorgestuurd naar `public/index.php`**.
Verplaats `index.php` niet naar de projectroot: vertrouwelijke configuratiebestanden zouden dan publiek toegankelijk worden.

```nginx theme={null}
server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    root /srv/example.com/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffer_size 32k;
        fastcgi_buffers 8 32k;
        fastcgi_busy_buffers_size 64k;
        fastcgi_hide_header X-Powered-By;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

### FrankenPHP

[FrankenPHP](https://frankenphp.dev/) is een moderne PHP-applicatieserver geschreven in Go.
Met alleen het volgende commando start je een Laravel-applicatie:

```shell theme={null}
frankenphp php-server -r public/
```

Voor geavanceerde functies zoals HTTP/3, moderne compressie, integratie met [Laravel Octane](https://laravel.com/docs/octane) en standalone binaries, zie de [Laravel-documentatie van FrankenPHP](https://frankenphp.dev/docs/laravel/).

### Maprechten

Laravel moet kunnen schrijven naar de mappen `bootstrap/cache` en `storage`.
Stel de rechten zo in dat de proceseigenaar van de webserver naar deze mappen kan schrijven.

```shell theme={null}
chmod -R 775 storage bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache
```

## Optimalisatie

Bij het deployen naar productie verbeter je de prestaties door configuratie, events, routes en views te cachen.
Met het commando `optimize` cache je alles in één keer.

```shell theme={null}
php artisan optimize
```

Gebruik `optimize:clear` om de cache te verwijderen.

```shell theme={null}
php artisan optimize:clear
```

### Individuele optimalisatiecommando's

`optimize` voert de volgende commando's gebundeld uit. Je kunt ze indien nodig ook afzonderlijk uitvoeren.

| Commando                   | Beschrijving                                                  |
| -------------------------- | ------------------------------------------------------------- |
| `php artisan config:cache` | Bundelt de configuratiebestanden in één bestand en cachet dit |
| `php artisan event:cache`  | Cachet de mapping van events naar listeners                   |
| `php artisan route:cache`  | Cachet de routedefinities en versnelt de routeregistratie     |
| `php artisan view:cache`   | Precompileert Blade-views en versnelt requests                |

<Info>
  Nadat je `config:cache` hebt uitgevoerd, mag je de `env()`-functie alleen nog binnen configuratiebestanden aanroepen.
  Na het cachen wordt het `.env`-bestand niet meer geladen; als je `env()` buiten configuratiebestanden aanroept, krijg je `null` terug.
</Info>

## Services herladen

Na het deployen van een nieuwe versie moeten langlopende services zoals queue-workers, Laravel Reverb en Laravel Octane opnieuw worden gestart om de nieuwe code te gebruiken.

```shell theme={null}
php artisan reload
```

Dit commando beëindigt de herlaadbare services.
Zorg dat een procesmonitor (zoals Supervisor) ze automatisch opnieuw opstart.

<Info>
  Als je Laravel Cloud gebruikt, wordt het gracieus herladen van alle services automatisch afgehandeld en heb je het `reload`-commando niet nodig.
</Info>

## Debugmodus

De `debug`-optie in `config/app.php` bepaalt hoeveel foutinformatie aan gebruikers wordt getoond.
Standaard wordt de waarde van de omgevingsvariabele `APP_DEBUG` uit het `.env`-bestand gebruikt.

<Warning>
  **Zet `APP_DEBUG` in productie altijd op `false`.**
  Als je met `APP_DEBUG=true` in productie draait, loop je het risico dat vertrouwelijke configuratiewaarden zoals databaseverbindingsgegevens en geheime sleutels aan eindgebruikers worden blootgesteld.
</Warning>

```ini theme={null}
# .env (productie)
APP_DEBUG=false
```

## Healthcheck-route

Laravel heeft een ingebouwde healthcheck-route om de status van je applicatie te monitoren.
Je kunt deze koppelen aan uptime-monitors, loadbalancers en orkestratiesystemen zoals Kubernetes.

Standaard is er een `/up`-endpoint dat `200` teruggeeft als de applicatie correct is opgestart, en `500` als er tijdens het opstarten een exception is opgetreden.

Je kunt de URI aanpassen in `bootstrap/app.php`:

```php theme={null}
->withRouting(
    web: __DIR__.'/../routes/web.php',
    commands: __DIR__.'/../routes/console.php',
    health: '/status', // standaard is /up
)
```

Bij een request naar deze route wordt het event `Illuminate\Foundation\Events\DiagnosingHealth` afgevuurd,
zodat je in een listener aanvullende controles op de database of cache kunt implementeren.

## Deployen met Laravel Cloud of Forge

### Laravel Cloud

Zoek je een volledig managed, automatisch schalend deployplatform, dan is [Laravel Cloud](https://cloud.laravel.com) een aanrader.
Het is een voor Laravel geoptimaliseerd PaaS dat managed compute, databases, caches en objectopslag biedt.

Het wordt rechtstreeks door het Laravel-ontwikkelteam getuned en werkt naadloos samen met het framework.

### Laravel Forge

Wil je zelf je servers beheren, maar geen tijd steken in het opzetten van services zoals Nginx en MySQL, dan is [Laravel Forge](https://forge.laravel.com) handig.

Het maakt servers aan bij grote cloudproviders zoals DigitalOcean, Linode en AWS, en installeert en beheert automatisch tools zoals Nginx, MySQL, Redis, Memcached en Beanstalk.

## Volgende stappen

<Card title="Deployment — officiële documentatie" icon="arrow-right" href="https://laravel.com/docs/deployment">
  In de officiële documentatie vind je de details van de nieuwste deployconfiguratie.
</Card>


## Related topics

- [Introductie tot Laravel Nightwatch](/nl/blog/nightwatch-introduction.md)
- [Consoletests](/nl/console-tests.md)
- [Mocking](/nl/mocking.md)
- [Databasetests](/nl/database-testing.md)
- [HTTP-tests](/nl/http-tests.md)
