Integrating Relation Database Service (RDS) with WordPress Server on AWS

Pradeep Kumar
7 min readJan 21, 2021

Hello connections…

In this article we will get to know how to integrate the Relational Database Service (RDS) with a WordPress server running on EC2 instance of AWS

Lets start

Step 1: Creating an EC2 instance

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

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

Step 2: 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 3: 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 4: 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 5: 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 6: 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 !!!

--

--