DEV Community

Cover image for 💻Deploying the Micro-Blog App on EC2: Setup of Apache, PHP, and MariaDB - (Part-2)
AKASH S
AKASH S

Posted on • Edited on

💻Deploying the Micro-Blog App on EC2: Setup of Apache, PHP, and MariaDB - (Part-2)

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
Enter fullscreen mode Exit fullscreen mode
  • check the image below it shows like this.

Image description

  • 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
Enter fullscreen mode Exit fullscreen mode
  • Start the web server:
sudo systemctl start httpd
sudo systemctl enable httpd
Enter fullscreen mode Exit fullscreen mode

➤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
Enter fullscreen mode Exit fullscreen mode
  • We should confirm that our File-structure is like:
/var/www/html/Micro-blog-app/
  └── frontend/
      └── index.php
      └── db.php
Enter fullscreen mode Exit fullscreen mode
  • This is the image that describes the above steps.

Image description

➤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>
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • we need to secure our DB using the command:
sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode
  • 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.

Image description

➤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;
Enter fullscreen mode Exit fullscreen mode
  • Check the Database is created and User is added.

Image description

➤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);
}
?>
Enter fullscreen mode Exit fullscreen mode
  • 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.

Image description

➤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)

Collapse
 
santhoshnc profile image
Santhosh NC

@akashabish , a perfect blog with perfect steps. Keep blogging

Collapse
 
akashabish profile image
AKASH S

Sure sir ..✨Thanks a lot 🎉