Symfony4 没有从 vHost 配置中获取环境变量

Symfony4 does not get Environment Varbiables from vHost Configuration

我目前正在使用 Raspbian 9 在 RaspberryPi 上设置 Web 服务器,并希望在其上部署一个高效的 Symfony4 网页。

对于这种情况,我在 Apache2 中创建了一个 vHost 文件,其中包含所需的环境变量。但是当我执行 "composer install --no-dev"(因为我不想使用 dotenv)时,我收到错误消息“PHP 致命错误:未捕获的 RuntimeException:APP_ENV未定义环境变量。您需要为配置定义环境变量或添加 "symfony/dotenv" 作为 Composer 依赖项以从 .env 文件加载变量。"

在 Symfonys Public 文件夹中创建 test.php 文件并查询 "echo $_SERVER['APP_ENV']" 时,我得到了正确设置的 "prod" 变量。

感谢您的帮助!

附上匿名虚拟主机文件:

<VirtualHost *:80>
    ServerName gate.keeper
    ServerAlias www.gate.keeper

    DocumentRoot /var/www/gk3/public
    <Directory /var/www/gk3/public>
        AllowOverride None
        Order Allow,Deny
        Allow from All

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ index.php [QSA,L]
        </IfModule>
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/gk3>
    #     Options FollowSymlinks
    # </Directory>

    # optionally disable the RewriteEngine for the asset directories
    # which will allow apache to simply reply with a 404 when files are
    # not found instead of passing the request into the full symfony stack
    <Directory /var/www/gk3/public/bundles>
        <IfModule mod_rewrite.c>
            RewriteEngine Off
        </IfModule>
    </Directory>
    ErrorLog /var/log/apache2/gk3_error.log
    CustomLog /var/log/apache2/gk3_access.log combined

    # optionally set the value of the environment variables used in the application
    SetEnv APP_ENV "prod"
    SetEnv APP_SECRET "secret"
    SetEnv DATABASE_URL "mysql://user:password@localhost:3306/db_name"
</VirtualHost>

当您在终端中 运行 composer 命令时,您正在命令行环境中访问 PHP,并且您不会通过 Apache 和您在虚拟主机。

也就是说,在执行 composer install --no-dev 之前,您需要 运行 以下命令才能设置 APP_ENV值:

export APP_ENV=prod