简体   繁体   中英

Running Laravel's migration command on AWS Elastic Beanstalk

I'm having a hard time deploying a Laravel app for test purposes on AWS Elastic Beanstalk. Followed all sources i could find in web including AWS documentation.

Created a Elastic Beanstalk environment and uploading an application is straightforward as long as i do not include .ebextensions and the .yaml file in it.

Based on Maximilian's tutorial i created init.config file inside .ebextensions with contents:

container_commands:
    01initdb:
        command: "php artisan migrate"

Environment gets to a degraded state as it finishes to update and i get the following logs:

[2018-11-20T23:14:08.485Z] INFO  [7969]  : Command processor returning results: 
{"status":"FAILURE","api_version":"1.0","results":[{"status":"FAILURE","msg":"(TRUNCATED)...y exists\")\n/var/app/ondeck/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458\n\n2   PDOStatement::execute()\n/var/app/ondeck/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458\n\nPlease use the argument -v to see more details. \ncontainer_command 01initdb in .ebextensions/init.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI","returncode":1,"events":[]}],"truncated":"true"}

I have been trying different .config files from other instruction resources but none of them seems to work.

I'm running: Laravel Framework 5.7.5 EB Platform uses PHP 7.2 running on 64bit Amazon Linux/2.8.4 RDS uses MySQL 5.6.40

I really do not know what is going on and would appreciate if you could give any suggestion.

I finally found my way out. Providing some documentation for anyone that hits the same issue.

What I was trying to do...

My main objective was to test a Laravel 5.7 application on a live AWS Elastic Beanstalk (EB) server. I was also in need of a way to visualize data using phpMyAdmin, a tool that fits my need. This is a very simple CRUD app just for learning the basics of both technologies.

What I did (worked)

Followed the normal workflow of creating an EB application mainly using the web console.

  1. Name the application
  2. Chose PHP as a platform
  3. Start off with a base application (do not upload code yet)
  4. Hit configure more options
  5. In security card select your key pair and save. ( This is valuable for SSH'ing on your server )
  6. In the database, the card creates an RDS instance. Select whatever options that fit your needs and set a username/password.
  7. Create environment.

After a while, you should have all resources created by EB (EC2 and RDS instances, security group, EIP, Buckets, etc) in the app environment.

Preparing your Laravel application is a straight forward process. You must not forget to change config/database.php to read server variables. My approach was to define them at the start of the file.

The main sources of troubles reside in configuring your server instance to include all software and configuration needed by your app and specific needs. This is done by including a .yaml file inside .ebextensions folder. This folder should reside in the root directory of your Laravel application. It's also a good idea to check your syntax before submitting another app version to EB. As per my needs, I used this script which basically installs phpMyAdmin as I deploy a new version. Specifically for this startup script, environment variables should be defined, namely $PMA_VER, $PMA_USERNAME, $PMA_PASSWORD for phpMyAdmin to work. You can create more environment variables in the software tab of your EB configuration page. Read the docs.

Another detail that might cause issues in running commands at startup using YAML script (specifically migration ) is caused by Laravel and MySql versions. As for example, I am using Laravel 5.7 and the default MySQL version option in EB RDS creation wizard is something like 5.6.x. This will throw issues of the type :

  Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))

If this is your scenario, despite you should have already googled and sorted out that adding the line of code Schema::defaultStringLength(191); to the boot function of your app/Providers/AppServiceProviders.php file will do the trick.

You can do a typical migration passing the script:

container_commands: 
  01_drop_tables:
    command:
      "php artisan migrate:fresh"

  02_initdb:
    command: 
      "php artisan migrate"

This will drop existing tables avoiding conflicts and create a new one based on your code. You can read more logs from your server by SSH'ing and getting content of /var/log/eb-activity.log .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM