Deploying WordPress with Amazon EC2 (Elastic Compute Cloud) and Amazon RDS (Relational Database Service)
Tags: aws, amazonwebservices, wordpress, tutorial
Published: 2023-10-19 11:02:11

Deploying WordPress with Amazon EC2 (Elastic Compute Cloud) and Amazon RDS (Relational Database Service) involves setting up a web server (EC2 instance) to host WordPress and a database server (RDS instance) to store WordPress data. Here's a step-by-step guide on how to do it:

[Step By Step Tutorial](https://www.youtube.com/watch?v=pKOEzzuH-BY)

**Prerequisites:**
An AWS account with appropriate permissions.
Basic knowledge of AWS services.


**Step 1: Launch an EC2 Instance**
1.	Log in to the AWS Management Console.
2.	In the top right corner of the Amazon RDS console, select the Region in which you want to create the DB instance.
3.	Navigate to the EC2 dashboard.
4.	Choosing an Amazon Machine Image.
a. Click "Launch Instance" and select an Amazon Machine Image (AMI) based on your preferences (e.g., Amazon Linux 2, Ubuntu).

b. On the first page, enter wordpress app as your instance name.

c. Next, choose an Amazon Machine Image (AMI).

5.	Choose an instance type based on your requirements. A t2.micro instance is suitable for testing, but you may need a larger instance for production use.

6.	Configuring SSH Key.
a. Open the key pair (login) section and choose Create new key pair for your instance.

b. Give your key pair a name. Then choose the Create key pair button, which will download the .pem file to your machine.

7.	Configure a Network Settings. To configure this, select Allow SSH traffic from My IP and select Allow HTTP traffic from the internet.


8.	Review and launch the instance, creating or selecting an existing key pair for SSH access.

You have successfully launched your EC2 instance. Next, we will configure your Amazon RDS database to work with your EC2 instance.


**Step 2: CREATING A MYSQL DATABASE WITH AMAZON RDS**
Open the AWS Management Console. When the screen loads, enter RDS in the search bar, then select RDS to open the service console.

Choose the Create database button to get started.

The first step is to choose the database engine you want to use. Amazon RDS supports six different engines, from popular open-source options like MySQL and PostgreSQL, to commercial options like Oracle and Microsoft SQL Server, to a cloud-native option called Amazon Aurora that was custom-built to take advantage of the cloud. WordPress uses MySQL, so select Standard create for the database creation method and choose the MySQL engine.
Next, you will specify the authentication settings for your MySQL deployment. These include the database name and the master username and password. In the Settings section, enter wordpress as your DB instance identifier. 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 module.
Finally, Amazon RDS provides a number of additional configuration options to customize your deployment. You need to make one change in this area. Select the Additional configuration line to expand the options. Set the Initial database name to wordpress. This will ensure Amazon RDS creates the database in your MySQL instance upon initialization. You will use this database name when connecting to your database.

Choose the Create database button to create your database.

You have now created a fully managed MySQL database using Amazon RDS.
 


**Step 3: CONFIGURING YOUR AMAZON RDS DATABASE**
To configure this, go to the Amazon RDS databases page in the AWS console. Choose the MySQL database you created.

Scroll to the Connectivity & security tab in the display and choose the security group listed in VPC security groups. The console will take you to the security group configured for your database.

Select the Inbound rules tab, then choose the Edit inbound rules 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. Click on Add Rule. Change the Type property to MYSQL/Aurora, which will update the Protocol and Port range to the proper values. Then, in the Source put the Private IPv4 addresses of the EC2 instance.



**Step 4: Connect to the EC2 Instance**
Once the instance is running, use SSH to connect to it using the private key pair you specified during instance creation. Previously, you downloaded the .pem file for the key pair of your instance. Locate that file now. It will likely be in a Downloads folder on your desktop.

For Mac or Linux users:
Open a terminal window. If you are on a Mac, you can use the default Terminal program that is installed, or you can use your own terminal.
In your terminal, run the following commands to use SSH to connect to your instance. Replace the “” with the path to your file, e.g., “~/Downloads/wordpress.pem”, and the “” with the public IP address for your EC2 instance.

```
chmod 400 <path/to/pem/file>
ssh -i <path/to/pem/file> ec2-user@<public_IP_DNSAddress>
```

You should see the following in your terminal to indicate that you connected successfully:
 
Step 5: Install Required Software on EC2 to Configure WordPress
1.	Create a database user .
a. First, run the following command in your terminal to install a MySQL client to interact with the database.

```
sudo yum install -y mysql
```

b. Go to the Amazon RDS databases page in the AWS console. You should see the wordpress database you created for the WordPress installation. Select it to find the hostname for your Amazon RDS database.

c. In the details of your Amazon 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 “” 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 “” and “” with the master username and password you configured when creating your Amazon RDS database.

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

Finally, create a database user for your WordPress application and give the user 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
```

As a best practice, you should use a better password than wordpress-pass to secure your database.
2.	Installing Apache Web Server
a. To install Apache on your EC2 instance, run the following command in your terminal:

```
sudo yum install -y httpd
```

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

```
sudo service httpd start
```

Go to the EC2 Instances page and find your instance. In the Description below, find the Public IPv4 DNS of your instance.


3.	Download and Configure Wordpress In this step, you will download the WordPress software and set up the configuration.
a. First, 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
```

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

 [ec2-user@~]$ ls
 latest.tar.gz wordpress
b. Change the directory to 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.
nano 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 this case admin.
 DB_PASSWORD: The password for the user (admin) you created.
 DB_HOST: The hostname of the database that you found in the endpoint of the RDS.
4.	Deploy Wordpress
a. First, install the application dependencies you need for WordPress. In your terminal, run the following command.
sudo amazon-linux-extras install -y mariadb10.5 php8.2

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

```
cd /home/ec2-user
```

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

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

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

```
sudo service httpd restart
```

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

That’s it. You have a live, publicly accessible WordPress installation using a fully managed MySQL database on Amazon RDS.
[Step By Step Tutorial](https://www.youtube.com/watch?v=pKOEzzuH-BY)