# Deploy WordPress website on AWS

### [**AWS EC2 Instance?**](https://mizanf123.hashnode.dev/)

Amazon EC2 (Elastic Compute Cloud) offers virtual servers (instances) in the cloud. These can be customized based on your needs, with various sizes and types available. You can choose operating systems, scale easily, control networking, ensure security, and connect with different storage options. EC2 is in various regions, each with multiple zones for reliability. Pricing includes pay-as-you-go, reserved, and spot instances. It's used for diverse tasks, from hosting to data analysis, due to its flexibility and scalability.

### [**AWS RDS?**](https://mizanf123.hashnode.dev/)

Amazon RDS (Relational Database Service) is AWS's managed database solution. It simplifies database setup, maintenance, and scaling for MySQL, PostgreSQL, Oracle, SQL Server, etc. It automates backups, offers scalability, high availability, security, and easy migration. RDS is ideal for businesses seeking efficient, hands-off database management in the cloud.

### [**Apache Server?**](https://mizanf123.hashnode.dev/)

The **Apache HTTP Server**, colloquially called **Apache**, is a free and open-source **cross-platform** web server software, released under the terms of **Apache License 2.0**. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.

### [**IAM Role?**](https://mizanf123.hashnode.dev/)

An **IAM role** is an **IAM identity** that you can create in your account that has **specific permissions**. An **IAM role** is similar to an **IAM user**, in that it is an **AWS identity** with **permission policies** that determine what the identity can and cannot do in AWS.

## [**Task-01**](https://mizanf123.hashnode.dev/)

Step-01: Let's create an EC2 instance for our WordPress site on AWS.Give it a name and select the Ubuntu Server 20.04 LTS (HVM), SSD Volume Type AMI.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693293716368/56878751-2b8f-4e53-8930-9f2d171c8a8c.png align="center")

* **<mark>Step-02:</mark>** Select the `t2.micro` instance type and key pair.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693293783709/f1dd3106-8a4d-40ac-9da0-45b90925b843.png align="center")

* **<mark>Step-03:</mark>** Create a `new security` group and add the following `rules` to it.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693293867263/0fa0a2e8-9ec8-4a51-93e8-35a6507fe5d4.png align="center")

* **<mark>Step-04:</mark>** We have created our EC2 instance.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693294048521/20ddc720-d55f-4411-8611-8db3fe8bc3bf.png align="center")

### [**Now let's create an** `RDS` for our WordPress site.](https://mizanf123.hashnode.dev/)

* **<mark>Step-01:</mark>** Search for `RDS` in the `AWS services` and select `RDS`.
    
* **<mark>Step-02:</mark>** Click on `Create database`.
    

