阅读背景:

如何使用MySQL在AWS EC2上设置Laravel 5的环境变量

来源:互联网 

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中呢?

2 个解决方案

#1


12  

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,并在其中添加所有的env vars。一种选择是将文件导入到框中并通过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到box,导出APP_ENV=生产所有的env vars(假设这是您的操作系统的正确命令)。

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 here for one method that uses tags to pull in and set env vars (non-laravel specific).

请参阅这里的一个方法,该方法使用标记拉入并设置env vars(非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).

我昨天刚通过这个,让Laravel在弹性豆茎上跑步,解决方法是干净的。您可以直接通过aws控制台(EB应用程序/环境->配置->软件配置->环境属性)设置env vars。

Update:

更新:

The key concept to understand is that Laravel just uses phpdotenv to 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文件中的vars转储到php的全局$_ENV中,而当php启动服务器(docs)时,任何已经存在的env vars都会自动包含在$_ENV中。因此。env文件本身是不必要的,实际上只是一个开发方便。(除非到目前为止我还被弹性豆茎给宠坏了)。

#2


17  

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。去你的弹性豆茎仪表板


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根设置为/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 -路径/美元/你/键/对pem /文件。pem ec2 - user@ec1 - 11 - 11 - 11 - 111. -欧盟-中央- 1. compute.amazonaws.com

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。为您的存储文件夹提供读/写访问,否则应用程序不能写入到logs文件夹,这将导致在运行迁移时出现错误。提供访问:$ sudo chmod -R ugo+rw存储


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所示。终于!请运行您的迁移并执行其他artisan命令!$ php artisan migration Success应该大致如下图所示:


分享到: