Symfony4不从vHost配置获取环境变量

时间:2018-03-23 11:08:55

标签: php apache deployment environment-variables symfony4

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

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

在Symfonys公共文件夹中创建test.php文件并查询“echo $ _SERVER ['APP_ENV']”时,我正确设置了“prod”变量。

感谢您的帮助!

附上匿名的vHost文件:

<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>

1 个答案:

答案 0 :(得分:2)

当您在终端中运行composer命令时,您将在命令行环境中访问PHP,并且您不会通过Apache以及您在VirtualHost中执行的任何配置。

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

export APP_ENV=prod