Running Multiple Laravel Nightwatch Agents on One Server with Forge
Laravel Nightwatch just launched, and it's already become an essential observability tool for my Laravel apps. But when running multiple Laravel projects on the same server, I ran into an issue:
❌ RuntimeException: Failed to listen on "tcp://127.0.0.1:2407": Address already in use
The solution? Use custom ports for each Nightwatch agent by leveraging the new --listen-on flag and NIGHTWATCH_INGEST_URI.
Step-by-Step Fix
Each site on my server needs its own Nightwatch agent. Laravel Nightwatch allows this with custom ports. Here’s how I set it up for multiple Forge-managed sites on the same box:
1. Update .env in each site
Site 1 (site1.com)
NIGHTWATCH_INGEST_URI=127.0.0.1:2407
Site 2 (site2.com)
NIGHTWATCH_INGEST_URI=127.0.0.1:2408
Site 3 (site3.com)
NIGHTWATCH_INGEST_URI=127.0.0.1:2409
2. Start the agent using a unique port per site
Use Laravel Forge’s Daemons tab for each site and define: (change php8.x for your version)
Site 1
php8.3 artisan nightwatch:agent --listen-on=127.0.0.1:2407
Site 2
php8.3 artisan nightwatch:agent --listen-on=127.0.0.1:2408
Site 3
php8.3 artisan nightwatch:agent --listen-on=127.0.0.1:2409
The Result
Now all Nightwatch agents run smoothly on the same server, each on its own port, without any conflicts. Laravel Forge keeps them alive as background daemons, and I get centralized observability across all my Laravel apps. I love it! I'll be using this on all my sites.