Laravel没有选择环境变量

时间:2018-11-04 22:48:34

标签: php laravel

我正在尝试使用Apache和Laravel在Docker容器中运行PHP应用程序。我的dockerfile如下所示:

#Create a minimalistic docker container for deploying PHP/Laravel applications
FROM php:7-apache
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
RUN a2enmod rewrite 
RUN a2enmod setenvif
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!AllowOverride None!AllowOverride All!g' /etc/apache2/apache2.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN sed -i '10i \
        SetEnvIf X-Forwarded-Proto "https" HTTPS=on \
        RewriteEngine on \
        RewriteCond %{HTTPS} !=on \
        RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC] \
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] \n' /etc/apache2/sites-available/*.conf
RUN mv $PHP_INI_DIR/php.ini-production $PHP_INI_DIR/php.ini
RUN apt-get update && apt-get install -y \
        zip \
        unzip \
        wget \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libpq-dev \
    && docker-php-ext-install -j$(nproc) iconv \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pdo pdo_pgsql pgsql

COPY install-composer.sh /usr/local/bin
RUN /usr/local/bin/install-composer.sh
COPY . /var/www/html
WORKDIR /var/www/html
RUN composer install --no-plugins --no-scripts --optimize-autoloader --no-dev
RUN composer require nunomaduro/collision --dev
RUN php artisan config:cache
RUN chown -R www-data:www-data /var/www/html
#RUN php artisan route:cache

我将容器部署在AWS ECS上,并在启动配置中设置环境变量。但是,由于某些原因,Laravel不会选择任何环境变量。当我检查正在运行的容器时:

docker exec -it <<mycontainerid>> /bin/bash
root@ed472a71a444:/var/www/html#env

我可以看到所有环境变量都已正确设置:

root@0c591ce9ef7c:/var/www/html# env
DB_PORT=5432
MAIL_DRIVER=smtp
DB_HOST=xxxxxxxxxxxxxxxxxxxxxxxxxxx
CAPTCHA_SITE_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxx
HOSTNAME=xxxxxxxxx
MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APACHE_CONFDIR=/etc/apache2
PHPIZE_DEPS=autoconf        dpkg-dev        file        g++         gcc         libc-dev        make        pkg-config      re2c
GPG_KEYS=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APP_DEBUG=false
PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2 --disable-cgi
MAIL_HOST=xxxxxxxxxxxxxxxxxxxxxxxxxxx
PHP_ASC_URL=https://secure.php.net/get/php-7.2.11.tar.xz.asc/from/this/mirror
PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2
APACHE_DOCUMENT_ROOT=/var/www/html/public
PHP_EXTRA_BUILD_DEPS=apache2-dev
APP_ENV=production
PWD=/var/www/html
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

但是当我这样做时:

root@ed472a71a444:/var/www/html# php artisan tinker
Psy Shell v0.9.8 (PHP 7.2.11 — cli) by Justin Hileman
>>> print_r($_ENV)
Array
(
    [SHELL_VERBOSITY] => 0
)
=> true

但是,运行以下命令将返回所有变量:

root@ac526d5b9184:/var/www/html# php artisan tinker
Psy Shell v0.9.8 (PHP 7.2.11 — cli) by Justin Hileman
>>> getenv()
=> [
     "DB_PORT" => "5432",
     "MAIL_DRIVER" => "smtp",
     "DB_HOST" => "xxxxxxxxxxxxx",
     "CAPTCHA_SITE_KEY" => "xxxxxxxxxxxxxxxxxxxxxxxx",
     "APP_DEBUG" => "false",
     "PHP_EXTRA_CONFIGURE_ARGS" => "--with-apxs2 --disable-cgi",
     "PHP_ASC_URL" => "https://secure.php.net/get/php-7.2.11.tar.xz.asc/from/this/mirror",
     ...........
  ]

我在做什么错?我该如何解决?

更新1:我意识到我的PHP.ini具有此功能:

variables_order = "GPCS"

在开头添加“ E”会导致环境变量出现在$_ENV中,但是Laravel仍然无法为其准备:

[2018-11-04 23:20:14] production.ERROR: No application encryption key has been specified. {"exception":"[object] (RuntimeException(code: 0): No application encryption key has been specified. at /var/www/html/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php:42)
[stacktrace]

更新2:以下内容也适用,但是不确定Laravel为什么看不到它:

root@b377f6cf15f0:/var/www/html# php artisan tinker
Psy Shell v0.9.8 (PHP 7.2.11 — cli) by Justin Hileman
>>> env('APP_KEY')
=> "base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
>>>

1 个答案:

答案 0 :(得分:0)

好的,感谢@Travis Britz,我弄清楚了。由于Please enter the seed number: 532 Please enter the hidden bound: 800 Upper bound is 800 Completed in 4 iterations Completed in 46.778499624 seconds 作为docker build的一部分运行,并且在构建时未设置环境变量,因此所有环境变量的值均为空。我删除了artisan config:cache命令,并用config:cache替换了它,似乎已经奏效了!

相关问题