Home > Back-end >  Run Symfony without dotenv?
Run Symfony without dotenv?

Time:08-25

I'm deploying my first app with symfony. I store the enviroment var inside nginx config, like this:

fastcgi_param APP_ENV prod;
fastcgi_param APP_SECRET my_secret;
fastcgi_param DATABASE_URL "my url;

And i have an .env.dist (with default values) file in the project root directory.

Now my question is: do i really need to install symfony/dotenv package in production? And do i really need the .env file?

It's 2 day that i try different configurations but every time i get this error if i don't install dotenv in prod

FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class "Symfony\Component\Dotenv\Dotenv" not found in /srv/nginx/my_project_name/htdocs/public/index.php:12

Or this error if i don't have an .env file

FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Symfony\Component\Dotenv\Exception\PathException: Unable to read the "/srv/nginx/my_project_name/htdocs/.env" environment file. in /srv/nginx/my_project_name/htdocs/vendor/symfony/dotenv/Dotenv.php:557

Another strange thing is that if i modify the .env.dist file with "APP_ENV=dev" but the nginx config remains "fastcgi_param APP_ENV prod;" the app stop working.

EDIT I moved symfony/dotenv package under the "require-dev" section in composer.json

As per @Mcsy suggestion i commented this line in public/index.php

// (new Dotenv())->bootEnv(dirname(__DIR__).'/.env');

The nginx config is this:

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param APP_ENV prod;
        fastcgi_param APP_DEBUG 0;
        fastcgi_param APP_SECRET your_secret;
        fastcgi_param DATABASE_URL "your_db_url_and_psw";
        
        include /etc/nginx/fastcgi.conf;
    internal;}

All work as intended, I just needed to manually create the directories var/log and var/cache/prod because of the error

PHP Fatal error:  Uncaught RuntimeException: Unable to write in the "cache" directory (/srv/nginx/my_project/htdocs/var/cache/prod)

CodePudding user response:

You definitely don't need to use the DotEnv package, that's just what Symfony uses by default and is pretty easy to use. Why don't you want to use DotEnv by the way?

You'll see the loading of dotenv the beginning if you open the public/index.php file. This parses the .env file, and injects the variable inside $_ENV.

(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');

So you can remove this line.

I don't recommend mixing nginx variables with .env variables, because it's a bad practice to configure your application in multiple places and it will lead you to errors.

Then check that nginx inject your variable inside $_ENV variable with var_dump($_ENV); at the beginning of the index.php.

I recommend you to use the DotEnv package expect if you have a really good reason not to use it.

  • Related