Mastering WordPress Deployment on AWS: A DevOps Journey with EC2 and LVM
Table of contents
Introduction
In today's digital landscape, creating a robust and scalable web presence is crucial for businesses and individuals alike. WordPress, being one of the most popular content management systems, offers a flexible platform for building websites. However, deploying WordPress in a scalable and maintainable way on cloud infrastructure requires careful planning and execution.
This blog post will guide you through the process of implementing a WordPress solution on Amazon Web Services (AWS) using EC2 instances and Logical Volume Manager (LVM). We'll cover everything from setting up the infrastructure to configuring the database and web server, ensuring you have a solid foundation for your WordPress site.
Project Overview
Our project involves setting up WordPress on two separate EC2 instances:
A web server running WordPress
A database server running MySQL
Both instances will use Red Hat Enterprise Linux (RHEL) 9.4 as the operating system, with three 10GB EBS volumes attached to each for flexible storage management using LVM.
This architecture offers several advantages:
Improved security by isolating the database
Better performance through dedicated resources for the web server and database
Easier scalability, allowing independent scaling of web and database servers
Prerequisites
Before we begin, ensure you have:
An AWS account with necessary permissions to create and manage EC2 instances and EBS volumes
Basic familiarity with AWS EC2 and Linux command line
SSH access to your EC2 instances
Step 1: Setting Up AWS EC2 Instances
Create two EC2 instances:
Web Server: t2.small instance type (minimum)
Database Server: t2.micro instance type
The web server requires more resources to handle PHP processing and serve web requests, hence the t2.small minimum requirement. The database server can start with a t2.micro, as MySQL doesn't require as much processing power for a small to medium-sized WordPress site.
For each instance, attach three 10GB EBS volumes. These will be used to create a flexible storage solution using LVM.
Step 2: Configuring EBS and Logical Volume Manager (LVM)
We'll configure LVM on both the web server and database server. Let's walk through the process for the web server (the process is similar for the database server):
Check Volume Visibility:
lsblk
This command lists all block devices. You should see your new volumes as
/dev/xvdb
,/dev/xvdc
, and/dev/xvdd
.Create Partitions: Use
gdisk
to create a single partition on each EBS volume:sudo gdisk /dev/xvdb
When prompted:
Enter
n
to create a new partitionAccept the default partition number (1)
Accept the default first sector
Accept the default last sector (to use the entire disk)
Enter
8e00
for the partition type (Linux LVM)Enter
p
to print the partition table and verifyEnter
w
to write the changes and exit
Repeat this process for /dev/xvdc
and /dev/xvdd
.
Create Physical Volumes:
sudo pvcreate /dev/xvdb1 /dev/xvdc1 /dev/xvdd1
This step prepares the partitions for use with LVM.
Create Volume Group:
sudo vgcreate webdata-vg /dev/xvdb1 /dev/xvdc1 /dev/xvdd1
This groups our physical volumes into a single volume group, allowing us to allocate space flexibly.
Create Logical Volumes:
sudo lvcreate -n app-lv -L 14G webdata-vg sudo lvcreate -n log-lv -L 14G webdata-vg
We create two logical volumes: one for the WordPress application data and another for logs.
Create File Systems and Mount: First, create the necessary directories and backup the existing log files:
sudo mkdir -p /var/www/html /home/recovery/logs sudo rsync -av /var/log/ /home/recovery/logs/
Now create the file systems and mount them:
sudo mkfs -t ext4 /dev/webdata-vg/app-lv sudo mkfs -t ext4 /dev/webdata-vg/log-lv sudo mount /dev/webdata-vg/app-lv /var/www/html sudo mount /dev/webdata-vg/log-lv /var/log
Restore the log files:
sudo rsync -av /home/recovery/logs/ /var/log/
Configure Persistent Mounts: Update
/etc/fstab
to ensure volumes are mounted automatically on boot:sudo blkid # Note the UUIDs of your logical volumes sudo nano /etc/fstab
Add the following lines (replace UUIDs with your actual UUIDs):
UUID=<app-lv-uuid> /var/www/html ext4 defaults 0 0 UUID=<log-lv-uuid> /var/log ext4 defaults 0 0
Save the file and exit. Then, reload the daemon:
sudo systemctl daemon-reload
LVM provides several benefits, including flexibility in resizing volumes, the ability to create snapshots for backups, and potential I/O performance improvements through striping.
Go through the same steps for the db-server instance, however, instead of “webdata-vg”", we use “dbdata-vg”. And the “app-lv” will be replaced by “db-lv“ and the mount location will be “/db“ instead of “/var/www/html“.
Step 3: Configuring Security Groups
Configure security groups to control access between our WordPress and MySQL instances:
MySQL Security Group:
Allow traffic on port 3306 from the web server's private IP.
Open SSH (port 22) for administrative access.
WordPress Security Group:
Open HTTP (port 80) for public access.
Open SSH (port 22) for administrative access.
Step 4: Installing and Configuring MySQL Database Server
Now, let's set up the MySQL database on our database server. SSH into your database server instance and follow these steps:
Install MySQL:
sudo dnf update sudo dnf install mysql-server -y
Start and Enable MySQL:
sudo systemctl start mysqld sudo systemctl enable mysqld
Set Root Password and Secure MySQL: First, set the root password:
sudo mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password'; FLUSH PRIVILEGES; EXIT;
Now run the MySQL secure installation script:
sudo mysql_secure_installation
This script helps you remove anonymous users, disallow remote root login, and remove the test database.
Create WordPress Database and User:
sudo mysql -u root -p CREATE DATABASE wordpress; CREATE USER 'wpuser'@'web_server_ip' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'web_server_ip'; FLUSH PRIVILEGES; EXIT;
Replace
web_server_ip
with your web server's private IP address.
Step 5: Installing Apache, PHP, and WordPress on Web Server
Now, let's set up our web server with Apache, PHP, and WordPress. SSH into your web server instance and follow these steps:
Install Apache:
sudo dnf install httpd sudo systemctl start httpd sudo systemctl enable httpd
Install PHP and Extensions: First, we need to install the EPEL and Remi repositories:
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
Now, install PHP 8.3 and necessary extensions:
sudo dnf module switch-to php:remi-8.3 sudo dnf module install php:remi-8.3 sudo dnf install php php-opcache php-gd php-curl php-mysqlnd php-xml php-json php-mbstring php-intl php-soap php-zip
Configure SELinux:
sudo setsebool -P httpd_execmem 1 sudo setsebool -P httpd_can_network_connect 1
These commands allow Apache to execute memory operations and make network connections, which are necessary for WordPress functionality.
Install WordPress:
bashCopysudo wget https://wordpress.org/latest.tar.gz sudo tar -xzvf latest.tar.gz sudo mv wordpress/ /var/www/html/ sudo chown -R apache:apache /var/www/html/wordpress sudo chmod -R 755 /var/www/html/wordpress sudo chcon -t httpd_sys_rw_content_t /var/www/html/wordpress -R
Conclusion
By following this guide, you've set up a scalable and maintainable WordPress installation on AWS EC2 using LVM for flexible storage management. This setup provides a solid foundation for your WordPress site, allowing for easy scaling and maintenance as your needs grow.
Remember to regularly update your WordPress installation, plugins, and themes, and to implement additional security measures such as SSL certificates and regular backups to ensure the longevity and security of your site.
References:
Subscribe to my newsletter
Read articles from Engr. Animashaun Fisayo Michael directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Engr. Animashaun Fisayo Michael
Engr. Animashaun Fisayo Michael
Frontend Developer | Javascript programmer | Registered Mechanical Engineer (MNSE, COREN) | Facilities Management Technologies Developer