Deploying WordPress on AWS EC2: A Beginner’s Guide

Deploying WordPress on AWS EC2: A Beginner’s Guide

Are you looking to host your WordPress website on the cloud but don’t know where to start? In this beginner’s guide, we will walk you through the step-by-step process of deploying WordPress on an Amazon EC2 (Elastic Compute Cloud) instance, giving you the freedom and scalability of cloud hosting. This is a beginner-level project, and we will configure MySQL on the same EC2 instance for simplicity. However, keep in mind that as you gain experience, you can explore more advanced configurations and cloud services. Let’s dive in!

Prerequisites:

Before we begin, make sure you have the following prerequisites in place:

  1. AWS Account: Sign up for an AWS account if you haven’t already.
  2. Domain Name: Purchase a domain name from a domain provider like get.tech.
  3. Basic Knowledge: Familiarize yourself with basic Linux commands and server configurations.

Step 1: Launching the EC2 Instance

Login to your AWS account and head over to the EC2 Dashboard.

Click on ‘Launch Instances’ and choose the ‘Ubuntu’ machine with the 22.04 edition.

Select ‘t2.micro’ as the instance type and add your PEM file (optional).

Configure the Security Group by opening ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).

Click on ‘Launch instance’ to create the instance.

Step 2: Allocating Elastic IP

Click on ‘Allocate Elastic IP address’ to assign a static public IP to your instance.

Without any additional configurations, click on ‘Allocate’.

Associate the Elastic IP with your EC2 instance by clicking on ‘Actions’ and selecting ‘Associate Elastic IP address’.

Confirm that the Elastic IP has been attached by checking the ‘public IP’ in the EC2 dashboard.

Step 3: Configuring the EC2 Instance

Connect to your EC2 instance using ‘EC2 Instance Connect’ or an SSH client with the PEM file.

Install the Apache2 web server using the following commands:

sudo apt-get update
sudo apt install apache2
sudo systemctl enable apache2
sudo systemctl restart apache2

Verify if the server is running by entering the public IP address in your browser.

Step 4: Installing PHP and MySQL

Install PHP and MySQL database on the EC2 server with the following commands:

sudo apt install php libapache2-mod-php php-mysql
sudo apt install mysql-server

Log in to the MySQL database as the root user:

sudo mysql -u root

Set a password for the root account and create a new user and database:

ALTER USER 'root'@’localhost' IDENTIFIED WITH mysql_native_password by 'MySQL-1234';
CREATE USER 'aman_new'@localhost IDENTIFIED BY 'Aman@123DevOps';
CREATE DATABASE aman;
GRANT ALL PRIVILEGES ON aman.* TO 'aman_new'@localhost;

Output of the above commands

Step 5: Installing WordPress

Download and install WordPress using the following commands:

sudo wget wordpress.org/latest.zip
sudo apt-get install unzip
unzip latest.zip
mv wordpress/ /var/www/html/

Access your WordPress site by browsing the public IP/WordPress.

Fill in the database details in the setup wizard using the database credentials created earlier.

Now, you have to write the wp-config. php file.

Copy the content.

We have to create a wp-config.php file inside the /var/www/html/wordpress/ directory and paste the above content into the wp-config.php file. To do that, follow the below commands:

cd /var/www/html/wordpress
sudo vim wp-config.php

Paste the above configuration which is given in the above screenshot and come back to the same screenshot and click on “Run the installation”.

Enter the details accordingly.

Now, WordPress needs to be configured, Enter the details according to the below screenshot, and remember the password to log in again.

WordPress is installed now! So click on “Log In”.

Now, the new window will ask you for your username and password. You can skip it by opening the incognito tab of your browser and paste “/wordpress.

Step 6: Configuring Domain Name

To avoid entering the public IP address for accessing WordPress, edit the “sites-available” file in “/etc/apache2/” and change the Document Root to “/var/www/html/wordpress”:

cd /etc/apache2/sites-available/
sudo vim 000-default.conf

Restart the Apache service:

sudo systemctl restart apache2

Now, we need to add the domain name to our WordPress application.

To do that, there is one Prerequisite in which you must have one domain name.

I have one domain that is purchased from get.tech domain provider.

Now Go to the ‘Route53’ Service in AWS and click on ‘Create hosted zone’.

Write your respective domain name Type should be ‘Public hosted zone’ and click on ‘Created hosted zone’.

You will get two Records in your hosted zone.

Now, we have to create two more records.

Click on ‘Create record select ‘Simple routing’ and click on ‘Next’.

Click on ‘Define simple word’.

Create the first simple record

  • No need to add a subdomain
  • The record type should be A type
  • Value/Route traffic as IP Address
  • Add the Public IP of the instance and click on ‘Define simple record’.

The record will be like this.

Now, create a second simple record.

  • Add ‘www’ in the subdomain
  • The record type should be CNAME
  • Value/Route traffic will be ‘your domain name’ only.

We have created both records. Don’t consider ‘www’ in the second ‘Value/Route traffic to’.

To follow the proper things with the help of video, kindly refer to the below link

How to connect Godaddy Domain Name with AWS Route 53 | EC2 instance

Now, we have to give the name to our Server that will be the same as our domain name. To do that,

  • cd /etc/apache2/sites-available/
  • sudo vim 000-default.conf

and add your domain in the ServerName and add ‘www’ as prefix in the ServerAlias and write the domain name as it is.

sudo systemctl restart apache2

Now, add the domain name by going to the WordPress admin.

Step 7: Secure the Connection for HTTP and HTTPS

Now, we need to secure the connection for HTTP and HTTPS.

Run the following commands on your web server

sudo apt install certbot python3-certbot-apache
sudo certbot --apache

Here, you can see the connection has been secured for the web server.

Conclusion:

Congratulations! You have successfully deployed WordPress on an AWS EC2 instance, configuring MySQL on the same server for simplicity. As a beginner, this project provides an excellent introduction to cloud hosting and basic server configurations. As you progress, you can explore more advanced setups, including separating the database and web server, auto-scaling, and using managed database services. Cloud computing offers a wide array of possibilities, and this is just the beginning of your cloud journey. Happy exploring!

Keep exploring, keep innovating, and keep building amazing things!

GitHub Profile: https://github.com/AmanPathak-DevOps

Follow on LinkedIn to stay up to date with the latest developments.

Happy Learning!

Aman Pathak