DEV Community

Cover image for Deploy a full-stack application(Angular-Laravel-Mongodb Atlas) on AWS.
Khalil Chaymae
Khalil Chaymae

Posted on

Deploy a full-stack application(Angular-Laravel-Mongodb Atlas) on AWS.

Introduction
Welcome to the comprehensive guide on deploying an Angular, Laravel, MongoDB Atlas application on AWS (Amazon Web Services). This documentation is focused on helping you understand the step-by-step process of deploying our application stack on AWS infrastructure. By following these instructions, you will be able to host and run your application securely and efficiently.

Project Overview
Our project is a basic web application designed to help users manage their to-do tasks efficiently. It combines a minimalistic user interface with the reliability of Angular, Laravel, MongoDB Atlas, and AWS. Here's a brief overview of the core components:

  • Angular: Angular powers the front-end of our application, enabling dynamic and responsive user interfaces. It offers a simple and intuitive way to manage your to-do tasks.

  • Laravel: Laravel serves as the back-end of our application, handling task creation, updates, and deletions. It provides a user-friendly API for interacting with the to-do list.

  • MongoDB Atlas: MongoDB Atlas is our database solution. It stores and retrieves task data securely, ensuring that your to-do list is always available and up to date.

  • AWS (Amazon Web Services): AWS hosts and deploys our application. It guarantees scalability, reliability, and security, ensuring you can access your to-do list from anywhere.

Prerequisites
Before we dive into the deployment process, make sure you have the following prerequisites in place:

  1. An AWS account with administrative privileges.

  2. An Angular front-end application. This is the repository of the Angular project I used in this guide: https://github.com/Chaymae-Khl/ToDoList--Angular-Laravel-MongoDb--FRONTEND-.git .

  3. A Laravel back-end application, this is the repository of the Laravel project I used in this guide: https://github.com/Chaymae-Khl/ToDoList-Laravel-MongodbAtlas--BackEnd.git .

  4. A MongoDB Atlas cluster with your application's database.


Let's get started !!!

Step 1: AWS account

Step2: MongoDb atlas account

Image description

  • Select the free cluster and your provider (AWS) and change the cluster name if you want: (You have 512 MB of data size available to use in the free mode)

Image description

  • Create the user:

Image description

Image description

  • And voilà, your MongoDB database is ready:

Image description

Step3: Connect the back-end with Mongodb Atlas

  • Click the "Connect" button to obtain the connection string:

Image description

  • Click on Drivers:

Image description

Image description

  • In your .env file, update the DB_DNS link to the MongoDB Atlas connection string.

Image description


PROBLEM !!!
In this step, I encountered a connectivity issue. To resolve it temporarily, we added an IP address to the access list. However, it's essential to remove this entry later, as it's not recommended to grant access to our database for everyone.

Image description

Image description


Step4: Deploying the back-end (Laravel)
After confirming that the connection between the back-end and MongoDB Atlas works locally, you can proceed to deploy your application on AWS.

  • First, we need to compress our application files (ZIP)

Image description

  • So, before creating our environment, we need to create two things: the role and the EC2 key pair that we are going to use in the creation of our environment.

1- IAM role:

An IAM (Identity and Access Management) role in AWS (Amazon Web Services) is a secure and flexible way to delegate permissions to resources within your AWS environment used to grant temporary permissions to perform actions on AWS resources.

To create a IAM role:

  • Search for IAM service:

Image description

  • Click Roles > “ Create role ” button:

Image description

  • Select the Use case of the account:

Image description

  • Click the “ next “ button.
  • Now we choose the permisions : AWSElasticBeanstalkWebTier AWSElasticBeanstalkWorkerTier AWSElasticBeanstalkMulticontainerDocker

Image description

  • Enter the name of the role:

Image description

  • Click “create role” button.

2- EC2 key pair:

An EC2 key pair in AWS (Amazon Web Services) is a security credential used to securely authenticate and access virtual instances, specifically Amazon Elastic Compute Cloud (EC2) instances.

To create a key pair:

Image description

  • click the “Create key pair” button:

Image description

  • Enter the name of the key pair > click the “create key pair” button:
  • After that, a .pem file will be downloaded, and we will use it to connect with the instance.

Image description

  • In AWS search for ‘ elastic beanstalk ‘ service: Amazon Elastic Beanstalk is an easy-to-use service for deploying and scaling web applications and services developed with Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS.

Image description

  • Now we are going to create our environment.

Image description

  • Enter the application name and the domain:

Image description

  • Select the PHP platform: When you select a platform in AWS Elastic Beanstalk, you are choosing the runtime environment for your application.

Image description

  • Here, we need to upload our zipped Laravel folder:

