DEV Community

Joel Johnson
Joel Johnson

Posted on

Simple WordPress website on AWS using EC2 and RDS

The goal of this project is to build an accessible website for everyone using the cloud. I will use the following technologies to accomplish this task.

  1. WordPress - The most popular CMS on the internet.
  2. AWS EC2 - The server hosting the website.
  3. AWS RDS - The database host for the WordPress MySQL.

Creating the Database

On the AWS dashboard, search for RDS and continue to the RDS dashboard. Click on the 'Create database' button.

Click on 'Create database' button

Use the standard option and select MySQL database engine.

Pick database engine

I have selected the free tier template to avoid charges on my account. You can select any of the others depending on your use case. Note that this will affect the default configuration of your database instance.

Pick a sample template

In the settings section, enter the DB instance name and Master credentials.

Enter DB instance name and credentials

Select your database instance configuration.

DB instance configuration options

Select the storage type and size of the database instance.

DB storage config

Next, enter network and connectivity settings.

Network settings

In the additional configuration section, enter a database name. This automatically creates a database when the database instance is launched.
Additional configuration section

The final step is to click the 'Create database' button.
Complete RDS database creation

After your RDS instance has been created, you should then see it running.

Creating RDS DB instance

Provisioning the server

Good job! We will now create an EC2 instance that will host the WordPress application. All packages and dependencies required to get the CMS running and communicating to the RDS instance will also be installed. They are:

  1. Apache (httpd) web server
  2. PHP
  3. MySQL

Now, go to the EC2 instance dashboard and click the 'Launch instance' button. Enter your preferred server name.

Enter EC2 server name

Next, select your operating system image. I have used Amazon Linux 2 here. Note that for another AMI the installation process might be slightly different.

Select AMI

Now select the instance type. Since we are in the free tier, I have selected the eligible t2.micro.

Select Instance type

Create a key-pair. They are used for making secure connection from a client.

Create key-pair for SSH

Key-pair created!

Next, configure your network settings and security group information. I have only allowed SSH (port 22) and HTTP (port 80) to communicate to the server. And for the secure connection I have selected only my IP address to have access, which will be done using the key pair I created above. You can click on 'Edit' to make more changes, also security groups can always be modified later on.

Network settings

To provision the server, go to the 'Advanced details' section and modify the 'User Data' with a script that installs & starts an Apache server.
Advance details

Custom script to install and run Apache web server

User data script:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
Enter fullscreen mode Exit fullscreen mode

That is the last configuration for now. You can now launch the EC2 instance by clicking the 'Launch instance' button in the summary section.

Launch EC2 instance

You should now see the instance running.

EC2 instance running

Visit the EC2 public IP address and you should see a sample Apache page.

Apache web server works

RDS Connection Configurations

After setting up both the EC2 web server and RDS database instances. We now want both of them to communicate directly. We would do this by modifying the security group of the RDS instance.

Go back to the RDS instance page. Click on the WordPress database to view the details page. In the 'connectivity & security' tab, click the link to the default security group.

RDS instance details

Under 'Inbound rules' tab, select 'Edit inbound rules'

Select Edit inbound rules

Modify the only inbound rule to allow MYSQL/Aurora type connection. In the source, search for the security group of your EC2 instance and select it. This will allow inbound MySQL connection from the WordPress EC2 instance. Save the rules.

Modify inbound rule

EC2 MySQL configuration

To connect the EC2 instance, copy the public ipv4 address of the WordPress server in the EC2 instances page. You will also need a CLI tool and the key you previously created and downloaded. Remember that the server allows SSH connection from only your IP address, so as long as your IP address has not changed you should be fine. If it has, modify your EC2 instance inbound rule

I will be using Git Bash on Windows. Feel free to use any other command line tool.

Now connect to the WordPress locally using the following.
$ ssh -i wordpress-key.pem ec2-user@<ipv4 address>

Successful SSH connection to EC2 instance.

Next, install MySQL on the WordPress server.

$ sudo yum install -y mysql

When the installation is completed, modify the MySQL Host with the endpoint address of your RDS instance. Get the endpoint address from your RDS instance details page. It will look something like this.
$ export MYSQL_HOST=wordpress.cdjuc7xlsvu.us-east-1.rds.amazonaws.com

Remember the Master user name and password you created earlier, you'll need to use them to establish connect with the RDS wordpress database that was created earlier.
$ mysql --user=admin --password=adminadmin wordpress

Now create a normal database user with credentials that will have access to the wordpress database.

CREATE USER 'wordpress' IDENTIFIED BY 'wp12345';
GRANT ALL PRIVILEGES ON wordpress.* TO wordpress;
FLUSH PRIVILEGES;
Exit
Enter fullscreen mode Exit fullscreen mode

Note: Always use better passwords/credentials to secure your systems

Download and Configure WordPress

Download the latest WordPress package from the official source.
$ wget https://wordpress.org/latest.tar.gz

Uncompress the downloaded archive.
$ tar -xzf latest.tar.gz

This creates a wordpress directory, change directory to the wordpress directory and duplicate the config file.
$ cd wordpress
$ cp wp-config-sample.php wp-config.php

Next, use a text editor to modify the config file.
$ nano wp-config.php

Edit the default configuration with their respective values.

Update config file

Next, update the key and salts with unique values found here. Then save and exit the file.

Key and Salts configuration

Update Apache Web Server with WordPress files

Install WordPress PHP dependencies.
$ sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2

Copy WordPress files to Apache root directory.
$ sudo cp -r /home/ec2-user/wordpress/* /var/www/html/

That's it. Restart Apache service to update.
$ sudo service httpd restart

Reload the WordPress website. Also, do the preliminary setup to get the full experience.
Initial WordPress Setup page

WordPress Welcome page

Mission accomplished! What's next? Use this setup to host your website. Or terminate resources to avoid charges. Be aware that RDS snapshots may incur charges to your account.

Thanks for reading.

Top comments (0)