How to Deploy a WordPress Website on AWS EC2 with MySQL Database on RDS
Introduction
If you’re looking to host your WordPress site on AWS, you're in the right place! WordPress is one of the most popular platforms for building websites and blogs, and AWS offers a scalable and reliable environment to host it. In this guide, we’ll walk you through setting up a WordPress site by using AWS EC2 to host the site and AWS RDS (Relational Database Service) to manage the MySQL database.
Here’s a quick look at the resources we’ll be using:
Amazon EC2 (Elastic Compute Cloud): A virtual server where you’ll install and run your WordPress site.
Amazon RDS (Relational Database Service): A managed database service where MySQL will store your WordPress data.
Step 1: Setting Up the MySQL Database on Amazon RDS
Before we start with the WordPress application, let's set up the database that will store all the content, user data, settings, and other essential information for your WordPress site. This will be managed by Amazon RDS.
How to Create the RDS Instance:
Login to AWS Console: Go to the AWS Management Console and navigate to the RDS service.
Create a Database: Click on “Create Database,” and select the MySQL option.
Choose Database Version: You’ll see various options for MySQL versions. Choose the one that's most compatible with WordPress (typically MySQL 5.7 or later).
DB Instance Settings: Set the DB instance identifier (for example, "wp-db"), username, and password.
Instance Class and Storage: Choose a reasonable instance class depending on the expected load (e.g., db.t3.micro for low traffic sites).
VPC and Subnet: You can use the default VPC and subnet, but make sure the RDS instance is accessible from your EC2 instance.
Security Settings: Set the security group rules to allow access to the database only from your EC2 instance to keep it secure.
Launch the DB Instance: After configuring the settings, click “Create Database” to launch the RDS instance.
Once created, note down the endpoint of the RDS instance, as you’ll need it to connect WordPress to the database.
Step 2: Launching the EC2 Instance to Host WordPress
Next, we’ll set up an EC2 instance to host the WordPress application. Think of EC2 as your virtual computer in the cloud.
Steps to Launch EC2:
Go to EC2 Dashboard: In the AWS Management Console, navigate to the EC2 service and click “Launch Instance.”
Choose an Amazon Machine Image (AMI): Select the Amazon Linux 2 AMI, or you can choose an Ubuntu AMI if you prefer that.
Instance Type: Choose an instance type based on your expected traffic. A t2.micro instance should be sufficient for small websites.
Configure Instance Details: Select the VPC that matches the one where your RDS instance is running. You can use the default settings for the rest.
Add Storage: The default storage (8 GB) should be enough for most WordPress sites.
Configure Security Group: Set up a security group to allow HTTP (port 80) and HTTPS (port 443) traffic from the internet. Also, allow SSH (port 22) to access the server remotely.
Review and Launch: After configuring the security group and settings, review everything and launch the instance.
Note: When you launch your EC2 instance, you will need to download the private key (.pem) file. Keep it safe, as it will be required to access the EC2 instance via SSH.
Step 3: Install Apache, PHP, and WordPress on EC2
Once the EC2 instance is up and running, we can SSH into it and install the required software.
SSH into the EC2 instance: Open your terminal or command prompt and run:
ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-ip
Install Apache Web Server: Update the instance and install Apache:
sudo yum update -y sudo yum install -y httpd
Install PHP: WordPress requires PHP, so let’s install it:
sudo amazon-linux-extras enable php7.4 sudo yum install -y php php-mysqlnd
Install MySQL Client: Install the MySQL client so we can connect to the RDS database:
sudo yum install -y mysql
Start Apache Web Server: Start Apache and enable it to run on boot:
sudo systemctl start httpd sudo systemctl enable httpd
Install WordPress: Download and install WordPress:
cd /var/www/html sudo wget https://wordpress.org/latest.tar.gz sudo tar -xvzf latest.tar.gz sudo rm -rf latest.tar.gz sudo chown -R apache:apache /var/www/html/wordpress
Step 4: Configure WordPress to Use the RDS Database
Now that WordPress is installed, we need to configure it to connect to the MySQL database in RDS.
Edit WordPress Configuration: Navigate to the WordPress directory:
cd /var/www/html/wordpress sudo cp wp-config-sample.php wp-config.php sudo nano wp-config.php
Update Database Connection Details: In
wp-config.php
, find the following lines and update them with your RDS database details:define('DB_NAME', 'your-db-name'); define('DB_USER', 'your-db-username'); define('DB_PASSWORD', 'your-db-password'); define('DB_HOST', 'your-db-endpoint'); // Use your RDS endpoint here
Set Permissions: Ensure the Apache user has the proper permissions to the WordPress files:
sudo chown -R apache:apache /var/www/html/wordpress
Restart Apache: Restart Apache to apply all changes:
sudo systemctl restart httpd
Step 5: Access WordPress and Complete the Setup
Open a web browser and enter the public IP address of your EC2 instance. You should see the WordPress setup page.
Choose your language and click “Continue.”
On the next screen, provide the database name, username, password, and host (the RDS endpoint).
Click Submit, and WordPress will establish a connection to the MySQL database on RDS.
Finally, you can create an admin account and start customizing your WordPress site!
Conclusion
Congratulations! You’ve just deployed a WordPress website using Amazon EC2 and RDS. This setup allows you to scale your website seamlessly, and Amazon’s managed services like RDS make it easier to handle database backups and performance tuning.
With this knowledge, you can now start building and deploying powerful WordPress websites with AWS.
Additional Tips:
Always keep your EC2 and RDS instances secure by using proper security groups and encryption.
Monitor your resources regularly using AWS CloudWatch for performance metrics.
Happy Learning!
Subscribe to my newsletter
Read articles from Urvish Suhagiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Urvish Suhagiya
Urvish Suhagiya
Exploring the world of DevOps 🌐.