When running a Laravel application across multiple production servers (load-balanced or distributed architecture), you need to update configuration values consistently across all servers without causing downtime or configuration drift.
The Problem
You have 8 servers running your application:
- 2 web servers (web1, web2)
- 2 API servers (api1, api2)
- 2 queue worker servers (worker1, worker2)
- 2 admin dashboard servers (admin1, admin2)
A third-party service changed their API endpoint from http://api.vendor.com/ to https://platform.vendor.com/, and you need to update the VENDOR_API_URL environment variable across all servers immediately.
The Solution
Use SSH commands combined with sed and grep to update .env files across all servers in one operation, with immediate verification.
Step 1: Check Current Values
for server in web1 web2 api1 api2 worker1 worker2 admin1 admin2; do
echo "=== $server ==="
ssh $server "grep '^VENDOR_API_URL=' /var/www/app/.env"
done
This gives you a baseline and confirms the current value is consistent across all servers.
Step 2: Update All Servers
for server in web1 web2 api1 api2 worker1 worker2 admin1 admin2; do
ssh $server "sudo sed -i 's|^VENDOR_API_URL=\"http://api.vendor.com/\"|VENDOR_API_URL=\"https://platform.vendor.com/\"|' /var/www/app/.env && sudo grep '^VENDOR_API_URL=' /var/www/app/.env"
done
Key techniques:
- Pipe delimiters in sed:
s|old|new|instead ofs/old/new/avoids escaping forward slashes in URLs - Chain with grep:
&&immediately verifies each update - Use sudo: Handle file permission requirements
- In-place edit:
sed -imodifies the file directly
Step 3: Independent Verification
After all updates complete, run a fresh check to confirm consistency:
for server in web1 web2 api1 api2 worker1 worker2 admin1 admin2; do
echo "=== $server ==="
ssh $server "grep '^VENDOR_API_URL=' /var/www/app/.env"
done
All servers should now show the new value. Document the results in your deployment notes.
Important Considerations
Service Restarts Required
.env changes don’t take effect until services restart. Plan your restart strategy:
- PHP-FPM:
sudo systemctl reload php8.2-fpm - Queue Workers:
sudo supervisorctl restart allorphp artisan queue:restart - Laravel Octane:
php artisan octane:reload
Consider rolling restarts to avoid downtime (restart workers first, then web servers one at a time).
Config Cache
If you’re using php artisan config:cache in production, updating .env alone won’t work. You need to rebuild the cache:
for server in web1 web2 api1 api2 worker1 worker2 admin1 admin2; do
ssh $server "cd /var/www/app && php artisan config:clear && php artisan config:cache"
done
Why Not Ansible/Chef/Puppet?
Configuration management tools are great for infrastructure-as-code, but for urgent hotfixes or one-off changes, this SSH approach is:
- Faster – No playbook to write/test/deploy
- Transparent – You see exactly what changed in real-time
- Auditable – Terminal history captures the exact commands
- Simple – Works even if CM tools aren’t set up yet
For routine configuration updates, absolutely use your CM tool. But when you need to update an API endpoint across 8 servers right now, this approach gets the job done safely.
Pro Tips
- Document before changing: Keep a record of old vs new values
- Test on one server first: Verify the sed command works before running across all servers
- Use SSH config: Simplify server names with
~/.ssh/configaliases - Consider tmux: Run parallel SSH sessions to see all updates simultaneously
- Backup first:
cp /var/www/app/.env /var/www/app/.env.backupbefore making changes
Need to update environment variables across your server fleet? This pattern ensures consistency, provides immediate verification, and gives you the confidence that all servers are aligned.
Leave a Reply