This post is part of the series WordPress and Elastic Beanstalk.
In the previous post Getting Started we created a new WordPress application deployed to an Elastic Beanstalk environment.
We manually cobbled together a source directory containing WordPress and Elastic Beanstalk configuration files, zipped up the directory into an artifact.zip package, and uploaded the bundle to Elastic Beanstalk using the web interface.
This process is fine for a one-off job but if you’re developing plugins or themes for your website, or upgrading WordPress or installing plugins it’s a good idea to automate the build and deployment processes. In this post we’ll create a project that will allow us to update our Elastic Beanstalk environment with a single command line. We’ll create scripts to copy files from our local WordPress installation, add in Elastic Beanstalk configuration files and other server files, and deploy the result to Elastic Beanstalk.
The files referenced below are available as a git repository. You can download or clone the repository and follow the README to get started. The below explains the files in more detail.
This post assumes that you have a local WordPress installation, an Elastic Beanstalk environment similar to the one described in an earlier post, access to a Bash shell (eg, Terminal on a Mac), and have installed the Elastic Beanstalk CLI.
Download or clone the git repository to your local machine. The resulting directory should appear as follows:
This directory holds configuration files for Elastic Beanstalk. During the build the entire directory is copied to the root of our source bundle. This directory has a single file env.config but you can add other configuration files and they will be included in the deployment.
This defines environment variables that are referenced by the wp-config.php file we’ll review later:
Replace all the placeholder text (in red) with your own values. If you’re unsure where to find these refer to the earlier post.
Note that your modified env.config file will contain sensitive information and should not be committed to git or any other repository.
This directory includes files that we want at the root level of our source bundle. The build script will include all files in this directory.
WordPress generates a .htaccess file to route to your WordPress directory and to enable pretty permalinks. Since you may have a different .htaccess file in your local environment, we use this one when deploying to Elastic Beanstalk.
This is a default .htaccess file that should work in most cases. Modify it as needed to suit your environment.
This is our WordPress configuration file. Although you probably have a wp-config.php file in your local WordPress installation, it’s likely not suitable for your Elastic Beanstalk environment. Maintaining separate configuration files means we can develop locally and deploy to production without having to swap configuration files in and out.
The keys in all the $_SERVER references map to the environment variables we defined in env.config.
This script assembles our source files and creates a zipped bundle. You need to first make it executable:
chmod +x build.sh
Enter the above in the command line. You should then be able to run the script by typing ./build.sh in the command line, though it won’t work until we make some changes.
Once you’ve changed “wordpress_local” to point to your WordPress installation you should be able to run ./build.sh to create the artifact. At this point you could upload the artifact to Elastic Beanstalk using the web interface, but we’ll add another script below to handle this automatically.
This is a simple script to deploy your artifact.zip to Elastic Beanstalk. Before executing the script we need to initialize our project directory for Elastic Beanstalk. (If you haven’t installed the Elastic Beanstalk CLI do so now.)
Navigate to the project directory in your shell and type eb init. Follow the prompts, making sure to select your WordPress application at “Select an application to use”. The command will create a new directory .elasticbeanstalk with a file config.yml. We need to modify the file to tell Elastic Beanstalk what to use as our source bundle:
The deploy script is straightforward:
Ensure that the deploy script is executable by typing chmod +x deploy.sh in your shell. You should now be able to enter ./deploy.sh in your shell to build and deploy your application to Elastic Beanstalk.
Now that you hopefully have a build process in place you can focus on developing your application. Check out Set Cache Controls in Elastic Beanstalk and Enable Server-side Compression in Elastic Beanstalk to improve your site’s performance, and Add a Custom Domain to an Elastic Beanstalk WordPress application to use your own domain name. Please feel free to ask questions or suggest improvements in the comments section below.
Comments Leave a comment
Leave a comment