Run WordPress on AWS EC2 with Ease

Getting a new site running has never been easier thanks to platforms like WordPress. In this guide, we will walk you through deploying WordPress on AWS using EC2 and Docker. By the end, you will have a WordPress site running smoothly on reliable AWS infrastructure.

Prerequisite: I am using Debian on my host machine.

1. Setting Up Your EC2 Instance:

  • Launch: Go to the EC2 dashboard and click “Launch Instance”.
  • Name Your Instance: For example, “wordpress”.
  • Choose an Instance Type: I recommend t3.small after some testing. It is ideal for beginners.
  • Key Pair: Create a new one and save the CSV file. We will need it for SSH access later.
  • Network Settings: Choose your VPC. If you have a private subnet configured, select it.
  • Firewall: Allow SSH and HTTP traffic from anywhere. Other settings can remain at their defaults.

2. Installing Docker and Docker Compose:

First, make sure your PEM file permissions are configured correctly:

$ chmod 400 <myPemFile>

SSH into your new EC2 instance:

$ ssh -i "<myPemFile>.pem" ec2-user@<publicIPorDNS>

Update packages, install Docker, and add ec2-user to the Docker group:

$ sudo yum update
$ sudo yum install docker
$ sudo usermod -a -G docker ec2-user

For Docker Compose:

$ sudo yum install python3-pip
$ sudo pip3 install docker-compose

Enable Docker on boot:

$ systemctl enable docker.service
$ systemctl start docker.service

Verify installations:

$ docker version
$ docker-compose version

3. Deploying WordPress:

Create a directory for the project:

mkdir myWordpressSite

Inside the directory, set up your docker-compose file.

version: '3.1'
services:
  wordpress:
    image: wordpress
    restart: always
    ports:
      - 80:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress:
  db:

Then start your services:

docker-compose up -d

Visit http://your-ec2-dns-or-public-ip and follow the WordPress setup.

4. Create an ACM Certificate:

To serve your site properly on 443, we need a certificate. You can do this in AWS using the ACM service.

  • Go to ACM > Certificates.
  • Click “Request”.
  • Certificate type: public certificate.
  • For the fully qualified domain name, add your domain, for example: mysite.com
  • Leave other options as default.
  • Click the create certificate button.
  • Then select your certificate and click “Create records in Route 53”. This will automatically create validation records in Route 53.

5. Create a Target Group:

We need to create a target group pointing to the new service running on the instance we created earlier. Go to the EC2 Console > Target Groups:

  • Click “Create target group”.
  • Select target type: “instances”.
  • Add a name, for example: MySiteTG
  • Leave protocol at the default: HTTP, port 80
  • Click Next Select your instance on port 80 and click “Create target group”.

6. Create a Load Balancer:

Now we can create an ALB. Go to EC2 > Load Balancers.

  • Click “Create load balancer”.
  • Select “Application Load Balancer”.
  • Choose a name, for example, MySiteLB
  • Scheme: Internet-facing
  • Network mapping: make sure you select the correct AZ where your instance is running.
  • Security group: ensure it is open on HTTPS 443 and HTTP 80.
  • Listeners and routing: Select HTTP, port 80. Forward to: MySiteTG (the target group created earlier).
  • Listeners and routing: Select HTTPS, port 443. Forward to: MySiteTG and select the certificate you created.
  • Click “Create load balancer”.

7. Create an A Record in Route 53:

For this, we assume you already have a hosted zone for your site and a domain.

  • Go to Route 53 > Hosted Zones > yoursite.com
  • Click create record, then choose “Simple routing”.
  • Choose record type A
  • In Value/Route traffic, select the load balancer we created earlier.

Now, when you access your domain, you should see your WordPress site.

Need a custom ecommerce solution? At Innoquanta, we can help you plan and build it.

Need AWS delivery help? See our AWS DevOps consulting services.