Image description

  • Click the “Next “ button.
  • In this step, select the role and the profile based on the permissions required and also choose the EC2 key pair that allows us to connect with the instance (We have already created the appropriate role and EC2 key at the beginning of this step).

Image description

  • Keep clicking "Next" until you reach this step. Fill the document root with "/public " > Next.

Image description

  • Click the “ Submit ” button.

Image description

  • After a few minutes, we can see that our application has been created successfully, and the health of the environment is "OK".

Image description

  • To check that the back-end works you can test in Postman the routes: EX:

Image description


PROBLEMS !!!
In this step I encountered two problems:

Problem 1:

  • I encountered an issue with the driver that is not available in the application's instance, causing the application to not work automatically. This is the opportunity to connect to our instance and make the necessary changes.
  • The first thing we need is Ubuntu because I encountered issues with Windows, and it didn't work properly. To avoid any problems, you can install Ubuntu from the Microsoft Store and use it for this process.
  • If you remember, during the process of creating the EC2 key pair, a .pem file was downloaded. We will now use this file.
  • After installing Ubuntu, place the file in the Ubuntu root directory Ubuntu\root.
  • Then, open the Ubuntu terminal.
  • In AWS search for EC2 > instances.

Image description

  • Select you instance and click the “Connect” button:

Image description

Image description

  • Now we can install the mongodb driver

Image description

  • Type the following command to install it, and it will take a few minutes.

Image description

  • Then, you have to restart the Nginx server using the following command:

Image description

  • To check if MongoDB has been installed successfully, type the following command and search for "mongodb", This command will list all installed PHP extensions, and if MongoDB is installed, you should see "mongodb" in the output.

Image description

  • Now that we have installed the MongoDB driver, we have resolved the driver problem, and your Laravel application should be able to connect to MongoDB without any issues.

Problem 2:

  • Another issue I encountered was related to route accessibility. It seems that not all routes in the project are accessible.
  • To resolve this issue, we need to make modifications to a configuration file in the Nginx server. This file likely controls how incoming requests are routed and processed, and making adjustments to it can help ensure that all routes in the project are accessible as intended.
  • You can use the following command to open the Nginx configuration file for editing:

Image description

  • Add the following lines > Ctrl+s > Ctrl+x

Image description

  • Restart the server :

Image description

  • And now the routes problem is solved.

  • Finally, remember to remove the 0.0.0.0/0 IP address from the MongoDB Atlas access list and replace it with the IP address of your instance to enhance security.
  • This ensures that only your AWS instance can access your MongoDB Atlas database.
  • Go to your AWS EC2 instances and copy the public IP address of your deployed instance

Image description

  • Now, go back to MongoDB Atlas and update your access list to include the public IP address of your AWS instance, which you copied in the previous step. This ensures that your database is accessible only from your AWS instance.

Image description

Image description

Congratulations! You have successfully deployed your back-end application.


-Step5: Deploying the Front-end

  • After deploying our back-end project and confirming that it works seamlessly with MongoDB Atlas, there are a few changes we need to make to our Angular project:
  • First, we need to update the API URL in all the services to reflect the domain where the back-end is deployed.
  • Make sure to update all the URL in your project. EX:

Image description

  • After updating all the URLs, we need to build our Angular project. Run the following command:

Image description

  • After running the build command, you'll notice that a new folder has been created: /dist
  • We're going to use these files in the deployment process.

Image description

  • Now, go to the AWS service, and this time, we're going to use a service named S3.

Image description

  • click on “Create bucket” button:

Image description

  • Enter the bucket name > Create bucket:

Image description

  • Uncheck 'Block all public access'.

Image description

  • Then click 'Create bucket' button.

  • Click on the bucket that you just created:

Image description

  • Click on “Upload” button:

Image description

  • Click on “Add files” button:

Image description

  • Now upload your files.

Image description

  • Click on “Upload” button:

Image description

  • Now after we uploaded our files click on your bucket.
  • Then click Propreties.

Image description

  • Edit the Static website hosting:

Image description

  • Change Static website hosting to enable:

Image description

Image description

  • then click 'save changes' button.

  • Now, it's time to make some changes to the permissions:

Image description

Let's first define what a policy means in the context of S3:
A policy is a JSON (JavaScript Object Notation) document that defines permissions and access control rules for S3 buckets and objects. S3 policies are used to specify who can access your S3 resources, what actions they can perform (e.g., read, write, delete), and under what conditions those actions are allowed or denied.

  • After defining the policy, it's time to make edits to the bucket policy:

Image description

  • Now, add a new policy:

Image description

  • Click on the 'Save changes' button.
  • We are done we can access our application via internet.
  • To access the application click on your bucket name > Properties

Image description

Image description

Congratulations! You have successfully deployed your FullStack application.

Image description

Image description

Top comments (0)