Run PHP script after deployment on AWS Elastic Beanstalk

Run PHP script after deployment on AWS Elastic Beanstalk

September 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Run PHP script after deployment on AWS Elastic Beanstalk

Running PHP Scripts After Deployment on AWS Elastic Beanstalk

As a developer working with PHP applications deployed on AWS Elastic Beanstalk, there are times when you need to execute specific scripts post-deployment. This could be for configuration purposes, data initialization, or other one-time tasks that require environment-specific context.

In your case, you have a PHP script that generates a local.xml file for Magento with necessary database settings and credentials. You want this script to run automatically after the application is deployed on Elastic Beanstalk.

Understanding Elastic Beanstalk Hooks

Elastic Beanstalk provides several hooks that you can leverage for running scripts before or after deployment events, environment updates, or scaling activities. The hooks are configured through .ebextensions directories and .config files located in your application’s root directory.

For your scenario, the relevant hook is after_deployment. This hook runs after the deployment package has been fully extracted but before the new version of the application is made available to the users.

Creating the Configuration File

  1. Create a new directory structure inside your project root if it doesn’t exist:

    .ebextensions
        run_after_deployment.config
    
  2. Open or create run_after_deployment.config and add the following content:

files:
  "/opt/php/var/task/run_after_deployment.sh":
    mode: "000755"
    owner: root
    group: webapp
    source: https://s3.amazonaws.com/mybucket/run_after_deployment.sh

commands:
  runAfterDeployment:
    command: /opt/php/bin/sh /opt/php/var/task/run_after_deployment.sh

In this example, run_after_deployment.sh is a shell script that wraps your PHP script. Note the usage of https://s3.amazonaws.com/mybucket/run_after_deployment.sh. This indicates you need to upload your script to an S3 bucket with appropriate permissions.

The Wrapping Shell Script

The shell script (run_after_deployment.sh) should look like this:

#!/bin/sh

cd /var/app/current/
php generate_local_xml.php

This script changes the directory to where your application is deployed and runs the generate_local_xml.php script. The PHP script itself should be structured according to Magento’s requirements for generating the local.xml file.

Conclusion

Remember, the .ebextensions configuration files are processed in alphabetical order, so ensure your file name begins with a zero or low ASCII character if you have multiple files.

Elastic Beanstalk hooks provide a powerful way to automate tasks around your application’s lifecycle. By leveraging them, you can ensure critical setup scripts like yours run reliably every time the environment is deployed or updated.

Visit Laravel Company for more tips and best practices on automating tasks in AWS Elastic Beanstalk.

Database Extensions Configuration Deployment

Our network