AWS ElasticBeanstalk ENV Vars 不工作
AWS ElasticBeanstalk ENV Vars not working
我使用 Elastic Beanstalk 在 AWS EC2 服务器上托管我的 PHP 项目。我已经使用 php dotenv
设置了我的 ENV Vars,这似乎让我的 vars 从我的根 .env
文件中得到了很好的:
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);
因此,在 AWS 管理控制台中,我在软件配置中设置了相同名称的 ENV 变量,git 推送,然后重新部署。我收到 500 错误,因为 EC2 ENV 变量似乎没有出现。
我还需要做些什么吗?
更新:
eb printenv
显示了正确的环境变量值。
引用自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.
如果您没有 .env 文件,您将遇到 php 致命错误
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
如果你愿意
下面是您可以设置 Environment Variable
进行检查的示例代码,它只加载 Dotenv class 如果它在本地/测试环境
if(getenv('APP_ENV') === 'local' || getenv('APP_ENV') === 'testing')
{
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
}
或者另一种方法是检查 .env 文件是否存在
$filePath = rtrim(__DIR__, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR . '.env';
if(is_file($filePath) && is_readable($filePath))
{
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
}
我使用 Elastic Beanstalk 在 AWS EC2 服务器上托管我的 PHP 项目。我已经使用 php dotenv
设置了我的 ENV Vars,这似乎让我的 vars 从我的根 .env
文件中得到了很好的:
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);
因此,在 AWS 管理控制台中,我在软件配置中设置了相同名称的 ENV 变量,git 推送,然后重新部署。我收到 500 错误,因为 EC2 ENV 变量似乎没有出现。
我还需要做些什么吗?
更新:
eb printenv
显示了正确的环境变量值。
引用自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.
如果您没有 .env 文件,您将遇到 php 致命错误
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
如果你愿意
下面是您可以设置 Environment Variable
进行检查的示例代码,它只加载 Dotenv class 如果它在本地/测试环境
if(getenv('APP_ENV') === 'local' || getenv('APP_ENV') === 'testing')
{
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
}
或者另一种方法是检查 .env 文件是否存在
$filePath = rtrim(__DIR__, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR . '.env';
if(is_file($filePath) && is_readable($filePath))
{
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
}