Step 2: Configure your source content to be deployed to the Amazon Linux or Red Hat Enterprise Linux Amazon EC2 instance
Now it's time to configure your application's source content so you have something to deploy to the instance.
Topics
Get the source code
For this tutorial, you deploy the WordPress content publishing platform from your development machine to the target Amazon EC2 instance. To get the WordPress source code, you can use built-in command-line calls. Or, if you have Git installed on your development machine, you can use that instead.
For these steps, we assume you downloaded a copy of the WordPress source code to the
/tmp
directory on your development machine. (You can choose any
directory you like, but remember to substitute your location for /tmp
wherever it is specified in these steps.)
Choose one of the following two options to copy the WordPress source files to your development machine. The first option uses built-in command-line calls. The second option uses Git.
Topics
To get a copy of the WordPress source code (built-in command-line calls)
-
Call the wget command to download a copy of the WordPress source code, as a .zip file, to the current directory:
wget https://github.com/WordPress/WordPress/archive/master.zip
-
Call the unzip, mkdir, cp, and rm commands to:
-
Unpack the
master
.zip file into the/tmp/WordPress_Temp
directory (folder). -
Copy its unzipped contents to the
/tmp/WordPress
destination folder. -
Delete the temporary
/tmp/WordPress_Temp
folder andmaster
file.
Run the commands one at a time:
unzip master -d /tmp/WordPress_Temp
mkdir -p /tmp/WordPress
cp -paf /tmp/WordPress_Temp/WordPress-master/* /tmp/WordPress
rm -rf /tmp/WordPress_Temp
rm -f master
This leaves you with a clean set of WordPress source code files in the
/tmp/WordPress
folder. -
To get a copy of the WordPress source code (Git)
-
Download and install Git
on your development machine. -
In the
/tmp/WordPress
folder, call the git init command. -
Call the git clone command to clone the public WordPress repository, making your own copy of it in the
/tmp/WordPress
destination folder:git clone https://github.com/WordPress/WordPress.git /tmp/WordPress
This leaves you with a clean set of WordPress source code files in the
/tmp/WordPress
folder.
Create scripts to run your application
Next, create a folder and scripts in the directory. CodeDeploy uses these scripts to set up and deploy your application revision on the target Amazon EC2 instance. You can use any text editor to create the scripts.
-
Create a scripts directory in your copy of the WordPress source code:
mkdir -p /tmp/WordPress/scripts
-
Create an
install_dependencies.sh
file in/tmp/WordPress/scripts
. Add the following lines to the file. Thisinstall_dependencies.sh
script installs Apache, MySQL, and PHP. It also adds MySQL support to PHP.#!/bin/bash sudo amazon-linux-extras install php7.4 sudo yum install -y httpd mariadb-server php
-
Create a
start_server.sh
file in/tmp/WordPress/scripts
. Add the following lines to the file. Thisstart_server.sh
script starts Apache and MySQL.#!/bin/bash systemctl start mariadb.service systemctl start httpd.service systemctl start php-fpm.service
-
Create a
stop_server.sh
file in/tmp/WordPress/scripts
. Add the following lines to the file. Thisstop_server.sh
script stops Apache and MySQL.#!/bin/bash isExistApp="pgrep httpd" if [[ -n $isExistApp ]]; then systemctl stop httpd.service fi isExistApp=pgrep mysqld if [[ -n $isExistApp ]]; then systemctl stop mariadb.service fi isExistApp=pgrep php-fpm if [[ -n $isExistApp ]]; then systemctl stop php-fpm.service fi
-
Create a
create_test_db.sh
file in/tmp/WordPress/scripts
. Add the following lines to the file. Thiscreate_test_db.sh
script uses MySQL to create atest
database for WordPress to use.#!/bin/bash mysql -uroot <<CREATE_TEST_DB CREATE DATABASE IF NOT EXISTS test; CREATE_TEST_DB
-
Finally, create a
change_permissions.sh
script in/tmp/WordPress/scripts
. This is used to change the folder permissions in Apache.Important
This script updated permissions on the
/tmp/WordPress
folder so that anyone can write to it. This is required so that WordPress can write to its database during Step 5: Update and redeploy your WordPress application. After the WordPress application is set up, run the following command to update permissions to a more secure setting:chmod -R 755 /var/www/html/WordPress
#!/bin/bash chmod -R 777 /var/www/html/WordPress
-
Give all of the scripts executable permissions. On the command line, type:
chmod +x /tmp/WordPress/scripts/*
Add an application specification file
Next, add an application specification file (AppSpec file), a YAML
-
Map the source files in your application revision to their destinations on the target Amazon EC2 instance.
-
Specify custom permissions for deployed files.
-
Specify scripts to be run on the target Amazon EC2 instance during the deployment.
The AppSpec file must be named appspec.yml
. It must be
placed in the root directory of the application's source code. In this tutorial, the root
directory is /tmp/WordPress
With your text editor, create a file named appspec.yml
.
Add the following lines to the file:
version: 0.0 os: linux files: - source: / destination: /var/www/html/WordPress hooks: BeforeInstall: - location: scripts/install_dependencies.sh timeout: 300 runas: root AfterInstall: - location: scripts/change_permissions.sh timeout: 300 runas: root ApplicationStart: - location: scripts/start_server.sh - location: scripts/create_test_db.sh timeout: 300 runas: root ApplicationStop: - location: scripts/stop_server.sh timeout: 300 runas: root
CodeDeploy uses this AppSpec file to copy all of the files in the
/tmp/WordPress
folder on the development machine to the
/var/www/html/WordPress
folder on the target Amazon EC2 instance. During the
deployment, CodeDeploy runs the specified scripts as root
in the
/var/www/html/WordPress/scripts
folder on the target
Amazon EC2 instance at specified events during the deployment lifecycle, such as
BeforeInstall
and AfterInstall
. If any of
these scripts take longer than 300 seconds (5 minutes) to run, CodeDeploy stops the deployment and
marks the deployment as failed.
For more information about these settings, see the CodeDeploy AppSpec file reference.
Important
The locations and numbers of spaces between each of the items in this file are important. If the spacing is incorrect, CodeDeploy raises an error that might be difficult to debug. For more information, see AppSpec File spacing.