https://github.com/julien-muke/host-wordpress-aws-ec2-rds-part1
Deploy WordPress to an Amazon EC2
https://github.com/julien-muke/host-wordpress-aws-ec2-rds-part1
aws-alb aws-ec2 aws-rds aws-route53 wordpress
Last synced: over 1 year ago
JSON representation
Deploy WordPress to an Amazon EC2
- Host: GitHub
- URL: https://github.com/julien-muke/host-wordpress-aws-ec2-rds-part1
- Owner: julien-muke
- Created: 2024-07-11T14:34:18.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-01T13:22:54.000Z (over 1 year ago)
- Last Synced: 2025-02-01T14:25:03.939Z (over 1 year ago)
- Topics: aws-alb, aws-ec2, aws-rds, aws-route53, wordpress
- Homepage: https://julienmuke.blog/
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#  How to Deploy WordPress Website on AWS using EC2, RDS, ALB and more (Part1).
How to Deploy WordPress Website on AWS using EC2
Build this hands-on demo step by step with my detailed tutorial on Julien Muke YouTube. Feel free to subscribe 🔔!
## 🚨 Tutorial
This repository contains the steps corresponding to an in-depth tutorial available on our YouTube
channel, Julien Muke.
If you prefer visual learning, this is the perfect resource for you. Follow my tutorial to learn how to build projects
like these step-by-step in a beginner-friendly manner!
Deploying a WordPress website on AWS involves leveraging various AWS services to ensure scalability, reliability, and security. In this guide, we will walk you through the process of setting up a WordPress site using Amazon EC2 for hosting, Amazon RDS for database management, and Amazon Application Load Balancer (ALB) for efficient traffic distribution.
Additionally, we will utilize Amazon Route 53 for domain name management and DNS routing. This comprehensive approach not only simplifies the deployment process but also equips your WordPress site with the robust infrastructure needed to handle varying levels of traffic, ensuring optimal performance and uptime.
## 📐 Architecture Diagram Overview
* Users will request to open WordPress website, that request will be received by Route 53 which is a domain Management Service in AWS.
* We will use Route 53 to host DNS entries of the website's domain.
* Route 53 will send request to Application Load Balancer (ALB), it handles distribution of the traffic, if you have multiple instances of the same website, it will handle all the incoming requests.
* ALB also support SSL certificate through AWS Certificate Manager, we will use it to issue a new SSL certificate for our domain name and ALB will apply that SSL certificate and send request to EC2 instance.
* EC2 instance is a virtual server where we will install all the needed packages to run WordPress and create files of our WordPress website.
* We will make EC2 and RDS accessible to public source and enforce security via Security Group rules.
* We will create an EC2 instance which will be our Virtual Server and RDS instance which will be used for Database Hosting.
- Amazon EC2
- Amazon Application Load Balancer (ALB)
- Amazon RDS
- Amazon Route 53
- AWS Certificate Manager
- WordPress
## ☑️ Steps
The procedure for deploying this architecture on AWS consists of the following steps:
* Step 1. [Create EC2 for WordPress](#create-ec2-for-wordpress)
* Step 2. [Connect to EC2 instance via Instance connect](#connect-ec2-to-instance)
* Step 3. [Connect to EC2 instance via SSH client (local Terminal)](#connect-ec2-to-ssh)
* Step 4. [Install Apache on Amazon Linux 2](#install-apache)
* Step 5. [Install PHP & MariaDB on Amazon Linux 2](#install-php-mariadb)
* Step 6. [Download the latest WordPress zip file](#download-wordpress)
* Step 7. [Create RDS instance for WordPress website](#rds-wp)
* Step 8. [Connect RDS and EC2 instance](#connect-rds-ec2)
* Step 9. [WordPress Installation on Amazon Linux 2 EC2 instance](#install-wp-on-ec2)
## ➡️ Step 1 - Create EC2 for WordPress
You can launch an EC2 instance using the AWS Management Console as described in the following procedure.
To launch an instance:
1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
2. From the EC2 console dashboard, in the Launch instance pane, choose Launch instance.

3. Under Name and tags, for Name, enter a descriptive name for your instance `WP-instance`.

4. Under Application and OS Images (Amazon Machine Image), Choose Quick Start, and then choose the operating system (OS) for your instance. For your first Linux instance, we recommend that you choose Amazon Linux.
5. From Amazon Machine Image (AMI), select `Amazon Linux 2 AMI`, that is marked Free Tier eligible.

6. Under Instance type, for Instance type, choose t2.micro, which is eligible for the Free Tier.
7. Under Key pair (login), for Key pair name, choose an existing key pair or choose Create new key pair to create your first key pair.

Note: If you choose Proceed without a key pair (Not recommended), you won't be able to connect to your instance using the methods described in this tutorial.
8. Enter Key pair name `wp-instance`, for Private key file format select .pem for use with OpenSSH.

9. Under Network settings, notice that we selected your default VPC, selected the option to use the default subnet in an Availability Zone that we choose for you, and configured a security group with a rule that allows connections to your instance from anywhere. For your first instance, we recommend that you use the default settings.
10. Make sure Auto-assign public IP is enabled, because we are creating the server in public subnet
11. Under Firewall (security groups), choose Create security group, enter security group name `WP-security-group`

12. Under Inbound Security Group Rules, let's add two more rules:
* The first one is `HTTP` Rule and allow it from `anywhere`, everyone can make HTTP request to the server.
* The second one is `HPPTS` Rule and allow it from `anywhere`, everyone can make HTTPS request to the server.

13. For the storage you can get 30 GB under free tier, i will just keep it 15 GB which is sufficient and click on launch instance.

## ➡️ Step 2 - Connect to EC2 instance via Instance connect
1. After the instance is created and its on the running state, choose instance ID, and click on Connect button.

2. Under EC2 Instance Connect, click Connect

3. if you see the welcome message from the AWS command line, then you are successfully connected to the server.

## ➡️ Step 3 - Connect to EC2 instance via SSH client (local Terminal)
There is another way to Connect to EC2 instance which is via SSH client, local Terminal to connect to EC2 instance via SSH client.
1. Select the instance, and choose Connect button.
2. Choose `SSH client`, copy the SSH command to connect to EC2 instance, and paste it into your terminal.

3. Open your terminal in your computer and paste the SSH command (make you sure your key pair is saved in same folder as your local user in your computer).
4. Click `Yes` to continue then click Enter.

## ➡️ Step 4 - Install Apache on Amazon Linux 2
We will run following command to ensure that all packages on the system are up-to-date with the latest patches and versions.
```bash
sudo yum update -y
```
By running the following command, you install the Apache web server on your system, which can then be used to host websites and serve web content.
```bash
sudo yum install -y httpd
```
Let's start the Apache HTTP Server service by running the following command, By running this command, you initiate the Apache web server, enabling it to begin serving web content.
```bash
sudo service httpd start
```
To test it, go back to your EC2 console, copy the public IP of your EC2 instance and paste it in your browser.


## ➡️ Step 5 - Install PHP & MariaDB on Amazon Linux 2
By running this command, you install PHP 8.2 on your Amazon Linux system.
```bash
sudo amazon-linux-extras install php8.2 -y
```
Next, let's install MariaDB, by running following command, you install MariaDB 10.5, which is a popular open-source relational database management system, on your Amazon Linux system.
```bash
sudo amazon-linux-extras install mariadb10.5 -y
```
Let's start the MariaDB service, by running the following command, you start the MariaDB database server, enabling it to accept connections and handle database operations.
```bash
sudo systemctl start mariadb
```
As you can see below the database is running.

Let's access the home directory `/home/ec2-user`, enter `cd ~` and `pwd` present home directory.

## ➡️ Step 6 - Download the latest WordPress zip file
Let's download latest WordPress files from their official website.
By running the following command, you download the latest WordPress package to your current directory. This package can then be extracted and used to set up a WordPress site.
```bash
wget https://wordpress.org/latest.tar.gz
```
After the wordpress is download, run `ls` to see the wordpress file `latest.tar.gz` then run `tar -xzf latest.tar.gz` to unzip the file, lastly run `ls` to see the WordPress folder with all the necessary WordPress files.

## ➡️ Step 7 - Create RDS instance for WordPress website
WordPress needs a database to run properly, we need to create a database before we the installation of WordPress.
Note: For the database you have two options:
* Option 1. You can create a database locally on EC2 instance and connect it locally via local host.
* Option 2. You can create RDS instance and host your database on it (it's recommend to keep the database separate on RDS instance for durability and security reasons).
Amazon RDS DB instances simplify the deployment and management of relational databases, enabling you to focus on your application development and business logic rather than database maintenance tasks.
To create a DB instance:
1. Sign in to the AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/rds/.
2. In the upper-right corner of the Amazon RDS console, choose the AWS Region in which you want to create the DB instance. (make sure your AWS Region is the same as your EC2 instance).
3. In the navigation pane, choose Databases.
4. Choose Create database, then choose Standard create.
5. For Engine type, choose MySQL.
6. For Version, choose the engine version.

7. Select Free Tier so we don't get unexpected monthly bill for our database instance.

8. Type a name for your DB instance `wp-database`
9. Type a login ID for the master user of your DB instance `admin`
10. Ender Credentials management, select `Self managed` to create your own password or have RDS create a password
that you manage.
11. Choose `Auto generate password` Amazon RDS can generate a password for you, or you can specify your own password.
12. For Instance confiquration, select `db.t3.micro`

13. For the Allocated storage, enter the minimum value of `20 GiB`

14. In the Connectivity section, make sure to select same VPC as of your EC2 instance and default submit group.
15. For Public Access we want to keep it private for security select `No`
16. In the Connectivity section under VPC security group (firewall), if you select Create new, a VPC security group is created with an inbound rule that allows your local computer's IP address to access the database. Enter VPC security group name `rds-sg`

17. Enter initial database name `wp_database`
18. It's a good practice to encrypt your RDS instance I will select default key.

Note: AWS provide automated Backup Service which is additional cost. Once you are in production, it's recommend to use it, you can keep database backup up to last 35 days but for this demo I will disable Backup.
19. Enable deletion protection so it can protect your instance from accidental deletion.
20. Click Create database.
Note: Based on the options we selected you can see estimated monthly cost, the Amazon RDS Free Tier is available to you for 12 months. Each calendar month, the free tier will allow you to use the Amazon RDS resources listed below for free:

21. It will take about 2 to 5 minutes to create the database, once you see status is `available` click on **view connection details** to see RS credentials, copy host username and password of the database somewhere safe as you will needed for database connection from our EC2 instance.

## ➡️ Step 8 - Connect RDS and EC2 instance
In this sttep, we need to add an inbound rule to RDS Security Group to allow connect to RDS, it's to make sure we can access database from our EC2 instance.
* Open RDS and go to Security Group, we can see inbound is only allowed from one IP.

* We have created two security groups, one for EC2 and one for RDS instance.
* Copy Security Group ID of EC2 instance `wp-security-group`, select RDS Security Group `rds-sg`, select inbound rules and click on EDIT inbound rules.

* we will add rule, select type `MYSOL/Aurora`, Port is auto selected, then paste the ID of EC2 Security Group and add description click on Save rules.

**Now let's check if these rules are workingopen**
* EC2 instance dashboard and click on connect.
* we will just use EC2 instance connect method.

* To quickly verify the connection to database, run the following command:
In my case:
```bash
mysql -h wp-database.cr2g2a04mnil.us-east-l.rds.amazonaws.com -u admin -p
```
In you case (get from connection details to your database wp-database):
```bash
mysql -h wp-database.YOUR-ENDPOINT -u YOUR-USERNAME -p YOUR-PASSWORD
```
Note: Whenever you are typing your password, will not see it writing any text this is a Linux security feature, just paste your password hit Enter.
* If you see welcome message and my SQL terminal then you are successfully able to connect to RDS instance.
* To check if the database is created run the following command
```bash
show databases;
```
* As we can see below `wp_database` is already present, we have successfully connect our database.

Next we can start the process of Wordpress installation.
## ➡️ Step 9 - WordPress Installation on Amazon Linux 2 EC2 instance
To install WordPresson on Amazon Linux 2 EC2 instance:
1. The following command copies all files and subdirectories from the wordpress directory to the `/var/www/html/` directory, using superuser privileges to ensure the operation has the necessary permissions. This is typically used when setting up a WordPress website on a web server.
```bash
sudo cp -r wordpress/* /var/www/html/
```
2. Run the following command to add the `ec2-user` to the apache group on a Linux system.
```bash
sudo usermod -a -G apache ec2-user
```
3. By running the following command, you changes the ownership of the `/var/www` directory and all its contents to the user ec2-user and the group apache. This is often done to ensure that the `ec2-user` has the necessary permissions to manage web files and that the apache group has the appropriate access to these files, facilitating web server operations.
```bash
sudo chown -R ec2-user:apache /var/www
```
4. The following command is typically used in web server environments to ensure proper permissions for web server processes and users, facilitating collaboration and proper access control.
```bash
sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
```
5. The following command is used in a Unix-like operating system (such as Linux) to change the permissions of all files within the /var/www directory. The command finds all files within the `/var/www` directory and sets their permissions to `0664`.
```bash
find /var/www -type f -exec sudo chmod 0664 {} \;
```
6. Let's move to HTML folder by running
```bash
cd /var/www/html
```
7. As we can see below, we have `wp-config-sample.php` file, we will create a copy of this file and name it `wp-config.php`. This file contains configuration of WordPress website.

8. Now let's edit `wp-config.php` file by running `nano wp-config.php`
* On the `wp-config.php` we need to change database parameters so WordPress can connect to our database
* Enter your database name, username and password, then paste host of the RDS instance as shown below.
* Once you are done editing hit Control + X to exit.

* To check the changes we made run the command `cat wp-config.php`. As you can see below our file is modified with our database information.

* Next, we need to allow WordPress to use permalinks in Apache server, we will edit a parameter in Apache configuration file.
To do that, we need to use the following command to open config file in VIM Editor.
```bash
sudo vim /etc/httpd/conf/httpd.conf
```
* Change the `AllowOverride None` to `AllowOverride All`
Note: To type in VIM Editor, first press `i` key to get into insert mode, then you can
type when you want, to quit from VIM Editor press `Escape(Esc)` key then write `:wq` and press Enter.

* To make sure everything is saved and new settings are applied, we need to restart Apache server with restart command:
```bash
sudo systemctl restart httpd
```
* Go back EC2 instance console, and click on public IP of the instance.
Note: If you click on the public IP link, sometimes you might get the error connection, that is not an actual server error that is your browser doing `https` request rather than `http` request, just remove s at the end and press enter.

As you can see below we have the WordPress Welcome Page
* Fill in the information, enter your Site Title, Username, Password and Your Email, then click Install WordPress.

* Now you have successfully installed WordPress on your server.

* To login enter your email and password and click on login.

* It will open the WordPress Admin Panel to manage contents of your website.

In conclusion, deploying a WordPress website on AWS using EC2, RDS and other AWS services provides a highly scalable, reliable, and secure solution for hosting your website. By following the steps outlined in this guide, you have set up a robust infrastructure that can efficiently handle traffic fluctuations, ensure data integrity, and deliver a seamless user experience.
STAY TUNED FOR PART 2
## 💰 Cost
All services used are eligible for the AWS Free Tier. However, charges will incur at some point so it's recommended that you shut down resources after completing this tutorial.