You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在AWS Elastic Beanstalk环境中运行php artisan migrate失败求助

Laravel部署AWS EB时无法执行php artisan migrate的问题

我已按照AWS官方文档步骤部署Laravel应用,但部署过程中始终无法运行php artisan migrate。尝试过container_commands、.ebextensions/.platform/hooks等多种方案,全部无效。

SSH登录EC2实例手动执行migrate命令时,因环境变量未设置导致数据库连接失败报错;想登录EC2上运行的Docker容器,执行docker ps命令却提示docker是未知命令,这和EB文档明确说明的“EB运行Docker容器”不符,完全不知道遗漏了什么。


编辑补充内容

手动执行artisan migrate:status的报错信息

Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'forge' and table_type = 'BASE TABLE' order by table_name)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:822
    818▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    819▕                 );
    820▕             }
    821▕ 
  ➜ 822▕             throw new QueryException(
    823▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    824▕             );
    825▕         }
    826▕     }

      +35 vendor frames 

  36  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

从报错信息里的where table_schema = 'forge'可以判断,裸机上的环境变量未正确设置。但尝试登录Docker容器时,docker命令不可用。

Platform Hooks结构

platform hooks目录结构

钩子脚本内容

01_run_artisan.sh:

#!/bin/bash
cd /var/app/staging && pwd && ls -la

02_run_migrations.sh:

#!/bin/bash
cd /var/app/staging && php artisan migrate:status

container_commands尝试失败的日志

[ERROR] Command 01_artisan_migrate (cd /var/app/staging && php artisan migrate:status) failed

更新补充内容

EB事件视图错误信息

January 10, 2024 13:26:40 (UTC-7)   ERROR   During an aborted deployment, some instances may have deployed the new application version. To ensure all instances are running the same version, re-deploy the appropriate application version.
January 10, 2024 13:26:40 (UTC-7)   ERROR   Failed to deploy application.
January 10, 2024 13:26:39 (UTC-7)   ERROR   Unsuccessful command execution on instance id(s) 'i-xxxxxxxxx'. Aborting the operation.
January 10, 2024 13:26:39 (UTC-7)   INFO    Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].
January 10, 2024 13:26:39 (UTC-7)   ERROR   [Instance: i-xxxxxxxx] Command failed on instance. Return code: 1 Output: Engine execution has encountered an error..
January 10, 2024 13:26:36 (UTC-7)   ERROR   Instance deployment failed. For details, see 'eb-engine.log'

eb-engine.log内容

2024/01/10 20:26:36.123454 [INFO] Error occurred during build: Command 01-migration failed
2024/01/10 20:26:36.123478 [ERROR] An error occurred during execution of command [app-deploy] - [PostBuildEbExtension]. Stop running the command. Error: Container commands build failed. Please refer to /var/log/cfn-init.log for more details.

cfn-init.log内容

2024-01-10 20:26:36,088 [ERROR] -----------------------BUILD FAILED!------------------------
2024-01-10 20:26:36,088 [ERROR] Unhandled exception during build: Command 01-migration failed
Traceback (most recent call last):
  File "/opt/aws/bin/cfn-init", line 181, in <module>
    worklog.build(metadata, configSets, strict_mode)
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 137, in build
    Contractor(metadata, strict_mode).build(configSets, self)
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 567, in build
    self.run_config(config, worklog)
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 579, in run_config
    CloudFormationCarpenter(config, self._auth_config, self.strict_mode).build(worklog)
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 277, in build
    changes['commands'] = CommandTool().apply(
  File "/usr/lib/python3.9/site-packages/cfnbootstrap/command_tool.py", line 127, in apply
    raise ToolError(u"Command %s failed" % name)
cfnbootstrap.construction_errors.ToolError: Command 01-migration failed

内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 19:14:53