In this guide, we will walk through setting up a WordPress website on a Linux EC2 instance attached to an EBS volume to back up our data.
Objectives
Launch EC2 instance with user data to pre-install docker and docker-compose
Create an EBS volume and attach it to the instance
Mount EBS volume to ec2 instance
Run WordPress website with Docker-compose
Procedure
1) Launch EC2 instance with docker
In your AWS console, navigate to the ec2 dashboard and launch an instance with the following configurations.
Give it any name of your choice
Select Amazon Linux AMI
Create or Select an existing Key Pair as we will need this to connect to our instance later on
Select a VPC and subnet taking note of the AZ(Availability Zone) as our EBS volume needs to be in the same AZ with the EC2 instance for them to work.
Create or Select an existing Security Group making sure it allows HTTP, HTTPS, and SSH traffic from the internet.
Scroll down to the User Data section and provide the following script which will install docker and docker-compose for us.
#!/bin/sh
sudo su
yum update -y
yum install docker -y
service docker start
usermod -a -G docker ec2-user
curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
Wait till you get the 2/2 checks.
Connect to your instance using SSH and run the command below to make sure docker-compose is installed successfully.
docker-compose version
2) Create and Attach EBS Volume
In the EC2 dashboard, scroll down to where you see "Storage" and click on "Volumes".
You should be brought to the page where you see an orange button that says "Create New Volume"
Click on the button and in the wizard, make sure to select the same Availability Zone that your EC2 instance is in. If you don't do so, you won't be able to attach it to the instance.
Once the volume is done creating, attach it to the EC2 instance you created earlier.
3) Mount EBS Volume
Now that we have created our EC2 instance and EBS volume, we are almost halfway done with this project. So let's continue by mounting the volume in our EC2 instance following the steps below.
- SSH into your EC2 instance and use the command below to run as the root user. We need to run as root users as it will help us go faster.
#switch to root user
sudo su
- Use the following command to list all the block devices attached to your instance and copy the name of your newly attached EBS Volume.
# list all block devices and
# copy the name of the attached ebs volume
lsblk
- The commands below will let you mount the XFS filesystem to the EBS volume and make sure you replace "volume_name" with the name of your EBS volume
# mount filesystem to ebs volume
# insert the name of your volume where is says [volume_name]
mkfs -t xfs /dev/[volume_name]
#view the mounted file system
file -s /dev/[volume_name]
- Create a directory and mount the volume to that directory using the commands below.
# create directory and mount ebs volume to that directory
mkdir -p /mnt/ebs
mount /dev/[volume_name] /mnt/ebs
- Now you can list all the devices that are mounted to your instance using the command below.
# list all the mounted devices
df -h
4) Set Up WordPress with Docker-compose file
Create a folder in your home directory and inside that folder, create a file called
docker-compose.yml
.Use your preferred text editor (like Nano or Vim) to edit the
docker-compose.yml
file then copy and paste the following text into it. This is the file that docker-compose will use to automatically set up and run WordPress and MySQL for us.
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /mnt/ebs/db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: PASSWORD1
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: PASSWORD1
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- /mnt/ebs/wordpress:/var/www/html
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: PASSWORD1
WORDPRESS_DB_NAME: wordpress
Once you're done, save the changes and close the file.
In your terminal, run the following command to start the services. Make sure you are in the same directory that the
docker-compose.yml
file is in.
docker-compose up -d
Once all that is done, copy the IP address of your instance and go to port
:80
to test if everything is working.If you see the WordPress installation screen, Congratulations👏🎉