2015-06-17 9 views
9

Sto ospitando il mio progetto PHP su server AWS EC2, utilizzando Elastic Beanstalk. Ho impostato il mio ENV Vars utilizzando php dotenv, che sembrano essere sempre i miei Vars bene dalla mia radice .env di file:AWS ElasticBeanstalk ENV Vars non funziona

DbConnect.php:

require '../vendor/autoload.php'; 
$dotenv = new Dotenv($_SERVER['DOCUMENT_ROOT']); 
$dotenv->load(); 

$DB_HOST = getenv('DB_HOST'); 
$DB_USERNAME = getenv('DB_USERNAME'); 
$DB_PASSWORD = getenv('DB_PASSWORD'); 
$DB_DATABASE = getenv('DB_DATABASE'); 

$mysqli = new mysqli($DB_HOST, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE); 

Così, in AWS Management Console, Ho impostato lo stesso nome ENV vars all'interno della configurazione software, git pushed e re-deployed. Sto ricevendo un errore di 500 perché i vars EC2 ENV non sembrano essere in aumento.

enter image description here

C'è qualcos'altro che devo fare?


Aggiornamento:

eb printenv visualizzati i corretti valori var env.

+2

non sapeva chi votare giù senza alcun commento. La domanda mi sembra soddisfacente nei dettagli. Se hai downvoted, è meglio dare alcuni motivi – BMW

+1

Puoi sempre usare 'eb printenv' per assicurarti che gli oggetti siano quelli che ti aspetti che siano. –

+1

Hai provato a usare la sintassi '$ _SERVER ['DB_HOST']' per tutti gli envvar invece di getenv? –

risposta

5

Citazione di https://github.com/vlucas/phpdotenv

phpdotenv is made for development environments, and generally should not be used in production. In production, the actual environment variables should be set so that there is no overhead of loading the .env file on each request. This can be achieved via an automated deployment process with tools like Vagrant, chef, or Puppet, or can be set manually with cloud hosts like Pagodabox and Heroku.

Avrete errore fatale di PHP, se non si dispone di un file .env

Fatal error: Uncaught exception 'Dotenv\Exception\InvalidPathException' with message 'Unable to read the environment file at /home/vagrant/Code/project/.env.' in /home/vagrant/Code/project/vendor/vlucas/phpdotenv/src/Loader.php on line 75

Se volete Di seguito è riportato il codice di esempio che è possibile impostare Environment Variable per verificare, carica solo la classe Dotenv se è in ambiente locale/testing

if(getenv('APP_ENV') === 'local' || getenv('APP_ENV') === 'testing') 
{ 
    $dotenv = new Dotenv\Dotenv(__DIR__); 
    $dotenv->load(); 
} 

o un altro metodo è controllare il file .env esiste o no

$filePath = rtrim(__DIR__, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR . '.env'; 
if(is_file($filePath) && is_readable($filePath)) 
{ 
    $dotenv = new Dotenv\Dotenv(__DIR__); 
    $dotenv->load(); 
} 
Problemi correlati