Deploy WordPress Blog on AWS: RDS & EC2 Setup
In the world of web hosting, deploying a robust and scalable website is a foundational skill for any DevOps engineer, system administrator, or developer. While shared hosting is simple, it lacks control and scalability. This guide will provide a comprehensive walkthrough on how to deploy a WordPress blog on AWS, leveraging the power of EC2 (Elastic Compute Cloud) for our application server and RDS (Relational Database Service) for our managed database. This architecture is the gold standard for a professional, high-performance WordPress installation, giving you full control over your environment.
By separating the web server from the database, we create a more resilient, secure, and independently scalable system. We will cover everything from launching the instances and configuring security groups to installing the necessary software and completing the WordPress setup.
Why Use AWS (EC2 + RDS) for Your WordPress Site?
Before we dive into the "how," let's understand the "why." This setup provides distinct advantages over a traditional, single-server installation:
- Scalability: You can scale your EC2 instance (application tier) and your RDS instance (database tier) independently. If your site gets high traffic, you can upgrade your EC2 instance without touching the database. If your database is the bottleneck, you can scale it up without impacting the web server.
- High Availability & Reliability: RDS is a managed service. AWS handles database patching, backups, and replication. You can easily deploy a Multi-AZ (Availability Zone) RDS instance, which provides an automatic, synchronous standby replica in a different AZ for fault tolerance.
- Performance: Dedicated resources for both your application and your database mean neither is competing for CPU, RAM, or I/O, leading to faster load times.
- Security: Separating the database into its own private subnet, accessible only by your web server, dramatically reduces its attack surface. We will use AWS Security Groups to enforce this strict communication policy.
EC2 (Elastic Compute Cloud): Your Virtual Server
EC2 provides the virtual machine (VM) that will run our web server (Apache or Nginx), PHP, and the WordPress application files. We have full root access, allowing us to install any software and configure the environment precisely as needed.
RDS (Relational Database Service): Your Managed Database
RDS takes the administrative burden of managing a database off your plate. We'll use the MySQL engine (or MariaDB, your choice), but AWS will manage the underlying OS, database software installation, patching, and automated backups, letting us focus on our application.
Prerequisites
To follow this tutorial, you will need:
- An AWS Account. If you don't have one, you can sign up and take advantage of the AWS Free Tier.
- A basic understanding of Linux command-line operations.
- An SSH client (like Terminal on macOS/Linux or PuTTY/Windows Terminal on Windows) to connect to your EC2 instance.
- A Key Pair for EC2. You can create this during the instance launch process.
Step-by-Step Guide to Deploy WordPress Blog on AWS
We will now proceed with the technical setup. Follow these steps carefully to ensure all components are correctly configured and communicating.
Step 1: Launching Your RDS MySQL Database
First, we'll set up our database. This is the foundation of our WordPress site.
- Log in to your AWS Management Console and navigate to the RDS service.
- Click on "Create database".
- Choose "Standard create" and select "MySQL" as the engine type.
- Under Templates, select "Free tier". This will pre-select settings (like
db.t3.micro
) that are eligible for 12 months of free usage. For a production site, you'd choose "Production". - Settings:
- DB instance identifier: Give it a unique name, e.g.,
wordpress-db
. - Master username: Choose a username, e.g.,
wp_admin
. - Master password: Create a strong, secure password and save it somewhere safe.
- DB instance identifier: Give it a unique name, e.g.,
- Storage: The Free Tier defaults are fine (General Purpose SSD, 20 GiB).
- Connectivity: This is the most critical part.
- VPC: Leave it as your default VPC.
- Public access: Select "No". We do not want our database to be accessible from the public internet. It should only be reachable by our EC2 instance.
- VPC security group: Choose "Create new". Name it something descriptive, like
wordpress-db-sg
.
- Database options: Under "Additional configuration," set an Initial database name, like
wordpress_db
. This is the database WordPress will use. - Click "Create database". It will take 5-10 minutes for the database to be created and become available. Once it is, click on it and note down the Endpoint. You will need this, your username, password, and database name later.
Step 2: Launching and Configuring the EC2 Instance
Next, we'll launch the virtual server that will host our WordPress files.
- Navigate to the EC2 service in your AWS console.
- Click "Launch instances".
- Name: Give it a name, e.g.,
wordpress-web-server
. - Application and OS Images (AMI): Select "Amazon Linux 2 AMI" (or Amazon Linux 2023). It's a solid, well-supported choice.
- Instance type: Select
t2.micro
ort3.micro
, as these are eligible for the Free Tier. - Key pair (login): Select an existing key pair you have access to, or create a new one. If you create a new one, download the
.pem
file immediately and store it securely. You will not be able to download it again. - Network settings: Click "Edit".
- VPC: Ensure it's the same default VPC as your RDS instance.
- Auto-assign public IP: Set to "Enable" so we can connect to it from the internet.
- Firewall (security groups): Choose "Create security group".
- Security group name:
wordpress-web-sg
- Inbound rules:
- Add rule: Type SSH (Port 22), Source My IP (This is for security, so only you can SSH in).
- Add rule: Type HTTP (Port 80), Source Anywhere (0.0.0.0/0).
- Add rule: Type HTTPS (Port 443), Source Anywhere (0.0.0.0/0).
- Security group name:
- Leave all other settings as default and click "Launch instance".
Step 3: Connecting the EC2 Instance to the RDS Database
This is the step where most people get stuck. We must explicitly allow our web server to communicate with our database.
- Navigate back to the RDS service and find your
wordpress-db
instance. - Click on the Connectivity & security tab.
- Click on the active VPC security group (it should be
wordpress-db-sg
). - Select the "Inbound rules" tab and click "Edit inbound rules".
- Click "Add rule":
- Type: Select "MySQL/Aurora" (it will default to port 3306).
- Source: This is the magic. Instead of an IP, start typing the name of your web server's security group:
wordpress-web-sg
. Select it from the list.
- Click "Save rules".
You have just told AWS: "Only allow traffic on port 3306 if it is coming from an instance that is part of the wordpress-web-sg
security group." This is far more secure and dynamic than using hard-coded IP addresses.
Step 4: Installing the LAMP Stack on EC2
Now, let's SSH into our EC2 instance and install the necessary software: Linux (already running), Apache, MySQL (client), and PHP.
- Go to your EC2 instances list, select your
wordpress-web-server
, and copy its Public IPv4 address. - Open your terminal and connect via SSH. (Replace
key.pem
and the IP with your own).
# First, make your key file read-only
chmod 400 /path/to/your-key.pem
# Connect to the instance
ssh -i /path/to/your-key.pem ec2-user@YOUR_PUBLIC_IP_ADDRESS
- Once connected, update your system and install Apache (httpd), PHP, and the MySQL client. We'll use Amazon Linux Extras to get a modern PHP version.
# Update all packages sudo yum update -y # Install Apache web server sudo yum install -y httpd # Start the Apache service sudo systemctl start httpd # Enable Apache to start on boot sudo systemctl enable httpd # Install PHP 7.4 (or newer) and common extensions for WordPress sudo amazon-linux-extras install -y php7.4 sudo yum install -y php-mysqlnd php-gd php-xml php-mbstring php-json # Install the mysql client (to test the DB connection) sudo yum install -y mysql
- (Optional but Recommended) Test RDS Connection: From your EC2 terminal, try to connect to your RDS database. This confirms your security groups are correct.
# Replace with your RDS endpoint and admin username mysql -h [your-rds-endpoint.aws.com] -u [wp_admin] -p # It will prompt for your password. # If you get a connection, type 'exit' and press Enter. # If it hangs or times out, your security group (Step 3) is incorrect.
Step 5: Downloading and Configuring WordPress
With our server ready, let's download and set up WordPress.
- Navigate to the web root directory.
cd /var/www/html
- Download the latest version of WordPress.
# Download the WordPress package sudo wget https://wordpress.org/latest.tar.gz # Extract it sudo tar -xzf latest.tar.gz # Move the contents of the 'wordpress' directory to the current directory sudo mv wordpress/* . # Clean up the empty directory and the downloaded file sudo rmdir wordpress sudo rm latest.tar.gz
- Create and edit the
wp-config.php
file.
# Copy the sample config to the real config file sudo cp wp-config-sample.php wp-config.php # Open the file with a text editor (nano is user-friendly) sudo nano wp-config.php
- Inside the
nano
editor, find the following lines and replace the placeholders with your RDS database information from Step 1:
/** The name of the database for WordPress */ define( 'DB_NAME', 'wordpress_db' ); // Your DB name from Step 1 /** MySQL database username */ define( 'DB_USER', 'wp_admin' ); // Your master username from Step 1 /** MySQL database password */ define( 'DB_PASSWORD', 'Your-Secure-Password-Here' ); // Your master password /** MySQL hostname */ define( 'DB_HOST', 'wordpress-db.c1a2b3c4d5.us-east-1.rds.amazonaws.com' ); // Your RDS Endpoint
- While still in
wp-config.php
, scroll down to the "Authentication Unique Keys and Salts" section. Open https://api.wordpress.org/secret-key/1.1/salt/ in your browser. Copy the entire block of text it generates and use it to replace the dummy keys in yourwp-config.php
file. - Save and exit
nano
(PressCtrl+X
, thenY
, thenEnter
).
Step 6: Finalizing Permissions and Apache Configuration
This is a critical security and functionality step. We need to give the Apache web server (which runs as the apache
user) ownership of the files so it can serve them and so you can install plugins/themes from the WordPress dashboard.
- Change the ownership of the web root.
# Give ownership of all files to the 'apache' user and group sudo chown -R apache:apache /var/www/html/
- Set the correct file and directory permissions.
# Set directories to 755 sudo find /var/www/html/ -type d -exec chmod 755 {} \; # Set files to 644 sudo find /var/www/html/ -type f -exec chmod 644 {} \;
- (Optional but Recommended for Permalinks) Allow
.htaccess
overrides in Apache.
# Edit the main Apache config file sudo nano /etc/httpd/conf/httpd.conf
Inside the file, find the <Directory "/var/www/html">
section (around line 151). Change the AllowOverride None
directive to AllowOverride All
. Save and exit.
- Restart Apache to apply all our changes.
sudo systemctl restart httpd
Step 7: Running the WordPress Installation
You're done with the command line! Now for the easy part.
- Open your web browser and navigate to your EC2 instance's Public IP address (e.g.,
http://YOUR_PUBLIC_IP_ADDRESS
). - If everything was configured correctly, you will see the famous WordPress 5-minute installation screen.
- Select your language.
- On the next screen, enter your desired:
- Site Title
- Admin Username (do not use "admin")
- Admin Password (use a strong one)
- Your Email
- Click "Install WordPress".
After a few seconds, you should see a "Success!" message. You can now log in to your new WordPress dashboard at http://YOUR_PUBLIC_IP_ADDRESS/wp-admin
.
Beyond the Basics: Securing and Scaling Your Deployment
You now have a functional site, but for a true production environment, consider these next steps:
- Assign an Elastic IP: By default, your EC2's public IP will change if you stop/start it. Create and associate an "Elastic IP" (a static IP) with your instance.
- Use a Domain Name: Use Amazon Route 53 to point your custom domain name (e.g.,
yourblog.com
) to your EC2's Elastic IP by creating an 'A' record. - Enable HTTPS (SSL/TLS): Use AWS Certificate Manager (ACM) to provision a free SSL certificate and attach it to an Application Load Balancer (ALB). The ALB will sit in front of your EC2 instance, handle SSL termination, and provide further scalability.
- Offload Static Assets: Use a plugin like W3 Total Cache or WP Offload Media to store your images and static files in an Amazon S3 bucket and serve them via Amazon CloudFront (CDN). This will dramatically speed up your site globally.
- Implement High Availability: The ultimate setup involves placing your EC2 instances in an Auto Scaling Group behind an ALB, and sharing the
wp-content/uploads
directory across instances using Elastic File System (EFS).
Frequently Asked Questions
Why not just install MySQL on the same EC2 instance?
You can, but you lose all the benefits of RDS. By installing MySQL on EC2, you become responsible for backups, patching, security, replication, and scaling. RDS manages all of this for you, is more durable, and allows you to scale your web and database tiers independently, which is a best practice in cloud architecture.
My site shows a "Database Connection Error." What's wrong?
This is almost always a security group misconfiguration. Double-check Step 3. Ensure your RDS security group (wordpress-db-sg
) has an inbound rule allowing traffic on port 3306 from your EC2 security group (wordpress-web-sg
). Also, verify the DB_HOST
, DB_NAME
, DB_USER
, and DB_PASSWORD
in your wp-config.php
file are 100% correct.
How much does this setup cost?
If you stay within the AWS Free Tier limits (t2.micro
or t3.micro
EC2 instance, db.t3.micro
RDS instance, 30GB of EBS, 20GB of RDS storage), this setup can be free for your first 12 months. After that, a small-to-medium traffic site might cost $15-$30 per month, depending on your instance sizes and data transfer.
How do I update WordPress or plugins?
Just like on any other host! Because we set the file permissions correctly in Step 6 (sudo chown -R apache:apache
), you should be able to update WordPress, themes, and plugins directly from the WordPress admin dashboard without needing to use SSH or FTP.
Conclusion
Congratulations! You have successfully moved beyond basic hosting and learned how to deploy a WordPress blog on AWS using a professional, scalable, and secure architecture. By separating your application on EC2 and your database on RDS, you have built a foundation that can grow with your traffic, from a small personal blog to a high-traffic enterprise website. This setup provides you with the performance, security, and reliability that define modern cloud infrastructure. Your next steps should be to explore implementing a load balancer, CDN, and a custom domain to create a truly production-ready solution.Thank you for reading the huuphan.com
Comments
Post a Comment