Speeding Up Laravel Tests with tmpfs (MySQL in RAM)
If your Laravel test suite is painfully slow and does a lot of database work (multi-tenancy, migrations, seeding), the bottleneck is almost…
Laravel Sail on Windows/WSL2 works great until you need to expose your site to the internet. You'll need this for testing webhooks, showing clients your work, or testing on different devices. The built-in sail share command and expose.dev both fail consistently on Windows/WSL2. This guide shows you how to use Ngrok instead, which actually works.
sail shareTwo reasons sail share sucks on Windows/WSL2:
sail share doesn't handle HTTPS termination properly. It gives you HTTP URLs, which are useless for testing modern webhooks that require HTTPS.I tried using the expose.dev client directly:
Ngrok solves all these problems:
There's only one problem to fix. Ngrok serves your site over HTTPS, but your Sail app inside Docker thinks it's on HTTP (port 80). This makes Laravel generate asset URLs with http://, causing browsers to block assets due to "mixed content" errors.
Here's how to fix it with a simple middleware:
1. Create the Middleware:
Create a file at app/Http/Middleware/UpgradeToHttpsUnderNgrok.php:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use Symfony\Component\HttpFoundation\Response;
class UpgradeToHttpsUnderNgrok
{
public function handle(Request $request, Closure $next): Response
{
// Check if the request is coming through a known ngrok domain suffix
// Added .ngrok.io as another common suffix besides the free tier one
if (str_ends_with($request->getHost(), '.ngrok-free.app') || str_ends_with($request->getHost(), '.ngrok.io')) {
URL::forceScheme('https');
}
return $next($request);
}
}
2. Register the Middleware:
Add the middleware to your web group in bootstrap/app.php:
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
// ... other configurations ...
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
// ... other web middleware ...
\App\Http\Middleware\UpgradeToHttpsUnderNgrok::class, // Add this line
]);
// ... other middleware groups ...
})
// ... exceptions, etc ...
->create();
Sign up: Go to ngrok.com and create an account.
Install it: Open your WSL2 terminal and follow the Linux setup steps from https://dashboard.ngrok.com/get-started/setup/linux. You'll download the agent and add your auth token.
Run it: Start your Laravel Sail app (sail up -d), then run:
# Sail uses port 80 by default
ngrok http 80
Use your URL: Ngrok gives you a URL like https://<random-string>.ngrok-free.app. Use this for webhooks, client demos, or whatever you need.
Add the middleware: Make sure you've added the UpgradeToHttpsUnderNgrok middleware shown above to fix the mixed content errors.
If you're using Laravel Sail on Windows/WSL2 and need to share your site, skip the headaches with sail share and expose.dev. Just use Ngrok. It takes minutes to set up, gives you proper HTTPS URLs, and with the simple middleware fix above, it works perfectly with Sail. The free tier is more than enough for most development needs.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com