Launching WordPress in EC2 with RDS backend

Satyam Singh
5 min readJul 8, 2021

Objective

Launch WordPress on Amazon EC2 Instance with MySQL in AWS RDS as a backend.

Content of this Blog

  • Prerequisite
  • Part 1 : Creation of EC2 Instance
  • Part 2 : Creation of MySQL Database with RDS
  • Part 3 : Allowing EC2 Instance to Access RDS Database
  • Part 4 : Configuration of WordPress on EC2 Instance
  • WordPress Output

Prerequisites

  • For WordPress, refer the following link:
  • For MySQL, refer the following link:
  • For EC2 & RDS AWS Service, click here

Part 1 : Creation of EC2 Instance

Go to EC2 > Instances > Launch instances

  • Amazon Machine Image Selection: Select AMI for the EC2 Instance (Amazon Linux 2 AMI in this case)
  • Instance Type Selection: Select Instance Type for the EC2 Instance (t2.micro in this case)
  • VPC Selection: Select VPC for the EC2 Instance to be launched within (default VPC in this case)
  • Addition of Tags(Optional): Addition of tags in form of key-value pair for easier identification of tags(Key: Name, Value: wordpress-ec2 in this case)
  • Security Group Configuration: Configure Security Group to allow SSH traffic from system’s IP and HTTP traffic from anywhere(wordpress-sg is the name in this case)
  • Launching EC2 Instance with generation of SSH Key: After configuration of Security Group, it provides the overall configuration to be reviewed. After clicking on “Launch”, it asks for using an existing key or generating a new key. In this case, new key would be generated.
Click on Launch
Generation of new key i.e., “wordpress” and then click on Launch Instances

Part 2 : Creation of MySQL Database with RDS

Go to RDS > Databases > Create database

  • Step 1 : Select Database creation method and Engine options
  • Step 2 : Select MySQL version(not present in the image below), Templates as per use case. Specify the DB Instance identifier, Master username and Master password.
  • Step 3 : Mention the VPC to launch RDS Instance within, Subnet group, VPC security group(if it exists) or create new security group by selecting the respective option. In this case, new security group would be created. Availability Zone preference could also be mentioned as well.
  • Step 4 : Additional configuration like generation of initial database could be done while setting up RDS itself.

Part 3 : Allowing EC2 Instance to Access RDS Instance

In order to use RDS Instance as a backend to the EC2 Instance, access needs to be provided for the same.

  • Step 1: Click on the respective RDS Instance and then click on the corresponding VPC security group.
  • Step 2 : Click on “Edit inbound rules” in the security group created in the previous part i.e., mysql-sg.
  • Step 3 : Add a new rule that allows the access of EC2 Instance to the RDS Instance by mentioning the corresponding security group as the Source.
RDS Security Group after configuration

Part 4 : Configuration of WordPress on EC2 Instance

  • SSH to the EC2 Instance: Access the EC2 Instance via SSH using the command mentioned below:
ssh -i <path/to/pem/file> ec2-user@<publicIpAddress>
After executing the above command in the terminal
  • Apache Httpd server setup: Use the second and third command mentioned below to setup Apache Httpd(Switch to the root user before by using the first command mentioned below)
sudo su - rootyum install httpd -y
systemctl enable httpd --now
Use the Instance’s Public IP to check if Apache Httpd server has been setup properly
  • WordPress Installation in EC2 Instance: Use the commands below to install WordPress and its dependencies in the EC2 Instance.
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
# WordPress dependency
amazon-linux-extras install -y php7.2
  • WordPress Configuration for enabling usage of MySQL in RDS Instance as a backend: Mention the respective changes in the wp-config.php file as shown below. Copy the entire wordpress directory to the Document Root of Apache Httpd server and restart the aforementioned server.
cd wordpress
cp wp-config-sample.php wp-config.php
vi wp-config.php
cd /root
cp -r wordpress/* /var/www/html/
systemctl restart httpd

Changes to be made in the wp-config.php file 👇

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpressdb' );
/** MySQL database username */
define( 'DB_USER', 'admin' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password' );
/** MySQL hostname */
define( 'DB_HOST', 'wordpress.c7xwcuxl56is.ap-south-1.rds.amazonaws.com' );
DB_HOST in wp-config.php could be obtained by using the RDS Instance Endpoint

WordPress Output

Access the Public IP or Hostname of the EC2 Instance, the output mentioned below appears

--

--