git 如何使用 MySQL 在 AWS EC2 上为 Laravel 5 设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28721365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set environment variables for Laravel 5 on AWS EC2 with MySQL
提问by MartinJH
I have successfully deployed my laravel 5 app to AWS EC2. I have also created a MySQL database with AWS RDS and associated it with my app instance.
我已成功将我的 laravel 5 应用程序部署到 AWS EC2。我还使用 AWS RDS 创建了一个 MySQL 数据库并将其与我的应用程序实例相关联。
Now I want to set my env variables so it uses homesteads default values when on my local machine in development, and my AWS database when deployed and in production.
现在我想设置我的 env 变量,以便它在我的本地机器上进行开发时使用 homesteads 默认值,在部署和生产时使用我的 AWS 数据库。
From here I've made a major edit to my original question to reflect what I've learned since asking it
从这里开始,我对我的原始问题进行了重大编辑,以反映自从提出问题以来我学到的东西
The classic .env in a laravel project for local development looks roughly like this:
用于本地开发的 laravel 项目中的经典 .env 大致如下所示:
APP_ENV=local
APP_DEBUG=true
APP_KEY=BF3nmfzXJ2T6XU8EVkyHtULCtwnakK5k (Note, not a real key)
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
For production, I've finally understood that I simply create a new .env file with my production variables. When using AWS, my .env file would roughly look like this:
对于生产,我终于明白我只是用我的生产变量创建一个新的 .env 文件。使用 AWS 时,我的 .env 文件大致如下所示:
APP_ENV=production
APP_DEBUG=false
APP_KEY=BF3nmfzXJ2T6XU8EVkyHtULCtwnakK5k (Note, not a real key)
DB_HOST=aaxxxxxxxxxxxxx.cyxxxxxxxxxx.eu-central-1.rds.amazonaws.com:3306
DB_DATABASE=MyAppsDatabaseName
DB_USERNAME=MyAWSRDSUserName
DB_PASSWORD=NotARealPassword
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
My question/problem now
我现在的问题/问题
I use AWS eb cli to deploy my app from git. But how do I deploy my production .env file without having to push it to git first?
我使用 AWS eb cli 从 git 部署我的应用程序。但是如何部署我的生产 .env 文件而不必先将其推送到 git?
回答by MartinJH
Russ Matney above gave the right answer, so he gets the checkmark. I'll write my own answer here to add in details on how I made things work. I assume you do have your database set up and have all the credentials you need.
上面的 Russ Matney 给出了正确的答案,所以他得到了复选标记。我将在这里写下我自己的答案,以添加有关我如何使事情运作的详细信息。我假设您确实设置了数据库并拥有所需的所有凭据。
1.Go to your elastic beanstalk dashboard
1.转到您的弹性 beanstalk 仪表板
2.Next go to your software config
2.接下来去你的软件配置
3.Add your production environment variables as shown below. Remember to set the doc root to /public, and also add :3306 at the end of your database end point to avoid the PDOEXCEPTION error.
3.添加您的生产环境变量,如下所示。请记住将 doc root 设置为 /public,并在数据库端点的末尾添加 :3306 以避免 PDOEXCEPTION 错误。
See bigger version of picture below
4.Next SSH into your apps eb instance. See details here, or try the following below:
4.接下来通过 SSH 连接到您的应用程序 eb 实例。在此处查看详细信息,或尝试以下操作:
$ ssh -i path/to/your/key/pair/pem/file.pem [email protected]
$ ssh -i path/to/your/key/pair/pem/file.pem [email protected]
Note the ec1-11-11-11-111.eu-central-1.compute.amazonaws.com is your apps public DNS. You'll find yours right here:
请注意 ec1-11-11-11-111.eu-central-1.compute.amazonaws.com 是您的应用程序公共 DNS。你会在这里找到你的:
5.cd to your app: $ cd /var/app/current
5.cd 到您的应用程序:$ cd /var/app/current
6.Give read/write access to your storage folder or the app can't write to the logs folder and that'll result in an error when running the migrations. To give access: $ sudo chmod -R ugo+rw storage
6.授予对存储文件夹的读/写访问权限,否则应用程序无法写入日志文件夹,这将导致运行迁移时出错。授予访问权限:$ sudo chmod -R ugo+rw storage
7.Finally! Run your migrations and do other artisan commands if you please! $ php artisan migrate
Success should roughly look like this from gitbash:
7.终于!如果您愿意,请运行您的迁移并执行其他工匠命令!$ php artisan migrate
从 gitbash 来看,成功应该大致如下:
回答by Russ Matney
You could create a new .env on your ec2 instance and add all the env vars in there. One option would be ssh-ing into the box and creating the file via vi or cat. Or you could write a script to remotely pull the .env in from an external location.
您可以在您的 ec2 实例上创建一个新的 .env 并在其中添加所有环境变量。一种选择是通过 ssh 进入框并通过 vi 或 cat 创建文件。或者您可以编写一个脚本来从外部位置远程拉入 .env。
You could also ssh into the box and export APP_ENV=production
all your env vars (assuming that's the right command for your OS).
您还可以通过 ssh 进入框和export APP_ENV=production
所有环境变量(假设这是您操作系统的正确命令)。
Adding env vars to your environment will depend on the OS that your ec2 instance is running, so the solution will depend on that. ec2 has a concept of 'tags' which might be useful, but the docs show they limit the number of tags to 10, so you may have to do it manually and per ec2 instance :/
将 env vars 添加到您的环境将取决于您的 ec2 实例运行的操作系统,因此解决方案将取决于它。ec2 有一个“标签”的概念,这可能很有用,但文档显示他们将标签数量限制为 10,因此您可能必须手动和每个 ec2 实例执行此操作:/
See herefor one method that uses tags to pull in and set env vars (non-laravel specific).
有关使用标签引入和设置环境变量(非 laravel 特定)的一种方法,请参见此处。
I just went through this yesterday while getting Laravel running on Elastic Beanstalk, the solution was clean. You can actually set the env vars directly via the aws console (EB app/environment -> Configuration -> Software Configuration -> Environment Properties).
我昨天刚刚在 Elastic Beanstalk 上运行 Laravel 时经历了这个,解决方案很干净。您实际上可以通过 aws 控制台(EB 应用程序/环境 -> 配置 -> 软件配置 -> 环境属性)直接设置环境变量。
Update:
更新:
The key concept to understand is that Laravel just uses phpdotenvto dump vars from the .env file into php's global $_ENV
, whereas any already existing env vars are automatically included in $_ENV
when php starts the server (docs). So the .env file itself is unnecessary, really just a dev convenience. (unless I've just been spoiled by elastic beanstalk so far).
要理解的关键概念是 Laravel 只是使用phpdotenv将 .env 文件中的变量转储到 php 的 global 中$_ENV
,而任何已经存在的 env 变量都会在$_ENV
php 启动服务器时自动包含(docs)。所以 .env 文件本身是不必要的,实际上只是为了方便开发。(除非到目前为止我刚刚被弹性豆茎宠坏了)。