如何在 AWS Elastic Beanstalk 上执行 Laravel Artisan 迁移?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22974516/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:20:25  来源:igfitidea点击:

How do I perform Laravel Artisan migrations on AWS Elastic Beanstalk?

phpamazon-web-serviceslaravelamazon-elastic-beanstalkamazon-rds

提问by oskarth

I have a Laravel installation and have set up three environments with their own corresponding config directories:

我有一个 Laravel 安装,并设置了三个具有自己相应配置目录的环境:

  • local
  • staging
  • production
  • 当地的
  • 分期
  • 生产

I use php artisan migrate:make create_users_tableetc as described hereto create database migrations.

我使用这里php artisan migrate:make create_users_table描述的 etc来创建数据库迁移。

In my local environment I use Vagrant and a simple MySQL server setup, and on staging & production I use AWS RDS.

在我的本地环境中,我使用 Vagrant 和一个简单的 MySQL 服务器设置,在暂存和生产中我使用 AWS RDS。

To configure database access for the stagingenvironmentI have a app/config/staging/database.phpfile with settings like this:

要为登台环境配置数据库访问,我有一个app/config/staging/database.php包含如下设置的文件:

...
"mysql" => array(
    "driver" => "mysql",
    "host" => $_SERVER["RDS_HOSTNAME"],
    "database" => $_SERVER["RDS_DB_NAME"],
    "username" => $_SERVER["RDS_USERNAME"],
    "password" => $_SERVER["RDS_PASSWORD"],
    "charset" => "utf8",
    "collaction" => "utf8_unicode_ci",
    "prefix" => "",
),
...

I use git to deploy the app with git aws.pushas described here.

我使用git的部署与应用程序git aws.push描述这里

The question is: How do I run the migration on my staging (and later production) EBS server when deploying?

问题是:部署时如何在我的登台(以及以后的生产)EBS 服务器上运行迁移?

回答by oskarth

I solved it by creating a new directory in the root of my project named .ebextensions. In that directory I created a script file my-scripts.config:

我通过在我的项目的根目录中创建一个名为.ebextensions. 在该目录中,我创建了一个脚本文件my-scripts.config

.ebextensions/
    my-scripts.config
app/
artisan
bootstrap
...

The file my-scripts.configgets executed when EBS deploys, is a YAML file and looks like this:

该文件my-scripts.config在 EBS 部署时执行,是一个 YAML 文件,如下所示:

container_commands:
    01-migration:
        command: "php /var/app/ondeck/artisan --env=staging migrate"
        leader_only: true

Add the directory and file to git, commit, and run git aws.pushand it will migrate.

将目录和文件添加到 git、提交和运行git aws.push,它将迁移。

Explanations on how stuff in .ebextensionsworks can be found here.

.ebextensions可以在此处找到有关如何工作的说明。

The path /var/app/ondeckis where your application lives when your script runs, it will afterwards be copied into /var/app/current.

路径/var/app/ondeck是脚本运行时应用程序所在的位置,之后它将被复制到/var/app/current.

The artisan option --env=stagingis useful for telling artisan what environment it should run in, so that it can find the correct database settings from app/config/staging/database.php

artisan 选项--env=staging可用于告诉 artisan 它应该在什么环境中运行,以便它可以从以下位置找到正确的数据库设置app/config/staging/database.php

If you need a quick and dirty way to log why the migrate command fails you might want to try out something like "php /var/app/ondeck/artisan --env=staging migrate > /tmp/artisan-migrate.log"so that you can log into your ec2 instance and check the log.

如果您需要一种快速而肮脏的方式来记录 migrate 命令失败的原因,您可能想要尝试类似的方法,"php /var/app/ondeck/artisan --env=staging migrate > /tmp/artisan-migrate.log"以便您可以登录到您的 ec2 实例并检查日志。