Overview
With the salts out of the way, we’ll need to stub in the basic commands under the .ebextensions directory that help define the lifecycle of our Elasticbeanstalk application.
In the interest of getting to our first testable source bundle, we’ll start by stubbing in commands.config under .ebextensions. So that, later, the configuration and lifecycle can be managed over in the Elasticbeanstalk console. This way we can manage all the ongoing tedium of environment re-builds, new deployments, and updates of WordPress, as well as the underlying infrastructure, including os updates & patches, for ongoing security.
Setup commands.config
Let’s create a new commands.config under the .ebextensions directory so that a growing collection of features can be easily accommodated as work progresses.
To start, let’s first define a new file to be created during the deployment of the Elasticbeanstalk source bundle:
files: "/home/ec2-user/reapply_configuration.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash set -xev source /opt/elasticbeanstalk/support/envvars # # Used for initial deploy and re-apply of configs # # Supported php.ini customizations sed -i "s/upload_max_filesize = .*/upload_max_filesize = ${PHP_UPLOAD_MAX_FILESIZE}/g" /etc/php.ini
I make sure to fail the script immediately in the first line, in the set, and then, source in the environment variables from Elasticbeanstalk. And then finally, execute the supported php.ini customizations in our script, all conveniently managed through the Elasticbeanstalk console.
Now that the functionality has been defined, it’s ready to use during the initial deployment of the EC2 instance being managed by Elasticbeanstalk. Add the following snippet above the one just defined so that it appears right after files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_post.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash set -e cd /var/app/current find . -type f -exec chmod 664 {} + find . -type d -exec chmod 775 {} + chmod 660 wp-config.php /home/ec2-user/reapply_configuration.sh
And then, put the command above the files block, that establishes the post directory we are leveraging in Elasticbeanstalk to handle at the end of the deployment:
commands: create_post_dir: command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post" ignoreErrors: true
And then, finally, above that block, enact each re-config of the Elasticbeanstalk environment:
"/opt/elasticbeanstalk/hooks/configdeploy/enact/00_php.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash # # Executed per ElasticBeanstalk re-config (environment variables) # set -xev /home/ec2-user/reapply_configuration.sh
So now commands.config should end up something like:
commands: create_post_dir: command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post" ignoreErrors: true files: "/opt/elasticbeanstalk/hooks/configdeploy/enact/00_php.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash # # Executed per ElasticBeanstalk re-config (environment variables) # set -xev /home/ec2-user/reapply_configuration.sh "/opt/elasticbeanstalk/hooks/appdeploy/post/99_post.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash set -e cd /var/app/current find . -type f -exec chmod 664 {} + find . -type d -exec chmod 775 {} + chmod 660 wp-config.php /home/ec2-user/reapply_configuration.sh "/home/ec2-user/reapply_configuration.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash set -xev source /opt/elasticbeanstalk/support/envvars # # Used for initial deploy and re-apply of configs # # Supported php.ini customizations sed -i "s/upload_max_filesize = .*/upload_max_filesize = ${PHP_UPLOAD_MAX_FILESIZE}/g" /etc/php.ini
Back in build.py
Now that commands.config has been established, let’s go ahead and get those copied into the wordpress directory from which the source bundle zip will be built:
# Files are always re-copied, per build shutil.copy('.ebextensions/commands.config', 'wordpress/.ebextensions') shutil.copy('wp-config.php', 'wordpress')
References
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
https://wordpress.org/support/article/editing-wp-config-php/