A few weeks ago, when I was weighing my options for hosting the WordPress site. I came across multiple services that provided list of features that were relatively the same and all of them had one click WordPress installation. But knowing the flexibility AWS provides, I did not think twice to put in that extra effort and set up the entire environment myself!
Here are my key takeaways from using AWS over Hosting Services-
- The reliability(99.95%), durability(11 9’s!) and speed is like no other! In today’s world, speed is extremely essential, no one has the patience to wait 5 minutes for your blog to start up! With AWS 43 availability zones, the speed is consistent throughout the world.
- Scaling your resources is super easy. To start off, you can use the smallest EC2 instance type — t2.micro and then begin to scale up when traffic to your blog increases.
- Customize your entire setup. You get to choose everything — from the type of OS, to the database, to the security rules for your site; the options are plenty and it’s well documented.
- Now for the best part, 12 months of Free Tier usage! You get enough resources to set up the site and keep it running for free for a year. What more can you ask for, right?
Now let’s begin setting up the WordPress site! Make sure you have created a new AWS account and have all services up and running.
#1 Domain Name Configuration
You can either register for a domain on Route 53, or use any other ICANN accredited registrar.
If you have registered your domain outside of Route 53, like I have on Namecheap, you will have to add in the domain name as a hosted zone otherwise you can skip this step.
To do this, navigate to Route 53 from the services list and enter the domain name in the right panel and hit create.
Click on the Domain name and copy the Name Servers from Route 53 –
Now in Namecheap, open up the domain list and add them under NameServers, by selecting Custom DNS. Keep in mind, this may take a day or two to come into effect.
#2 Security and Access Management
In order for the EC2 instance to access the objects in S3 buckets, we need to create a role and provide access.
In the IAM service, start by selecting the service to add the role type
- Select the AmazonS3FullAccess Policy
- Provide EC2-S3Access as the name and create the role.
We will create security groups to specify rules for EC2 instance and RDS database
- Website-SG — Add Inbound rules for HTTP to allow all traffic (Source — 0.0.0.0/0)and SSH for your IP address only
- RDS-SG — Add Inbound Rule for MySQL and select the Website-SG security group as source. This will limit only your webserver to access your database.
#3 Setting up your first EC2 Instance
As mentioned earlier, you can always start off with a micro instance, and scale up to bigger instances as you start to get more traffic.
Head to the EC2 service and create an instance –
- Select Amazon Linux AMI with the SSD volume
- Select t2.micro instance type, which is eligible for free tier
- On the Configure Instance page, select S3-Access IAM role
- It is always a good practice to add Tags, go ahead and add your website/project name.
- On configure Security Group, select Website-SG.
- Navigate through the rest of the configurations and hit Review and Launch.
- Create a new keypair, call it MyKeyPair and save it on your machine.
- Now, once the instance is in Running state, identify and copy your Public DNS.
- Use PUTTY to SSH into the instance with the Host Name as ec2-user@<Public DNS>
- On the left nav bar, select Auth and add in your keypair file saved earlier.( If you are working on a Windows machine, you will have to convert the .ppk to .pem file using Puttygen.)
In the terminal,
Switch user to root user, some commands may run only with root access
Update all software packages on server
yum update -y
Install Apache web server, PHP, Mysql database
yum install httpd php php-mysql -y
Install WordPress on the server in the html directory
tar -xzf latest.tar.gz
If you want your URL without a wordpress extension, ex: hertechtales.com and not hertechtales.com/wordpress, you have to move all files under wordpress into the html directory
cp -r wordpress/* /var/www/html/
rm -rf wordpress
rm -rf latest.tar.gz
Create a file wp-config.php to store your database configurations that will be available when your wordpress site is up
Finally, start the Apache server!
service httpd start
#4 Setting up persistent storage using RDS
Navigate to the RDS service
- Create Instance and select MySQL, community edition
- Next Page — Select Dev/Test instance, covered under free tier
- Next Page — Click Only show options that are eligible for RDS Free Tier
- Specify db identifier, username and password
- Next Page — configure advanced settings — under vpc security group, select RDS-SG, entire a database name
Hit Launch DB and your RDS is ready to use.
#5 Making your website resilient by using S3
This section is optional, follow the steps if you want to have a backup of wordpress site code and all media files uploaded on wordpress site on s3.
In the S3 service create two buckets –
- one for storing wordpress site code — wordpress-code
- one for saving media files — wordpress-media
To automate the process of syncing wordpress code and media files, you will have to create a CRON job(scheduled job) on linux server.
Head to the terminal and type in the following commands
cd /etcnano crontab
The crontab file editor opens, where in we will be adding two scheduled jobs to run every 10 minutes. You can always change the execution period.
*/10 * * * * aws s3 sync --delete /var/www/html s3://wordpress-code
*/10 * * * * aws s3 sync --delete /var/www/html/wp-content/uploads s3://wordpress-media
Restart the CRON service service crond restart
Now it is time to reap the fruits of your hard work!
Type in your domain name in the browser and you should see the WordPress landing page. Next, you will be asked to add in the details of your database — Database Name, Username, Password, Database Host — will be your database endpoint and Submit.
From the next page, copy the contents of the wp-config.php and paste it into the wp-config.php file you created earlier.
Run the install and your WordPress is ready to use !