Now that our AWS infrastructure is ready (VPC, subnets, EC2s) from Part 1
In this Blog we'll:
- Connect to our EC2 instances using SSH
- Install Apache, PHP, MariaDB
- Deploy our micro-blog app (frontend + backend)
- Configure everything to connect successfully
➤Step 1:Access EC2 via SSH:
- Note:You have
.pem
key downloaded (from Part 1) - Open our terminal (Linux/macOS) or Git Bash (Windows)
- Enter the below Code to access and secure the
.pem
file.
cd path/to/your-key.pem
chmod 400 your-key.pem
- check the image below it shows like this.
- Connected to the Frontend EC2(public subnet)
➤Step 2:Update EC2 & Install Apache + PHP (on Frontend EC2):
- Type the Following codes to setup our Linux(EC2) system that suitable for our deployment.
sudo yum update -y
sudo yum install -y httpd php php-mysqli git
- Start the web server:
sudo systemctl start httpd
sudo systemctl enable httpd
➤Step 3:clone the app from GitHub:
- setting up the
var/www/html
where the httpd can access easier.
cd /var/www/html
sudo git clone https://github.com/yourusername/Micro-blog-app.git
sudo chmod -R 755 Micro-blog-app
- We should confirm that our File-structure is like:
/var/www/html/Micro-blog-app/
└── frontend/
└── index.php
└── db.php
- This is the image that describes the above steps.
➤Step 4:SSH into Backend EC2(private Subnet):
Note: While installing the dependencies it cause error like internet gateways and NAT gateways.
- So set it up if needed.
- From inside Frontend EC2:
ssh -i your-key.pem ec2-user@<Backend-Private-IP>
- Use the private IP of backend EC2 (get it from AWS EC2 dashboard)
➤Step 5:Install MariaDB on Backend EC2
- Use the below commands to setup the MariaDB
sudo yum install -y mariadb-server
sudo systemctl start mariadb
sudo systemctl enable mariadb
- we need to secure our DB using the command:
sudo mysql_secure_installation
- It will ask us to set up a password to access the DB.
- The Below image describes the above task to get logged in to MariaDB.
➤Step 6: Create a Database & User for the App
- Login to the database using the root password which we provided in the above step.
- Create a Database and User to access the DB:
- Use the below code to Create User and DB
CREATE DATABASE microblog;
CREATE USER 'bloguser'@'%' IDENTIFIED BY 'blogpassword';
GRANT ALL PRIVILEGES ON microblog.* TO 'bloguser'@'%';
FLUSH PRIVILEGES;
EXIT;
- Check the Database is created and User is added.
➤Step 7:Configure MariaDB to Allow Remote Connections
- Go to
sudo vim /etc/my.cnf
dir to change the configuration file. - add or modify the
bind-address=0.0.0.0
. -
sudo systemctl restart mariadb
-Restart the DB.
➤Step 8:Update db.php on Frontend EC2
- Move to the dir
sudo vim /var/www/html/Micro-blog-app/frontend/db.php
- Example config has attached below:
<?php
$servername = "PRIVATE-IP-OF-BACKEND";
$username = "bloguser";
$password = "blogpassword";
$dbname = "microblog";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
- Save and close the nano editor.
➤Step 9:Launch the App in Browser
- Open the browser and go to:
http://<Frontend-EC2-Public-IP>/Micro-blog-app/frontend
- we can see our Micro-blog app is live and working.
➤Step 10: Deleting all the Resources to Avoid additional charges:
- Check and terminate the EC2 Instances and Volumes EBS Load balancer if Created.
- Check and detach the created VPC, Internet and Nat Gateways and Elastic IP's.
- Also Set up the Bill Limit to Get notified When the limit Reached.
Stay tuned for Upcoming blogs and Project Deployment 🌐🚀!
Top comments (2)
@akashabish , a perfect blog with perfect steps. Keep blogging
Sure sir ..✨Thanks a lot 🎉