Home » Php » symfony – When running a project without a .env file, bootstrap.php throws a fatal error when the DotEnv component attempts to read it

symfony – When running a project without a .env file, bootstrap.php throws a fatal error when the DotEnv component attempts to read it

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have tried to deploy a Symfony application by using the following tutorial: https://medium.com/@runawaycoin/deploying-symfony-4-application-to-shared-hosting-with-just-ftp-access-e65d2c5e0e3d

When trying to load the site I got a 500 internal server error. As per the tutorial I looked into my hosting error logs and saw a lot of stuff about DotEnv. Here’s the first part of the output:

PHP Fatal error: Uncaught
Symfony\Component\Dotenv\Exception\PathException: Unable to read the
“/var/www/vhosts/137/332002/webspace/httpdocs/symfony_temp/.env”
environment file. in
/var/www/vhosts/137/332002/webspace/httpdocs/symfony_temp/vendor/symfony/dotenv/Dotenv.php:484

The tutorial tells me to store my env variables in other places like index.php for example. But looking at the code I see that bootstrap.php immediately calls DotEnv and tries to get data from there, which results in an error eventually I think.

How can I disable DotEnv, so that Symfony doesn’t use it, and I can simply get my env variables from somewhere else? It is hosted in a hosting environment belonging to a customer where I think .htaccess doesn’t allow access to DotEnv and I can’t change that in this case as I have no authorization to edit that .htaccess file or even open it.

How to&Answers:

bootstrap.php belongs to you. If you do not like its behaviour, just change it.

If you do not want to use DotEnv, just remove it and remove whatever references to it in your code (e.g. the aforementioned bootstrap.php).

Mind you, hardcoding configuration variables in code like index.php sounds like a bad idea. Even if you are not using environment variables, DotEnv will help you centralize your configuration data in a single file not related to your application logic.

For best performance, configure your environment variables for production settings while on dev and use composer dump-env prod as mentioned here. This will generate a .env.local.php that you can upload to production, and for the default bootstrap.php will prevent the use of DotEnv.