# Deployment
- [Introduction](#introduction)
- [Server Requirements](#server-requirements)
- [Hypervel PHP Requirements](#hypervel-php-requirements)
- [Server Configuration](#server-configuration)
- [Nginx](#nginx)
- [Nginx and WebSockets](#nginx-and-websockets)
- [Running the Hypervel Server](#running-the-hypervel-server)
- [HTTP/2 Stream Cancellation](#http2-stream-cancellation)
- [Graceful Shutdown](#graceful-shutdown)
- [Directory Permissions](#directory-permissions)
- [Optimization](#optimization)
- [Caching Configuration](#optimizing-configuration-loading)
- [Caching Events](#caching-events)
- [Caching Routes](#optimizing-route-loading)
- [Caching Views](#optimizing-view-loading)
- [Autoloader Optimization](#autoloader-optimization)
- [Reloading Services](#reloading-services)
- [Debug Mode](#debug-mode)
- [The Health Route](#the-health-route)
- [Deploying With SonicStack](#deploying-with-sonicstack)
## Introduction
When you're ready to deploy your Hypervel application to production, there are some important things you can do to make sure your application is running as efficiently as possible. In this document, we'll cover some great starting points for making sure your Hypervel application is deployed properly.
## Server Requirements
Hypervel ships with its own Swoole-based application server, so your production server should run the long-running Hypervel server process instead of PHP-FPM.
### Hypervel PHP Requirements
Your server must meet Hypervel's [installation requirements](/docs/{{version}}/installation#requirements), including the PHP version, the required PHP extensions, Swoole's build options, and the `swoole.use_shortname` setting. You can check the PHP version installed on your server with:
```shell
php -v
```
## Server Configuration
Hypervel supports direct TLS termination, but for internet-facing production deployments, we recommend terminating TLS at a dedicated reverse proxy or load balancer.
### Nginx
If you are deploying your application to a server that is running Nginx, you may use the following configuration file as a starting point for configuring your web server. Most likely, this file will need to be customized depending on your server's configuration. **If you would like assistance in managing your server, consider using a fully-managed Hypervel platform like [SonicStack](https://sonicstack.io).**
Please ensure, like the configuration below, your web server proxies dynamic requests to your Hypervel server. The default Hypervel HTTP server listens on port `8000` and may be configured in your application's `config/server.php` file:
```nginx
server {
listen 80;
listen [::]:80;
server_name example.com;
root /srv/example.com/public;
location / {
try_files $uri @hypervel;
}
location @hypervel {
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffer_size 32k;
proxy_buffers 8 32k;
proxy_busy_buffers_size 64k;
proxy_pass http://127.0.0.1:8000;
}
location ~ \.php$ {
return 404;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
```
When using a web server such as Nginx, you should let the web server serve your application's static files directly and only proxy dynamic requests to Hypervel. This is more efficient than serving static files through PHP. Hypervel also includes a built-in static file handler for direct-to-client deployments; when using a web server to serve static files as shown above, you may disable it by setting the `SERVER_STATIC_FILE_HANDLER` environment variable to `false`.
### Nginx and WebSockets
If you are proxying WebSocket connections, your Nginx configuration should include the WebSocket upgrade headers. For example, Hypervel Reverb listens on port `8080` by default:
```nginx
server {
listen 80;
listen [::]:80;
server_name websocket.example.com;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 60s;
proxy_pass http://127.0.0.1:8080;
}
}
```
### Running the Hypervel Server
In production, your Hypervel server should be kept running by a process monitor, container orchestrator, or managed platform. The server may be started using the `serve` Artisan command:
```shell
php artisan serve
```
By default, the HTTP server binds to `0.0.0.0:8000` with HTTP/2 enabled. You may configure the server host, port, worker count, max requests per worker, graceful shutdown allowance, HTTP/2 support, and other Swoole settings using the `SERVER_HOST`, `SERVER_PORT`, `SERVER_WORKERS`, `SERVER_MAX_REQUESTS`, `SERVER_MAX_WAIT_TIME`, and `SERVER_HTTP2` environment variables read by `config/server.php`.
Swoole's `event_object` setting is not supported because Hypervel dispatches its own lifecycle event objects from the native server callbacks. Leave this setting disabled and use Hypervel's lifecycle events when integrating with server activity.
The `serve` command also accepts `--host` and `--port` options for overriding the HTTP server address for the current process. In production, prefer durable configuration in `config/server.php` and your environment.
### HTTP/2 Stream Cancellation
When a client cancels a request by resetting its HTTP/2 stream, Hypervel can stop handling that request. Other streams on the same connection continue. This behavior is off by default, and most web applications should leave it off. Cancellation can interrupt a handler waiting for a database query or an HTTP response. For example, your application might save an order and then stop before sending the confirmation email.
For a gRPC or streaming service, you may want to stop producing a response as soon as the client resets its stream. If all your HTTP/2 handlers can safely stop this way, set the following environment variable and restart the server.
```dotenv
SERVER_HTTP2_CANCEL_ON_STREAM_RESET=true
```
The default `config/server.php` reads this variable in its global `settings`. Adding the option to an individual listener's settings, including the gRPC listener's, has no effect. Clients must reach Hypervel over HTTP/2, either directly or through a proxy that forwards stream resets, such as Envoy. An Nginx `proxy_pass` connection using HTTP/1.1 cannot forward them.
> [!NOTE]
> This option requires a Swoole build with [HTTP/2 stream reset cancellation](https://github.com/swoole/swoole-src/pull/6436), such as a recent nightly. The tagged 6.3.0RC2 release predates it.
### Graceful Shutdown
Sending `SIGTERM` or `SIGINT` to the server's main process starts a graceful shutdown. `Ctrl+C` sends `SIGINT` to the entire terminal process group and can interrupt active requests. To allow requests to finish, signal the main process only.
The `SERVER_MAX_WAIT_TIME` environment variable controls Swoole's server-wide graceful shutdown allowance in seconds. It defaults to `3`. Increase this value when long-running requests, WebSocket connections, or server-process cleanup need more time to finish. Swoole may forcefully terminate work that exceeds the configured allowance.
A value of `0` does not provide unlimited shutdown time. Workers receive no graceful drain period, while Swoole's final timeout for custom server processes is disabled.
### Directory Permissions
Hypervel will need to write to the `bootstrap/cache` and `storage` directories, so you should ensure the user running the Hypervel server has permission to write to these directories.
## Optimization
When deploying your application to production, there are a variety of files that should be cached, including your configuration, events, routes, and views. Hypervel provides a single, convenient `optimize` Artisan command that will cache all of these files. This command should typically be invoked as part of your application's deployment process:
```shell
php artisan optimize
```
The `optimize:clear` command may be used to remove all of the cache files generated by the `optimize` command as well as all keys in the default cache driver:
```shell
php artisan optimize:clear
```
In the following documentation, we will discuss each of the granular optimization commands that are executed by the `optimize` command.
### Caching Configuration
When deploying your application to production, you should make sure that you run the `config:cache` Artisan command during your deployment process:
```shell
php artisan config:cache
```
This command will combine all of Hypervel's configuration files into a single, cached file, which greatly reduces the number of trips the framework must make to the filesystem when loading your configuration values.
> [!WARNING]
> If you execute the `config:cache` command during your deployment process, you should be sure that you are only calling the `env` function from within your configuration files. Once the configuration has been cached, the `.env` file will not be loaded and all calls to the `env` function for `.env` variables will return `null`.
### Caching Events
You should cache your application's auto-discovered event to listener mappings during your deployment process. This can be accomplished by invoking the `event:cache` Artisan command during deployment:
```shell
php artisan event:cache
```
### Caching Routes
If you are building a large application with many routes, you should make sure that you are running the `route:cache` Artisan command during your deployment process:
```shell
php artisan route:cache
```
This command reduces all of your route registrations into a single method call within a cached file, improving the performance of route registration when registering hundreds of routes.
### Caching Views
When deploying your application to production, you should make sure that you run the `view:cache` Artisan command during your deployment process:
```shell
php artisan view:cache
```
This command precompiles all your Blade views so they are not compiled on demand, improving the performance of each request that returns a view.
### Autoloader Optimization
When deploying to production, install your dependencies with an authoritative Composer class map:
```shell
composer install --no-dev --classmap-authoritative
```
Composer will then load classes only from its generated class map. This avoids filesystem searches for missing classes and stops long-running workers from keeping a record of every missing class name they look up, including names taken from request data. Classes added after the map is built can't be loaded until it is rebuilt. Hypervel's generated AOP proxies and class map overrides use their own loaders and are not affected. Leave this option off during local development so new classes load without rebuilding the map.
To check this during deployment, install [Doctor](/docs/{{version}}/doctor#installation) without `--dev`, run the following command against the built release in its production environment, and stop the deployment if it fails:
```shell
php artisan doctor --only=ComposerAutoloadIsAuthoritative --no-interaction
```
The check is skipped when Doctor runs in a [local environment](/docs/{{version}}/doctor#environment-modes).
## Reloading Services
> [!NOTE]
> When deploying to [SonicStack](https://sonicstack.io), it is not necessary to use the `reload` command, as gracefully reloading all services is handled automatically.
Hypervel provides a `reload` Artisan command that replaces the server's event and task workers, signals queue workers to restart, and interrupts the scheduler:
```shell
php artisan reload
```
If you are not using [SonicStack](https://sonicstack.io), you should configure a process monitor to restart services such as queue workers when they exit.
To reload only the Hypervel server, you may use the `server:reload` command:
```shell
php artisan server:reload
```
This command gracefully replaces the server's event workers and any configured task workers. It does not rebuild the server application's booted service providers, container services, routes, listeners, or other application state inherited from the master process.
Before each replacement worker starts, Hypervel re-reads the environment and rebuilds the configuration repository. However, services that already copied configuration into their own state retain their inherited values.
Therefore, you should restart the full server process when deploying new code or changing environment variables, configuration, certificates, routes, listeners, package registration, server settings, or other boot-time application state. Use `server:reload` only when you intend to recycle workers without changing the application they inherited.
Cache and Rate Limiter Swoole table definitions also require a full restart because shared tables can only be created before workers are forked. If a newly configured table is used after a reload, Hypervel reports an error instead of creating a separate table inside one worker.
The command will fail if the configured PID file cannot be read, does not contain a valid process ID, or a reload signal cannot be delivered.
If the reloaded configuration is invalid, replacement workers fail before accepting work and the server logs each worker error. Swoole immediately starts another replacement, so the failures continue until you correct the configuration. Once corrected, the next worker starts normally; you do not need to run `server:reload` again.
The server reload does not restart queue workers, the scheduler, Horizon, or custom server processes. Use the `reload` command for queue workers and the scheduler. Restart Horizon and custom server processes separately.
## Debug Mode
The debug option in your `config/app.php` configuration file determines how much information about an error is actually displayed to the user. By default, this option is set to respect the value of the `APP_DEBUG` environment variable, which is stored in your application's `.env` file.
> [!WARNING]
> **In your production environment, this value should always be `false`. If the `APP_DEBUG` variable is set to `true` in production, you risk exposing sensitive configuration values to your application's end users.**
## The Health Route
Hypervel includes a built-in health check route that can be used to monitor the status of your application. In production, this route may be used to report the status of your application to an uptime monitor, load balancer, or orchestration system such as Kubernetes.
When your application's `bootstrap/app.php` file enables the health route, it is typically served at `/up`, which will return a 200 HTTP response if the application has booted without exceptions. Otherwise, a 500 HTTP response will be returned. You may configure the URI for this route in your application's `bootstrap/app.php` file:
```php
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up', // [tl! remove]
health: '/status', // [tl! add]
)
```
When HTTP requests are made to this route, Hypervel will also dispatch a `Hypervel\Foundation\Events\DiagnosingHealth` event, allowing you to perform additional health checks relevant to your application. Within a [listener](/docs/{{version}}/events) for this event, you may check your application's database or cache status. If you detect a problem with your application, you may simply throw an exception from the listener.
When the request expects JSON, the health route returns a JSON object containing a `status` value of `up` or `down`. The response uses the same 200 or 500 HTTP status code as the HTML response:
```json
{"status":"up"}
```
## Deploying With SonicStack
If you would like a fully-managed, auto-scaling deployment platform tuned for Hypervel, check out [SonicStack](https://sonicstack.io). SonicStack is a robust deployment platform for Hypervel applications, offering managed compute, databases, caches, and object storage.
Launch your Hypervel application on SonicStack and fall in love with the scalable simplicity. Because SonicStack is built and maintained by the Hypervel team, it works seamlessly with the framework and directly supports the framework's ongoing development.