# Host a WordPress Site on Amazon EC2 and map to the custom domain name

# Overview

Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 eliminates your need to invest in hardware upfront, so you can develop and deploy applications faster. You can use it to launch as many or as few virtual servers as you need.

### Now let us launch the WordPress site in Lightsail

#### Step1: Create EC2 instance

- Search EC2 on the search bar and click on EC2

![1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1666287261693/0v1jzyNV0.png align="left")

- Click on Launch instance

![2.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666287400066/kSMgbP3a5.PNG align="left")

- Give a name to your instance (say shoeasy server)

![3.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666287500308/z3lbN6vTC.PNG align="left")

- Choose an Amazon Machine Image(AMI) (say ubuntu)

![4.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666287655454/JujUxLY3U.PNG align="left")

- Choose an instance type 
(I am choosing t2.micro because it is included in the free tier. You can choose according to your needs.)

![5.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666287772146/Uyt9OEpUK.PNG align="left")

- Create a Key pair 

![6.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666287870734/QFydJ5RSn.PNG align="left")

- Give the Key pair a name (say ShoeasyKey) and Create Key pair
> Note: Keep Key pair Private, anyone who gets get pair can log in to your instance.


![7.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666325969568/mvTqtrgBS.PNG align="left")

- Create a Security Group, check all 3 box 
> It allows inbound traffic to connect our EC2 instance.

![8.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666288357721/PpTSW1-tn.PNG align="left")

- Configure Storage
> Amazon EC2 stores data on a network-attached virtual disk called Elastic Block Store. We will launch the Amazon EC2 instance using a default 8 GiB disk volume but you can get up to 30 GiB in the free tier. This will be your root volume (also known as a 'boot' volume). 


![9.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666288507519/di9uwrrDU.PNG align="left")

- Leave the rest of the settings default and launch instance 

![10.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666288574599/Ae4PHUCZS.PNG align="left")


![12.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666288674506/gLIXraoXG.PNG align="left")


#### Step2: Assign the Elastic IP to your instance 
> Every time our instance is rebooted it gets a different IPV4 address from a pool of AWS IPV4. Every time IP changes we need to modify A record to point the IP to our Domain. So, to solve this problem we allocate an Elastic IP address to our domain.

- On the left sidebar under Network & Security click Elastic IPs

![13.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666289124354/zvxJ--fgP.PNG align="left")

- Click on Allocate Elastic IP address and allocate it.

![14.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666289204948/dmJybWs8c.PNG align="left")
![15.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666289320216/KYPh7Tw_r.PNG align="left")

- Click on actions and Associate Elastic IP address

![16.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666289500835/vJZJooC_G.PNG align="left")

- Choose your running instance created in Step:1, its private IP address, and click Associate

![18.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666289676187/n_ux5Jl3a.PNG align="left")

- Now the Elastic IP will be attached to our instance

![19.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666289860192/1D9bWMyC_.PNG align="left")

#### Step3: Connect to instance 
> We have several different ways to connect to an instance using putty, PowerShell, and even directly through the Amazon Console. I have used PowerShell but you can use your convenient method.

- Goto running instances dashboard, select instance (say shoeasy server) and click on connect

![20.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666290152805/lMumabX9F.PNG align="left")

- Click on SSH client and copy the command to connect the instance with the username

The general structure to connect to an instance through ssh is:
```
ssh -i <name of key file><space><Public ipv4 DN>

``` 


![21.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666290322484/oUR6m8xjg.PNG align="left")

- Paste the above-copied command on PowerShell, You will be successfully connected.
> Note: Launch Powershell from the folder you have your "Shoeasykey.pem" file

![22.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666290582609/E9cg1q25W.PNG align="left")

#### Step4: Installing an Apache Server,MySQL and WordPress

- First elevate your privileges

```
sudo su
``` 

![23.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666291011014/jcUYKG25Y.PNG align="left")

- Update package and dependencies

```
apt-get update
``` 
> The apt-get upgrade command downloads and installs the updates for each outdated package and dependency on your system.

- Install the Apache server

```
apt install apache2
``` 
- Test the Apache server by pasting your Elastic IP on the browser

![24.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666291365995/e9kJHYuSW.PNG align="left")
 > If you get the above page you are good now.

- Install PHP runtime and PHP MySQL connector

```
apt install php libapache2-mod-php php-mysql
``` 
![25.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666291496609/9jtNu6daO.PNG align="left")

- Install MySQL server

```
apt install mysql-server
``` 
![27.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666291584994/e9uId1HBO.PNG align="left")

- Login to MySQL server

```
mysql
``` 

![28.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666291643874/bMaExIF9z.PNG align="left")
- Change the authentication plugin to MySQL native password

```
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'Y0urStr0ngP@ssw0rdHere';
``` 

![29.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666291773919/AXyMkpFKO.PNG align="left")

- Create a new database user for WordPress

```
CREATE USER 'your_username'@localhost IDENTIFIED BY 'Y0urStr0ngP@ssw0rdHere';
``` 

![30.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666291854752/S_8aBRlrd.PNG align="left")

- Create a database for WordPress

```
CREATE DATABASE wp;
``` 

![31.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666291989636/Fzd_ehkPM.PNG align="left")
- Give all privileges to database database 'wp'

```
GRANT ALL PRIVILEGES ON wp.* TO 'your_username'@localhost;
``` 

![32.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666292002780/2iMLDdIFO.PNG align="left")

- Download WordPress 

