Simple WordPress website on AWS using EC2 and RDS
Tags: aws, devops, wordpress, beginners
Published: 2023-02-17 15:37:06

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b68w3t6n46c46jqt7hd8.png)

Use the standard option and select MySQL database engine.

![Pick database engine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aur07o7aq6macf2lfw9n.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhrsd1hzh2jty3goy3op.png)

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

![Enter DB instance name and credentials](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0iflpc4ozxsmig1hl4ey.png)

Select your database instance configuration.

![DB instance configuration options](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rh7j5chwuyhizctzca08.png)

Select the storage type and size of the database instance.

![DB storage config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5xecks9ktslawjtc4mr.png)

Next, enter network and connectivity settings.

![Network settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhe7wbxjdnc0thapqeb9.png)

In the additional configuration section, enter a database name. This automatically creates a database when the database instance is launched.
![Additional configuration section](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7utkk3fg2qr1qfdt3ltd.png)

The final step is to click the 'Create database' button.
![Complete RDS database creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1w7qdm2rx3mm91sc6x3s.png)

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

![Creating RDS DB instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w753ts58wyzx85l136j3.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3btchj59931qw4u29qwu.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfydb9e5szj5lo5yaoeb.png)

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

![Select Instance type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3qspa0xfevol2ej73yr.png)

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

![Create key-pair for SSH](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rt7r21zayfl5jvcwqf88.png)

![Key-pair created!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2cbjvro5xhejipciig6.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21d5okuhouoo2e1ecy5p.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqn0fclpq3ugiwcprkj5.png)

![Custom script to install and run Apache web server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r85i8864fzjjeuzhm9ap.png)

####User data script:
```
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0x4u7i5e7gt7nh18amr0.png)

You should now see the instance running.

![EC2 instance running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sk2iv5lcwe00811xhobw.png)

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


![Apache web server works](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx87e4cds21i8k1m6c5d.png) 


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/klqprqpi3je75l6pp8ih.png)

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

![Select Edit inbound rules](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1g4ri27giszbc7zzxxv.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2b9owabmi0d0yvxqyvyj.png)


## 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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6czc8gfnfhs0iqdbpp4.png)

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
```

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yis11a4o9qpzjiamsxx7.png)

Next, update the key and salts with unique values found [here](https://api.wordpress.org/secret-key/1.1/salt/). Then save and exit the file.

![Key and Salts configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndwlrjd963rskj8ctu0x.png)


##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4245pbuikmfi8rvt7zf3.png)

![WordPress Welcome page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7d00iidkv47pio44eu49.png)

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.