Reloading .env variables without restarting server (Laravel 5, shared hosting)

Reloading .env variables without restarting server (Laravel 5, shared hosting)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Reloading .env Variables Without Restarting on Shared Hosting

When a Laravel application transitions from a development environment to a production server, particularly on shared hosting platforms like Dreamhost, you often encounter a specific friction point: the need to restart the entire PHP-FPM or web server process to apply changes made in your .env file. This is especially true if the configuration change involves database credentials. A classic example of this failure mode is the SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known error, which typically indicates that the application cannot resolve the database host because the .env variables were updated but never reloaded by the running process.

On a VPS or local machine where you control the server stack, restarting php-fpm or Apache is trivial. However, shared hosting environments impose strict constraints. You do not have access to the underlying OS processes to signal them for a reload, and many hosts explicitly forbid restarting web services via SSH. Consequently, the standard workflow of "edit .env, save, restart server" becomes impossible.

To resolve this on restricted servers, you must bypass the need for a process restart by forcing Laravel to re-read its configuration files during the application lifecycle. The core mechanism relies on the fact that Laravel caches configuration and compiled service providers in the bootstrap/cache directory. If these cached files are stale, they hold onto old environment variable values regardless of what is currently written to .env.

The immediate fix involves clearing the cache directories before accessing the database. While this does not technically "restart" the server process, it invalidates the configuration cache, forcing the application to re-parse the .env file on the next request. Execute the following command in your project directory:

php artisan config:clear
php artisan cache:clear
php artisan view:clear

Running config:clear removes the compiled config.php file generated by artisan config:cache. By default, Laravel does not cache configuration on every request unless explicitly configured to do so, but in shared hosting scenarios, manual caching or framework behavior might lead to stale reads. More importantly, clearing the cache ensures that no serialized objects or cached service bindings hold references to closed database connections or old hostnames.

After executing these commands, refresh your application page. The next HTTP request will trigger a fresh bootstrapping sequence where Laravel reads the updated .env file, resolves the new database hostname, and establishes a connection without requiring a server restart.

Trade-offs and Limitations

While clearing the cache is an effective workaround for shared hosting, it introduces specific behavioral changes you should be aware of. The most significant side effect is that php artisan serve or any command that relies on cached configuration will behave differently after a cache clear. You must ensure that your deployment script or manual update process always includes these three commands whenever .env files are modified.

Furthermore, this approach only works if the web server has not cached the initial request in a way that bypasses the bootstrap sequence (which is rare but possible with aggressive opcode caching like APCu or Redis configured incorrectly). If you encounter persistent connection issues immediately after clearing the cache, verify that your database credentials are syntactically correct and that the hostnames resolve correctly via a terminal test:

ping your-database-hostname

If the ping fails, the issue is network-level, not Laravel configuration. If it succeeds but the SQL error persists, double-check that you haven't accidentally cached a specific database connection class in a way that retains the old state. In some rare cases involving long-running processes on shared hosting, clearing the cache might require two consecutive requests to fully propagate the new configuration to all active connections.

This method preserves your deployment workflow on restricted servers without needing root access or server restart permissions. It is the standard pattern for managing environment variables in production Laravel apps hosted on platforms like Dreamhost, Linode managed instances, or any shared environment where process management is outsourced to the host provider.

Our network