[![imageedit_2_4092586013](https://user-images.githubusercontent.com/76991475/253860396-fec872ee-a1ac-4e85-bd52-67e1807c42ae.png align="left")](https://user-images.githubusercontent.com/76991475/253860396-fec872ee-a1ac-4e85-bd52-67e1807c42ae.png)

* **<mark>Step-03:</mark>** Select `MySQL` as the `Database creation method`.
    

[![Screenshot from 2023-07-15 21-18-32](https://user-images.githubusercontent.com/76991475/253859533-b07d573a-eef4-4d1c-8a8c-d21b16232c19.png align="left")](https://user-images.githubusercontent.com/76991475/253859533-b07d573a-eef4-4d1c-8a8c-d21b16232c19.png)

* **<mark>Step-04:</mark>** Select **Engine Version** and then Select `Free tier` in `Templates`.
    

[![Screenshot from 2023-07-15 23-11-13](https://user-images.githubusercontent.com/76991475/253859541-43154a0d-f628-49cb-a08a-c27438eab79b.png align="left")](https://user-images.githubusercontent.com/76991475/253859541-43154a0d-f628-49cb-a08a-c27438eab79b.png)

* **<mark>Step-05:</mark>** Now we will set the `DB instance identifier` and `Master username` and `Master password`.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693294489975/034f8ad3-1cb6-4372-a178-f9a646098506.png align="center")

* **<mark>Step-06:</mark>** Let the instance and storage type by default and click on `Create database`.
    
* **<mark>Step-07:</mark>** Now check the connectivity of the database by clicking on **"Don't connect to an EC2 instance"** and **VPC** is the default.
    

[![Screenshot from 2023-07-21 11-48-09](https://user-images.githubusercontent.com/76991475/255123090-90985e32-fd50-47c7-b87b-2fac1fe1990b.png align="left")](https://user-images.githubusercontent.com/76991475/255123090-90985e32-fd50-47c7-b87b-2fac1fe1990b.png)

* **<mark>Step-08:</mark>** Configure the **Additional Setting** by adding a name to it.
    

[![Screenshot from 2023-07-21 11-48-22](https://user-images.githubusercontent.com/76991475/255123123-0e8d0ff9-097f-4b9f-894e-cfbbc1773fb7.png align="left")](https://user-images.githubusercontent.com/76991475/255123123-0e8d0ff9-097f-4b9f-894e-cfbbc1773fb7.png)

* **<mark>Step-09:</mark>** Click on `Create Database` and check the dashboard for your database.
    

[![Screenshot from 2023-07-21 14-58-56](https://user-images.githubusercontent.com/76991475/255136689-7c1c2dcd-3002-4419-9f57-60b6d2e73e83.png align="left")](https://user-images.githubusercontent.com/76991475/255136689-7c1c2dcd-3002-4419-9f57-60b6d2e73e83.png)

---

### [Now let's configure the AWS Relational Database Service or RDS with our EC2 instance.](https://mizanf123.hashnode.dev/)

* By giving the network connectivity to our EC2 [**instance.So**](http://instance.So) Open **AWS RDS**.
    

[![Screenshot from 2023-07-21 14-58-56](https://user-images.githubusercontent.com/76991475/255138635-4fd3ebd6-da20-476d-934e-86fc9d8b2eb6.png align="left")](https://user-images.githubusercontent.com/76991475/255138635-4fd3ebd6-da20-476d-934e-86fc9d8b2eb6.png)

* In the RDS console &gt; Select the database &gt; Click on **Connectivity & security** &gt; Select **VPC security groups** &gt; Click on **Edit** &gt; Select the security group &gt; Click on **Save**.
    

[![imageedit_20_3810322249](https://user-images.githubusercontent.com/76991475/255138482-ca432a1e-20a6-4049-99a9-d0eb3aed6e4b.png align="left")](https://user-images.githubusercontent.com/76991475/255138482-ca432a1e-20a6-4049-99a9-d0eb3aed6e4b.png)

* Go inside the Security Group &gt; Click on **Inbound rules** &gt; Click on **Edit inbound rules**.
    

[![imageedit_22_3880708883](https://user-images.githubusercontent.com/76991475/255139222-c1c4a475-695e-47cd-a641-5cebbbdd8a52.png align="left")](https://user-images.githubusercontent.com/76991475/255139222-c1c4a475-695e-47cd-a641-5cebbbdd8a52.png)

* Click on it and add the source as the **Security group** we created earlier while creating an `EC2 instance` and click on **Save rules**.
    

[![imageedit_26_8964083573](https://user-images.githubusercontent.com/76991475/255140414-e1a4ed19-2d4a-46be-97ee-3588f1f5f283.png align="left")](https://user-images.githubusercontent.com/76991475/255140414-e1a4ed19-2d4a-46be-97ee-3588f1f5f283.png)

---

### [Now let's create an IAM Role for our EC2 instance to access the RDS instance.](https://mizanf123.hashnode.dev/)

* **<mark>Step-01:</mark>** Go to `AWS Console` &gt;&gt; `Services` &gt;&gt; `IAM` &gt;&gt; `Roles` &gt;&gt; `Create Role` and also select EC2 as the service.
    

[![Screenshot from 2023-07-17 09-56-25](https://user-images.githubusercontent.com/76991475/253859592-7372ee87-83ab-445a-82dc-3efcfa68c04c.png align="left")](https://user-images.githubusercontent.com/76991475/253859592-7372ee87-83ab-445a-82dc-3efcfa68c04c.png)

* **<mark>Step-02:</mark>** Add the `AmazonRDSFullAccess` policy to the role.
    

[![Screenshot from 2023-07-17 09-56-54](https://user-images.githubusercontent.com/76991475/253859603-52977de4-3292-4fbe-8c3e-103a9a333282.png align="left")](https://user-images.githubusercontent.com/76991475/253859603-52977de4-3292-4fbe-8c3e-103a9a333282.png)

* **<mark>Step-03:</mark>** Give a name to the role and create the role and review it.
    

[![Screenshot from 2023-07-17 09-58-06](https://user-images.githubusercontent.com/76991475/253859611-e55ac07a-0b3a-4d0c-82a1-e5441162a701.png align="left")](https://user-images.githubusercontent.com/76991475/253859611-e55ac07a-0b3a-4d0c-82a1-e5441162a701.png)

* **<mark>Step-04:</mark>** Now check whether the role is created or not.
    

[![imageedit_4_5255382338](https://user-images.githubusercontent.com/76991475/253874970-b08fb251-5ad5-496b-945d-35f67452fa0a.png align="left")](https://user-images.githubusercontent.com/76991475/253874970-b08fb251-5ad5-496b-945d-35f67452fa0a.png)

* **<mark>Step-05:</mark>** Now go to `EC2` and select the `Instance` and click on `Actions` &gt;&gt; `Instance Settings` &gt;&gt; `Modify IAM Role`.
    

[![Screenshot from 2023-07-17 12-48-50](https://user-images.githubusercontent.com/76991475/253875497-26678580-8019-499d-a409-f18fc7994d0d.png align="left")](https://user-images.githubusercontent.com/76991475/253875497-26678580-8019-499d-a409-f18fc7994d0d.png)

* **<mark>Step-06:</mark>** Select the role which we created previously and click on `Save`.
    

[![Screenshot from 2023-07-17 09-59-46](https://user-images.githubusercontent.com/76991475/253859634-4606fb9d-9793-4cd3-a83b-078695c0f989.png align="left")](https://user-images.githubusercontent.com/76991475/253859634-4606fb9d-9793-4cd3-a83b-078695c0f989.png)

---

* Now let's connect to our **EC2 instance** using **SSH**.
    

* Now let's install **MySQL-client** in our EC2 instance.
    

```plaintext
sudo apt update
sudo apt-get install mysql-client
```

```plaintext
mysql --version
```

* Go to **RDS Console** &gt; Open the `Database you created` &gt; In the details of your Amazon RDS database, the hostname will be shown as the **Endpoint** in the **Connectivity** & **Security section.**
    

[![imageedit_28_6002623661](https://user-images.githubusercontent.com/76991475/255144849-928324b8-76cd-4402-bd53-f50e512d8b46.png align="left")](https://user-images.githubusercontent.com/76991475/255144849-928324b8-76cd-4402-bd53-f50e512d8b46.png)

* Here we are connecting the RDS Database with mysql-client in our EC2 instance.
    

```plaintext
export MYSQL_HOST=<your-endpoint>
#export MYSQL_HOST=wordpress.caxt8wmsdc7r.ap-south-1.rds.amazonaws.com
```

* Now let's fill in the **Master username** and **Master password** which we created while creating the RDS instance so that it verifies and give access to mysql.
    

```plaintext
mysql --user=<user> --password=<password> wordpress
#mysql --user=admin --password=test@123$ wordpress
```

* Now let's Install a Server where we can Host our Website for that we have used **Apache Server**.
    

```plaintext
sudo apt update
```

```plaintext
sudo apt install apache2
```

* Now let's check the status of the **Apache server**. Open the **Public IP** of your `EC2 instance` in the browser.
    

* Now let's create a Database for our WordPress site.
    

```plaintext
CREATE DATABASE wordpress;
CREATE USER 'wordpress' IDENTIFIED BY 'wordpress-pass';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress';
FLUSH PRIVILEGES;
Exit
```

* After this let's **Download and COnfigure** the **WordPress**.
    
* wget command is used to download **WordPress**.
    

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

* Now let's **extract the tar file**.
    

```plaintext
tar -xvzf latest.tar.gz
```

[](https://user-images.githubusercontent.com/76991475/255123291-cba217a5-54f8-488e-9397-b25531c40b1f.png)

* After Extracting `change the directory` to WordPress and create a **copy** of the default `configuration file` which is **(wp-config-sample.php)**.
    

```plaintext
cd wordpress
cp wp-config-sample.php wp-config.php
```

* Now let's edit the **wp-config.php** file.
    

```plaintext
vim wp-config.php
```

* First, We will edit the **database name** and **database user** and **database password**.
    

* Now let's Deploy the WordPress site, but before that, we need to install some dependencies.
    

```plaintext
sudo apt install php libapache2-mod-php php-mysql -y
```

* Now let's copy the WordPress files to the **Apache root directory**.
    

```plaintext
cd ..
sudo cp -r wordpress/* /var/www/html/
sudo systemctl restart apache2
```

* To check whether the website is running or not open the **Public IP** of your `EC2 instance` in browser.
    

```plaintext
http://<public-ip>/wp-admin
```

[![Screenshot from 2023-07-21 13-38-36](https://user-images.githubusercontent.com/76991475/255123515-54087e5f-620d-45f1-a3f5-14b51c50f035.png align="left")](https://user-images.githubusercontent.com/76991475/255123515-54087e5f-620d-45f1-a3f5-14b51c50f035.png)

Thus we have **successfully deployed our WordPress site** and also **connected** it with the **RDS instance**.