> Goto [WordPress](https://wordpress.org/) and click on get WordPress, Copy the downloadable link and run the below commands.

![34.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666292277382/EH42_bdUR.PNG align="left")


```
cd /tmp
``` 

```
wget https://wordpress.org/latest.zip
``` 
![35.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666292435592/MmKj-6ow2.PNG align="left")

- Unzip the zip file downloaded
> To Unzip the file, first of all, you need to install the unzip package. To do so, type in a terminal:

```
apt-get install unzip
``` 

```
unzip latest.zip
``` 


![38.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666292763062/Gs9pJ9u8m.PNG align="left")
- After unzipping you will find a directory named WordPress.Move the WordPress folder to the Apache document root and goto to that directory

```
mv wordpress/ /var/www/html
``` 

```
cd /var/www/html
``` 

![39.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666292838427/EzVqYwgOn.PNG align="left")

- Reload the apache server

```
systemctl reload apache2
``` 

![40.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666292905001/gqcmYzj7J.PNG align="left")

> Now when you paste your IPV4 on the browser you still see the default apache page and the WordPress is accessible only on http://YourIPV4/wordpress

![24.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666293093786/JQTKPdv_k.PNG align="left")

![43.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666293142323/sEFhIlCaJ.PNG align="left")

Let us correct this now 

#### Step5: Moving subpart into the root directory
- Modify 000-default.conf 
```
cd /etc/apache2/sites-available/
``` 

```
nano 000-default.conf
``` 
- Modify DocumentRoot from /var/www/html/ to /var/www/html/wordpress

![42.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666293605160/3AUeew9Vc.PNG align="left")

- Now we can access the WordPress from the root directory (i.e. YourIPV4)

![44.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666293754837/r0Plr_vIZ.PNG align="left")

#### Step6: Wordpress Setup

- Click on let's go

![44a.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1666322204434/i-wtVGKTG.png align="left")

- You may get this type of error, don't worry we will fix this. Copy this code.

![45.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666322250779/Iftzf_pOY.PNG align="left")

- Create *wp_config.php* file on the WordPress directory and paste the copied code.

```
cd /var/www/html/wordpress
``` 

```
nano wp-config.php
``` 

![46.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666322427804/gUIhUawL1.PNG align="left")

> save the code and restart the apache server

- Go back to the setup process and click Run the installation

![45.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666322597002/KFvX5QTSy.PNG align="left")

- Fill up the information required and click on Install WordPress

![47.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666322741300/97n_mN8bO.PNG align="left")

- WordPress has been successfully installed.

![48.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666322819636/zE-KP6A5N.PNG align="left")

- Login to WordPress using the login credentials you used before during the setup phase

![49.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666322872946/kw5u44Vud.PNG align="left")

- Boom !! You are done.

![50.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666322924393/DphQHkCIJ.PNG align="left")

![new.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666323223483/cXAh-w0C2.PNG align="left")

#### Step7: Map Elastic IP to a custom domain
- Open Route53 and Create a hosted zone

![51.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666323337941/v8U7mC4R3.PNG align="left")

- Give your domain name and description(optional), keep the rest default, and hit create

![52.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666323434765/RvhTgD1Ms.PNG align="left")

- Change nameservers of your domain
> I am using NameCheap for a domain. You can use Route53 or any third-party registrar to purchase the domain.

![53.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666323593240/yiBSlPf8b.PNG align="left")


![54.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666323640838/hNBp4NefE.PNG align="left")

> Note: It might take up to 48 hours to reflect the DNS change depending upon your domain registrar.

-  Create record

![55.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666323844562/UGTd-qOVv.PNG align="left")

- Copy your Elastic IP

![56.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666323881507/XqiPUPSsW.PNG align="left")

- Create a A record and paste the above copied IPV4 in the value field

![57.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666323923167/amjq1RtG5.PNG align="left")

![58.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666324003055/f7XedRE8J.PNG align="left")

> Note: The time for which a DNS resolver caches a response is set by a value called the time to live (TTL) associated with every record. Amazon Route 53 does not have a default TTL for any record type. You must always specify a TTL for each record so that caching DNS resolvers can cache your DNS records to the length of time specified through the TTL.

> I have used a simple routing policy. I will write a separate blog about the use cases of different routing policies.

- Modify 000-default.conf file and add your ServerName and ServerAlias

> Example ServerName shoeasy.me
ServerAilas www.shoeasy.me

```
cd /etc/apache2/sites-available/
``` 

```
nano 000-default.conf
``` 

![59.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666324443724/h5QVbtZVS.PNG align="left")

- Save and restart an Apache Server You are all done
```
systemctl reload apache2
``` 


![shoeasy.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666328942015/V5X_I3wn2.PNG align="left")

- when you try to log in to the website using *yoursite/wp-login.php* it again redirects to IPV4. So, let's fix this

- login to WordPress 
-Goto settings, Replace WordPress Address(URL) and Site Address(URL) with your domain name, and Save Changes.

![62.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666330193850/sn7kIuac3.PNG align="left")

#### Step8: Adding SSL Certificate 
>An SSL certificate encrypts the information so that it's not readable until it reaches the server that it's intended for. No one can intercept and read the information as it travels from your computer to the web servers.

![63.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666330378524/JhIOCPy6q.PNG align="left")

- Install certbot

```
apt install certbot python3-certbot-apache
``` 
- Request and install SSL on your site with certbot
> Read the Terms of Service and Type **Yes**  to agree. Provide an email and it will detect your domains simply hit enter to install SSL on all your domain

```
certbot --apache
``` 

![64.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666330812965/1uzFc1x1f.PNG align="left")

![65.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666330917028/oTRUo26HT.PNG align="left")

- You have successfully installed SSL certificate on your site

![66.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666331247457/1PFRjdvJx.PNG align="left")

![67.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1666331297972/s5GGKcmad.PNG align="left")




## Thank You








