WordPress with AWS RDS

Shobhit Singh Pal
7 min readMar 9, 2021

To setup the WordPress with AWS RDS you are going to implement the following steps:

  • Create an AWS EC2 instance
  • Configure the instance with Apache Webserver.
  • Download php application name “WordPress”.
  • As WordPress stores data at the backend in MySQL
    Database server. Therefore, you need to setup a
    MySQL server using AWS RDS service using Free Tier.
  • Provide the endpoint/connection string to the
    WordPress application to make it work.

Step 1:Create an AWS EC2 instance

Click on the launch instances button:

Choose amazon AMI:

Choose the t2.micro instance type:

Keep the instance details and add storage steps default by clicking on next.

I add tag to the ec2 instance as follow:

Configured the security group as follow:

The ssh to login into the ec2 instance and the http for accessing the WordPress.

Launch and get an SSH key.

Step 2:Configure the instance with Apache Webserver

Install the apache server i.e. HTTPD on amazon AMI ec2 instance:

Started the HTTPD service:

Step 3: Download php application name “WordPress”.

Download and uncompress the software by running the following commands in your terminal:

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

x — Extract files

z — The file is a “gzipped” file

f — Use the following tar archive for the operation

Step 4: As WordPress stores data at the backend in MySQL Database server. Therefore, you need to setup a MySQL server using AWS RDS service using Free Tier.

In the amazon RDS console click on Create database:

Tick on MySQL database:

User free template to create amazon RDS instance:

In the Settings section, enter wordpress as your DB instance identifier, you can give any name. Then specify the master username and password for your database. Choose a strong, secure password to help protect your database. Store the username and password for safekeeping as you will need it in a later step.

Configure the DB instance size and its storage as default:

Keeping connectivity settings also as default

Click on the below Additional configuration:

Give the initial database name as wordpress:

AWS will show you estimated monthly costs for your RDS database. If you are still eligible for the Amazon RDS Free Tier, you will see a note that the database will be free to you for up to 12 months.

Click the orange Create database button to create your database.

You should see a success notice indicating that your database is being created.

Allow your EC2 instance to access your RDS database

you created security group rules to allow SSH and HTTP traffic to your WordPress EC2 instance. The same principle applies here. This time, you want to allow certain traffic from your EC2 instance into your RDS database

To configure this, go to the RDS databases in the AWS console. Click on the MySQL database you created in the current step.

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

The console will take you to the security group configured for your database. Click the Inbound tab, then click the Edit button to change the rules for your security group.

The default security group has a rule that allows all inbound traffic from other instances in the default security group. However, since your WordPress EC2 instance is not in that security group, it will not have access to the RDS database.

Change the Type property to MYSQL/Aurora, which will update the Protocol and Port Range to the proper values.

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.

Creating a database user

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

sudo yum install -y mysql

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

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

export MYSQL_HOST=<your-endpoint>

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

mysql --user=<user> --password=<password> wordpress

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

Finally, create a database user for your WordPress application and give it permission to access the “wordpress” database.

Run the following commands in your terminal:

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

You should use a better password than “wordpress-pass” to secure your database.

Step 5: Provide the endpoint/connection string to the WordPress application to make it work.

Configure the WordPress

If you run “ls” to view the contents of your directory, you 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

Then, open the wp-config.php file using the nano editor by running the following command.

vim wp-config.php

You need to edit two areas of configuration.

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 you created in the database in the previous module

● DB_PASSWORD: The password for the user you created in the previous module.

● DB_HOST: The hostname of the database that you found in the previous module.

Deploying WordPress

First, install the application dependencies you need for WordPress. In your terminal, run the following command.

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

Second, change to the proper directory by running the following command:

cd /home/ec2-user

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

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

Finally, restart the Apache web server to pick up the changes.

sudo systemctl restart httpd

You should see the WordPress welcome page and the five-minute installation process.

That’s it. you have done what you committed before.

--

--