Integration of Amazon Relational Database Service (RDS) with WordPress on AWS

Simran Shrivas
9 min readJan 14, 2021

Hello Learner 😊

Here is one more article to show how we can deploy WordPress with Amazon RDS.

So start with the Introduction😇

Amazon Web Services (AWS) is a secure cloud services platform, offering computing power, database storage, content delivery, and other functionality to help businesses scale and grow.

In simple words AWS allows you to do the following things-

  1. Running web and application servers in the cloud to host dynamic websites.
  2. Securely store all your files on the cloud so you can access them from anywhere.
  3. Using managed databases like MySQL, PostgreSQL, Oracle, or SQL Server to store information.
  4. Deliver static and dynamic files quickly around the world using a Content Delivery Network (CDN).
  5. Send bulk emails to your customers.

Amazon RDS

Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching, and backups.

Amazon RDS is available on several database instance types — optimized for memory, performance, or I/O — and provides you with six familiar database engines to choose from, including Amazon Aurora, PostgreSQL, MySQL, MariaDB, Oracle Database, and SQL Server. You can use the AWS Database Migration Service to easily migrate or replicate your existing databases to Amazon RDS.

WordPress is the simplest, most popular way to create your own website or blog. In fact, WordPress powers over 39.5% of all the websites on the Internet. Yes — more than one in four websites

Why use Amazon RDS for your WordPress database?

Many installation guides for WordPress use a MySQL database that is on the same server as the WordPress installation. While this may be sufficient to start, there are several reasons you may not want your MySQL database on the same server as your WordPress installation:

● MySQL and WordPress will be competing for computing resources on the same server, potentially hurting your site’s performance.

● You are unable to horizontally scale WordPress by adding additional WordPress servers as your site becomes more popular.

● You are responsible for all database maintenance tasks, including database backups and security upgrades.

The result is that WordPress makes building a website accessible to anyone — even people who aren’t developers.

🔰Task Description📄

🎯 Create an AWS EC2 instance

🎯 Configure the instance with Apache Webserver.

🎯 Download PHP application name “WordPress”.

🎯 As WordPress stores data at the backend in the MySQL database server. Therefore, you need to set up a MySQL server using AWS RDS service using Free Tier.

🎯 Provide the endpoint/connection string to the WordPress application to make it work.

So lets start the task

Step 1: Creating an EC2 instance

In this step, I will create an Amazon EC2 instance to run the WordPress site.

For more discussion about how we can launch EC2-instance using AWS CLI go to this link Integration with the AWS Services using CLI.

To create EC2 instance, go to Amazon EC2 in the AWS console follow the below steps shown in the screenshots

AWS EC2 Instance
Choose AMI
Instance Type

After selecting the t2.micro instance, click the blue Review and Launch button to skip some of the advanced configuration steps.

But you need to configure one more thing before launching your instance.

Configuring a security group in EC2 instance

To configure this, click the Edit security groups link on the review page.

  • SSH traffic from current IP address so I can use the SSH protocol to log into the EC2 instance and configure WordPress;
  • HTTP traffic from all IP addresses so that users can view the WordPress site
  • Once you have the security group rules in place, give your security group a name I name “WordPress” so that it will be easy to find.
  • Once you’ve named it, click the Review and Launch button

Once you have the security group rules in place, give your security group a name I name “WordPress” so that it will be easy to find.

Once you’ve named it, click the Review and Launch button

Successfully created EC2 instance

Step2 Creating a MySQL database with RDS.

WordPress uses MySQL, so select that engine now. Below the screenshot show creation of the database.

Create Database

Now select the database and follow all the steps shown in the screenshot:

MySQL
Create Username and Password for Database
Successfully Create Database

Configuring RDS database

At this point, I have to create an RDS database and an EC2 instance.

Step 2. Allow EC2 instance to access RDS database

In the previous step, create security group rules to allow SSH and HTTP traffic to your WordPress EC2 instance. The same principle applies here. This time, I want to allow certain traffic from your EC2 instance into your RDS database.

