-
-
Save Nilpo/e88d9d43623876921854d38851cd37e8 to your computer and use it in GitHub Desktop.
| APP_URL=https://laravel.test | |
| APP_SERVICE=laravel.test | |
| [...] |
| <?php | |
| # config/app.php | |
| [...] | |
| /* | |
| |-------------------------------------------------------------------------- | |
| | Application Service | |
| |-------------------------------------------------------------------------- | |
| | | |
| | The APP_SERVICE environment variable is used when the default docker | |
| | service name is changed from its default 'laravel.test' value. | |
| | Laravel Sail will fail to start if there is a mismatch. | |
| | | |
| */ | |
| 'service' => env('APP_SERVICE', 'laravel.test'), | |
| [...] | |
| ]; |
| # ./Caddyfile | |
| { | |
| on_demand_tls { | |
| ask http://laravel.test/domain-verify | |
| } | |
| local_certs | |
| } | |
| :443 { | |
| tls internal { | |
| on_demand | |
| } | |
| reverse_proxy laravel.test { | |
| header_up Host {host} | |
| header_up X-Real-IP {remote} | |
| header_up X-Forwarded-For {remote} | |
| header_up X-Forwarded-Port {server_port} | |
| header_up X-Forwarded-Proto {scheme} | |
| health_timeout 5s | |
| } | |
| } |
| <?php | |
| # app/Http/Controllers/CaddyProxyController.php | |
| # > php artisan make:controller CaddyProxyController | |
| namespace App\Http\Controllers; | |
| use App\Store; | |
| use Illuminate\Http\Request; | |
| class CaddyProxyController extends Controller | |
| { | |
| public function verifyDomain(Request $request) | |
| { | |
| $authorizedDomains = [ | |
| config('app.service'), // laravel.test | |
| 'localhost', | |
| // Add subdomains here | |
| ]; | |
| if (in_array($request->query('domain'), $authorizedDomains)) { | |
| return response('Domain Authorized'); | |
| } | |
| // Abort if there's no 200 response returned above | |
| abort(503); | |
| } | |
| } |
| # For more information: https://laravel.com/docs/sail | |
| version: '3' | |
| services: | |
| laravel.test | |
| [...] | |
| ports: | |
| # - '${APP_PORT:-80}:80' | |
| - '${VITE_PORT:-5173}:${VITE_PORT:-5173}' | |
| [...] | |
| caddy: | |
| image: caddy:latest | |
| restart: unless-stopped | |
| ports: | |
| - '${APP_PORT:-80}:80' | |
| - '${APP_SECURE_PORT:-443}:443' | |
| volumes: | |
| - './Caddyfile:/etc/caddy/Caddyfile' | |
| - sail-caddy:/data | |
| - sail-caddy:/config | |
| networks: | |
| - sail | |
| [...] | |
| volumes: | |
| [...] | |
| sailcaddy: | |
| driver: local |
| <?php | |
| # routes/web.php | |
| [...] | |
| use App\Http\Controllers\CaddyProxyController; | |
| [...] | |
| Route::get('/domain-verify', [CaddyProxyController::class, 'verifyDomain')]; |
-
To use this method, create a new Laravel 9 project using the online builder.
curl -s https://laravel.build/caddy-laravelsail | bash && cd caddy-laravelsail -
Create a controller to handle the Caddy domain verification route.
php artisan make:controller CaddyProxyController -
Create or edit the listed files as shown above.
-
Install the Laravel dependencies, including Laravel Sail.
docker run --rm --interactive --tty -v $(pwd):/app composer install --ignore-platform-reqs -
Build and run the Docker containers using Laravel Sail.
./vendor/bin/sail up --build --remove-orphans -d -
(Optional) Run database migrations.
./vendor/bin/sail artisan migrate -
(Optional) Install NPM dependencies and run the Vite dev script.
./vendor/bin/sail npm -i && ./vendor/bin/sail npm run dev -
Visit the project URL in the browser.
A fully integrated example can be found at the following repository.
https://mikefallows.com/posts/laravel-sail-vite-ssl-custom-domain/