9

I have an application with Composer dependencies which I want to deploy to an Elastic Beanstalk container. However my composer.json file is not in the project root folder. My project root has the following structure:

- .ebextensions
- scripts
- www (Webroot)
  - composer.json

And I have set the document root to /www in the container configuration options. The issue is that I need to install composer on the box and run the composer install script to add the project dependencies. I understand that during a deploy EB will check to see if there is a composer.json file in the project root and install Composer but in this case my composer.json file is in a sub-directory.

I thought that could use .ebextenstions to add commands to install Composer and dependencies after the application has been deployed. I created a file .ebextensions/01-composer.config with the following container commands:

container_commands:
  01-install-composer:
    command: "curl -sS https://getcomposer.org/installer | php"
  02-install-packages:
    command: "php composer.phar install"
    cwd: "/var/app/current/www/"

But my app won't deploy with this configuration. Would appreciate some assistance to see where I am going wrong.

Thanks.

Adrian Walls
  • 852
  • 3
  • 19
  • 31
  • 4
    Have you tried moving composer.json to the root of your project and deploying it? – tbjers Jul 08 '13 at 22:24
  • A quick test and this appear to have worked. Thanks. Not sure why I didn't actually think of that, seems obvious now. – Adrian Walls Jul 09 '13 at 08:20
  • 1
    I had a somewhat related problem. It is important to note that you need to create the .ebextensions folder-- do not use the existing .elasticbeanstalk folder. – Acyra Feb 09 '14 at 14:10

4 Answers4

19

Composer is already installed default in Beanstalk's PHP AMI.

Also consider that container_commands are ran through '/var/app/ondeck' and not on current. Try something like this:

container_commands:
  01-install-packages:
    command: "composer.phar install -d /var/app/ondeck/www"
Ker Ruben Ramos
  • 642
  • 5
  • 13
  • There is no /var/app/ondeck/www directory. Is this something only visable to Beanstalk and not to a user (root) logged directly onto the EC2 instance? – Adrian Walls Jul 08 '13 at 19:59
  • 5
    The `ondeck` directory is created during container_commands phase. Here's a slight summary of it: * download new version * extract code on /var/app/ondeck * run all `commands` * run all `container_commands` with default CWD=/var/app/ondeck * if it passed without errors, it will replace the current with ondeck :edit reasons: crappy enter button acting as SAVE. – Ker Ruben Ramos Jul 08 '13 at 22:34
  • 1
    Thanks, this is really useful to know. I moved composer to the project root directory as suggested by @tbjers and linked everything from there which after a quick test appears to have worked. This means EB deals with all of this out of the box and I don't have to tinker with it. But I'll try this as well just of out curiosity. – Adrian Walls Jul 09 '13 at 08:24
9

Just a note, most of the PHP containers that AWS is using in Elastic Beanstalk are auto deploying by running composer.phar install now. You should be able to skip this step if you don't have a "vendors" folder present. If you want to run it manually, the above methods should work, but you should only need something like @kewubenduben mentioned.

If you are trying to access a private remote repository, check out the Q and A here: AWS Elastic Beanstalk using PHP with Private Composer Repositories , shameless plug.

Community
  • 1
  • 1
four43
  • 1,675
  • 2
  • 20
  • 33
1

Went with the suggestion provided by @tbjes and moved composer related files outside of my document root to the project root and after a quick test all appears to be working out of the box without having to run composer via .ebxtenstions config files.

Adrian Walls
  • 852
  • 3
  • 19
  • 31
0

The syntax has changed slightly as of 2019. To run composer automatically when you deploy via elastic beanstalk, add the following file (01composer.config) to a folder called ".ebextensions" in the root of your repository / code project :

enter image description here

The contents of that file should be a follows, for a simple but effective run of composer each time your code is deployed :

commands:
   composer_update:
      command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update

option_settings:
   - namespace: aws:elasticbeanstalk:application:environment
     option_name: COMPOSER_HOME
     value: /root

container_commands:
  01-install_dependencies:
       command: "php /usr/bin/composer.phar install"
       cwd: "/var/app/ondeck"
  
  02-optimize:
      command: "php /usr/bin/composer.phar dump-autoload --optimize"
      cwd: "/var/app/ondeck"

Spacing is important. Indent as the code above (copied from a working example, June 2020). The number 01 at the beginning of the file name indicates the running order of these command files. You can change these numbers / order based on your setup. I always put composer 1st in the list.

EDIT: FYI composer install doesn't install composer! It installs the packages within composer. Composer must already be installed, which it should be by default as part of AWS's PHP AMI.

Leon
  • 1,851
  • 3
  • 21
  • 44