For this follow the steps below the screenshots;

Select the WordPress database

Go to the Connectivity & security tab in the display, and click on the security group listed in VPC security groups.

Security Group

However, since the WordPress EC2 instance is not in that security group, it will not have access to the RDS database.

Select default security group and create inbound rules we have to configure MYSQL/Aurora rule to EC2 instance security group then remove the current security group value configured for the rule, and type “WordPress” instead. The console will show the available security groups that are configured.

Successfully Created Rules

Step 3. Creating a database user

Connect to the terminal

First, run the following command in the terminal to install a MySQL client to interact with the database.

sudo yum install -y mysql
MySQL installed

Next, find the hostname for the RDS database in the AWS console. In the details of the RDS database, the hostname will be shown as the Endpoint in the Connectivity & security section.

endpoint

In the terminal, enter the following command to set an environment variable for the MySQL host. Be sure to replace “<your-endpoint>” with the hostname of the RDS instance.

export MYSQL_HOST=<endpoint>

Next, run the following command in the terminal to connect to the MySQL database. Replace “<user>” and “<password>” with the master username and password configured when creating the RDS database.

mysql --user=<user> --password=<password> wordpress1

If you connected successfully, the terminal should indicate the connection to the MySQL database as shown in the following image.

Finally, create a database user for the WordPress application and permit it to access the “wordpress1” database.

Run the following commands in the terminal:

CREATE USER 'wordpress1' IDENTIFIED BY 'pass';
GRANT ALL PRIVILEGES ON wordpress1.* TO wordpress;
FLUSH PRIVILEGES;
Exit
User create

Now the main task started

Step 4. Configuring WordPress on EC2

🎯 Configure the instance with Apache Webserver.

To install Apache on EC2 instance, run the following command in the terminal:

sudo yum install -y httpd

To start the Apache web server, run the following command in your terminal:

systemctl start httpd

see that the Apache web server is working and that your security groups are configured correctly by visiting the public DNS of the EC2 instance in your browser.

Successfully installed httpd

.

🎯 Download PHP application name “WordPress”.

First, download and uncompress the software by running the following commands in the terminal:

wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz

I run “ls” to view the contents of the directory, will see a tar file and a directory called WordPress with the uncompressed contents.

$ ls
latest.tar.gz wordpress

Change into the WordPress directory and create a copy of the default config file using the following commands:

cd wordpress
cp wp-config-sample.php wp-config.php
output

Then, open the wp-config.php file using the vim editor.

First, edit the database configuration by changing the following lines:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

The values should be:

● DB_NAME: “WordPress”

● DB_USER: The name of the user

● DB_PASSWORD: The password for the user-created

● DB_HOST: The hostname of the database.

The second configuration section that needs to configure is the Authentication Unique Keys and Salts. It looks as follows in the configuration file:

/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define( 'AUTH_KEY', 'put your unique phrase here' );
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' );
define( 'LOGGED_IN_KEY', 'put your unique phrase here' );
define( 'NONCE_KEY', 'put your unique phrase here' );
define( 'AUTH_SALT', 'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT', 'put your unique phrase here' );
define( 'NONCE_SALT', 'put your unique phrase here' );

Go to this link to generate values for this configuration section. I can replace the entire content in that section with the content from the link.

save the file

Step 5: Deploying WordPress

In this step, the Apache webserver handles requests for WordPress.

First, install the application dependencies need for WordPress.

use below commands

sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2

Then, copy the WordPress application files into the /var/www/html directory used by Apache.

sudo cp -r wordpress/* /var/www/html/

Finally, restart the Apache webserver to pick up the changes.

Successfully see the WordPress welcome page

🎯 Provide the endpoint/connection string to the WordPress application to make it work.

Hope you understand this article

Thanks for Reading !!📃

🔰 Keep Learning !! Keep Sharing !! 🔰

--

--