Reloading .env variables without restarting server (Laravel 5, shared hosting)
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Introduction: Often in Laravel development, you may encounter a situation where your application is running smoothly until the database configuration changes. This commonly results in errors like the one described in the query and requires restarting the server to reload environment variables. However, this can be problematic if using shared hosting services, as they frequently do not allow access to restart server functionality.
Method 1: Use Artisan Commands
Prerequisite: Ensure you have an SSH connection or terminal access on your shared hosting account. Artisan commands are available by running php artisan within your Laravel project.
artisan config:clearclears the cache, which will force the application to reload environment variables from .env file during its next execution. This method is faster and more efficient than restarting the server without affecting your Laravel project's state.artisan config:cachecan be used after updating the .env file to recompile the configuration cache with the latest changes applied. This ensures that your application has access to the new environment variables you have configured.
Method 2: Directly Manipulate Environment Variables
Prerequisite: Ensure you have an SSH connection or terminal access on your shared hosting account. You will also need to be familiar with the basics of shell scripting and how to edit files using commands like nano.
cdinto your Laravel project's root directory, then runnano .envto open the .env file in a text editor.- Edit the required environment variables within the file and save the changes. You may also want to comment out or delete any previous configurations that are not relevant or could cause conflicts with your new settings.
- Save the updated file, then run
cdback into your project directory and restart the artisan server usingphp artisan serve. This will ensure that your Laravel application runs as expected based on the newly configured environment variables.
Conclusion:
In conclusion, there are two effective methods for reloading .env variables without restarting the server when using Laravel 5. The first method utilizes Artisan commands to clear the cache and recompile it with updated environment variables. Alternatively, directly editing the .env file can achieve this goal as well, especially in situations where Artisan is not available or doesn't provide the flexibility you need.
Remember that regardless of the approach used, always ensure your .env file is properly configured and secure. This will not only prevent potential issues but also contribute to a smooth development process for Laravel applications on shared hosting servers.
Note: If you need assistance or expert advice related to Laravel development or shared hosting, feel free to visit Laravel Company's resources and get in touch with our team of experienced developers.