app icon indicating copy to clipboard operation
app copied to clipboard

New installation, "" is invalid environment under PHP-FPM

Open lourdas opened this issue 3 months ago • 10 comments

I just saw the first stable release of the app package for Yii3 and thought to try it out myself.

I run composer create-project yiisoft/app myproject locally in my Linux box, I corrected the permissions so that the httpd user can write in the folders and when I click on the public directory in my browser, I get this error:

Fatal error: Uncaught RuntimeException: "" is invalid environment. in /home/xxxxx/public_html/myproject/src/Environment.php on line 77
Image

If I run yii serve from the cli, it works, since I pass APP_ENV as a environment variable before the command.

But how the heck do I do this in the web application? I see no reference in the docs about what to do, let's say I'm a newbie user and I want to try the framework out. It fails miserably, and I will look somewhere else.

I've been using Yii 1.1 and 2.0 as my framework of choice for all my PHP projects for the last decade or so, but Yii3 seems like an alien thing to me. Am I too old to deploy it under the classic php-fpm-Apache installation that only other deployment types are now of relevance (like docker)?

I guess the docs could be improved.

lourdas avatar Aug 25 '25 16:08 lourdas

Environment variables are quite cumbersome to use without any isolation such as Docker. If you have only a single project, defining variables locally should do such as

export APP_ENV=dev

and then adding env APP_ENV to the very top of your Nginx config.

samdark avatar Aug 25 '25 17:08 samdark

Also try fastcgi_param APP_ENV dev; in your location block. Not sure if it will work well since I haven't touched "classic" setups for a while. But if I'd still used it, I'd have used it this way.

samdark avatar Aug 25 '25 17:08 samdark

Can I hard-code somewhere in the project files the APP_ENV environment? This is totally different compared to the Yii 2 way, where you changed the index.php file where you have the YII_DEBUG and YII_ENV constants. If I don't need isolation and the same installation (PHP-FPM/Apache/Nginx) is used for several projects, which way should I follow?

Upgrading from Yii 2 needs more details.

lourdas avatar Aug 25 '25 17:08 lourdas

Can I hard-code somewhere in the project files the APP_ENV environment? This is totally different compared to the Yii 2 way, where you changed the index.php file where you have the YII_DEBUG and YII_ENV constants. If I don't need isolation and the same installation (PHP-FPM/Apache/Nginx) is used for several projects, which way should I follow?

Hard-code way is use default value instead of exception in Environment::setEnvironment() method:

https://github.com/yiisoft/app/blob/5705eb836ca8536ee9ec52565ee54510a6563c40/src/Environment.php#L73-L82

Or another way — add $_ENV['APP_ENV'] = 'dev'; to /src/autoload.php before Environment::prepare();.

vjik avatar Aug 25 '25 19:08 vjik

Can we not close this yet, this is a serious issue. I've just ran into the same problem, developing on a Debian 13 VM on Proxmox. Not everybody is running Docker and there's no reason for it, when using Plesk with a lot of vHosts. Also with clients having only vHosts and no access to their server configuration, modifying core files seems strange.

bigrepository avatar Aug 26 '25 18:08 bigrepository

@bigrepository reopened. btw., template is not "core" files. Once you've used it, this code is all yours.

samdark avatar Aug 26 '25 18:08 samdark

If environment don't support environment variables' setting, vlucas/phpdotenv usage will be good way: initiate it in /src/autoload.php before Environment::prepare();.

vjik avatar Aug 27 '25 06:08 vjik

Some good topics for a cookbook.

samdark avatar Aug 27 '25 10:08 samdark

https://github.com/yiisoft/app/pull/399

samdark avatar Aug 30 '25 10:08 samdark

поддерживаю поставил приложение через DDEV 15 минут ломал голову куда ENV прописать пока в этот issue не попал...

proweb avatar Sep 01 '25 19:09 proweb