How To Host a WordPress Website Using AWS ?

Step1

AWS is the web service provided by Amazon Pvt Ltd and we can create a free tier account for testing/tutoring purpose.

Use the below link and follow the steps to create an aws account in AMAZON

https://aws.amazon.com/free/activate-your-free-tier-account/

Step 2

Register your domain and host DNS zones in Amazon route 53.

Amazon route 53 is the DNS platform for AWS customers, here you can register domains, host DNS entries. We are going to register a new domain name.

Consider this Domain name: myblog2018.com  as a sample domain for tutorial purpose.

Go to your AWS console >> Networking & Content Delivery >> Route 53 >> register your domian >> Follow the procedure to activate it

Go to Route 53 >> hosted zones ( here you will see the name servers and you can add DNS entries)

We have completed Domain part, next, we are going to create a VPS(amazon instance or EC2)

Step 3: Create an EC2 instance

Go to services >> EC2 >> Launch instance >> select Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type – ami-922914f7 >> select the option “t2.micro”, this is the free tier option and cost-free, we are not adding any other disks or extra services, thus go ahead and click ” next” and launch the instance, during the setup download the pem key and store it in your local machine.

Make sure to create a new security group and open port 80,22 and 443 for worldwide during the instance creation. (we need to create/use security groups for amazon instance and it act like a firewall)

Step 4: Installing and configuring Nginx web server in EC2 instance

Go to your key file location in your local machine. Protect your key file by adding 400 permission.

chmod 400 key.perm

ssh [email protected] -i key.perm

Use the public Ip address to login ec2 instance.

Once you logged in, escalate your privilege as root user

sudo su

We need to update the YUM repositories first.

yum update -y

yum install nginx -y

service start nginx

chkconfig nginx on

The default location for nginx configuration is /etc/nginx. Default nginx configuration file is /etc/nginx/nginx.conf

We need to create a virtual host entry for our domain.

Go to /etc/nginx

Create a directory called sites-enabled.

Create a file called myblog2018.com.conf ( This is our conf file for nginx for our website).

Virtual host entry

server {
listen 80;      —————————-> for listening to port 80
listen [::]:80;———————————–> for ipv6 connections
root /home/myblog/;————————-> your website file location
index index.html index.htm index.php;
server_name myblog2018.com www.myblog2018.com; ———————> replace with your domain name
location / {
try_files $uri $uri/ =404;
}
}

Check whether our nginx conf is error-free using the below command:

nginx -t

Restart nginx

service nginx restart

Step 5: Installing WordPress

Go to your Documentroot location for your website.

cd /home/myblog/

wget https://wordpress.org/latest.tar.gz

tar -xvzf latest.tar.gz

mv latest/* .

rm -rf latest.tar.gz latest/

Step 6: Installing Letsencrypt SSL certificate

yum install python27-devel git  —————> install git on our server
git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt ——–> clone the git repository with a folder on our server

/opt/letsencrypt/letsencrypt-auto –debug —–> this will install Lets encryp

You will see, during the SSL installation, it will ask for the domain name and email address.

The certboot will create 443 section for the domain automatically in /etc/nginx/sites-enabled/myblog2018.com.conf

server {
listen 443;
listen [::]:443;
root /home/myblog/
index index.html index.htm index.php;
server_name myblog2018.com www.myblog2018.com;
location / {
try_files $uri $uri/ =404;
}
}

Step 7: Redirect all traffic to HTTPS

You can put the redirection directive in block 80 in our configuration.

rewrite ^ https://www.myblog2018.com._uri? permanent;

Step 8: Pointing our domain to our EC2 instance Ip address

Go to AWS console >> Networking & Content Delivery >> Route 53 >> Hosted Zones >> create a A entry to our Ec2 instance public IP address for both myblog2018.com and www.myblog2018.com