如何使用MySQL为AWS EC2设置Laravel 5的环境变量 [英] How to set environment variables for Laravel 5 on AWS EC2 with MySQL

查看:149
本文介绍了如何使用MySQL为AWS EC2设置Laravel 5的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将我的Laravel 5应用程序部署到AWS EC2。我还创建了一个具有AWS RDS的MySQL数据库,并将其与我的应用程序实例相关联。



现在我想设置我的env变量,以便在我的时候使用homesteads的默认值本地机器在开发中,以及我的AWS数据库部署和生产时。



从这里我对我的原始问题做了大量的修改,以反映我学到了什么因为问它



在本地开发的Laravel项目中,经典的.env大致如下:

  APP_ENV = local 
APP_DEBUG = true
APP_KEY = BF3nmfzXJ2T6XU8EVkyHtULCtwnakK5k(注意,不是真正的键)

DB_HOST = localhost
DB_DATABASE = homestead
DB_USERNAME = homestead
DB_PASSWORD = secret

CACHE_DRIVER =文件
SESSION_DRIVER =文件
QUEUE_DRIVER = sync

MAIL_DRIVER = smtp
MAIL_HOST = mailtrap.io
MAIL_PORT = 2525
MAIL_USERNAME = null
MAIL_PASSWORD = null

对于生产,我有翅我明白我只是用我的生产变量创建一个新的.env文件。当使用AWS时,我的.env文件大致如下:

  APP_ENV = production 
APP_DEBUG = false
APP_KEY = BF3nmfzXJ2T6XU8EVkyHtULCtwnakK5k(注意,不是真正的密钥)

DB_HOST = aaxxxxxxxxxxxxx.cyxxxxxxxxxx.eu-central-1.rds.amazonaws.com:3306
DB_DATABASE = MyAppsDatabaseName
DB_USERNAME = MyAWSRDSUserName
DB_PASSWORD = NotARealPassword

CACHE_DRIVER =文件
SESSION_DRIVER =文件
QUEUE_DRIVER = sync

MAIL_DRIVER = smtp
MAIL_HOST = mailtrap.io
MAIL_PORT = 2525
MAIL_USERNAME = null
MAIL_PASSWORD = null



我的问题/问题现在



我使用AWS eb cli从git部署我的应用程序。但是如何部署我的生产.env文件,而不必先把它推送到git?

解决方案

你可以创建一个新的。 env在你的ec2实例上,并添加所有env vars在那里。一个选项将是进入框并通过vi或cat创建文件。或者你可以写一个脚本从外部位置远程拉入.env。



你也可以将ssh插入框中,导出APP_ENV =生成所有的env vars(假设这是您的操作系统的正确命令)。



将env vars添加到您的环境将取决于操作系统您的ec2实例正在运行,所以解决方案将取决于此。 ec2有一个'标签'的概念,这可能是有用的,但文档显示他们将标签的数量限制为10,所以你可能需要手动和每个ec2实例:/



有关一种方法,请参见此处它使用标签来拉入并设置env vars(不具体)。



我刚刚经历了这个昨天,而Laravel在Elastic Beanstalk上运行,解决方案很干净。您可以通过aws控制台直接设置env vars(EB应用程序/环境 - >配置 - >软件配置 - >环境属性)。



更新:



要理解的关键概念是Laravel只是使用 phpdotenv 将.env文件中的变量转储到php的全局 $ _ ENV 中,而任何已经存在的env var都会自动包含在 $ _ ENV 当PHP启动服务器时( docs )。所以.env文件本身是不必要的,真的只是一个开发人员的便利。 (除非我刚刚被弹性豆串破坏了)。


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.

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.

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:

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:

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?

解决方案

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.

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).

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 :/

See here for one method that uses tags to pull in and set env vars (non-laravel specific).

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).

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).

这篇关于如何使用MySQL为AWS EC2设置Laravel 5的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