Codementor Events

How i deploy angular application in AWS.

Published Nov 21, 2019Last updated Nov 28, 2019
How i deploy angular application in AWS.

In order to deploy an angular app you need the following:

  1. An angular application build
  2. Access to one linux instance
  3. A domain name , if you dont have one public ip address of the instance should be enough.

Step 1:
If you have the angular app already in place then you can simple run :
ng build --prod

this will generate the build in the dist/<application_name> dirctory.

If you dont have an appplication you can simply create one with :

ng new new-application --style scss --routing

then you can run the command above and get the build ready.

Step 2:

SSH to the Linux instance:
1. update the apt packages: apt update -y
2. upgrade the updated packages: apt upgrade -y
3. install nginx : apt install nginx
4. create the direcory from where you want to host the website.
cd /home/ubuntu
mkdir site
5. Now upload your content of the build directory to this folder /home/ubuntu/site that we have created in step 4. You can use filezila or scp to transfer the files to the instance
6. Now the only part we need to do is set the instance to accept connection on the default server port which is port 80 for http and 443 for https. we will keep it simple and only do http for this app. Meaning the application will be served when request comes to port 80.

Step 3:

1. go to the nginx sites-enabled direcotry: 
    **cd /etc/nginx/sites-enabled**
2. create a conf file for the application : **vi my_application.conf**
3. in  that file we will write nginx configuration, which is :

	server {
		listen 80;    listen [::]:80;
        root /home/ubuntu/site;
        index index.html index.htm index.nginx-debian.html;

        server_name 223.34.12.122;

        location / {
                try_files $uri $uri/ /index.html;
        }
	}
in place of 223.34.12.122 you will mention your instance ip address.


So now the request will go to port 80 and then it will load the index.html file of the angular application from the directory /home/ubuntu/site


**To deploy the angular application to s3 bucket you can simply upload the content of the build directory to the s3 bucket and set its policy as below.**
        {
            "Version":"2012-10-17",
            "Statement":[{
              "Sid":"PublicReadGetObject",
                  "Effect":"Allow",
                "Principal": "*",
                "Action":["s3:GetObject"],
                "Resource":["arn:aws:s3:::example-bucket/*"
                ]
              }
            ]
  	     }
    Thanks for reading. Enjoy coding with developwithlax.
Discover and read more posts from Laxmikanta Nayak
get started
post comments2Replies
Arokia Lijas
4 years ago

If I have to deploy another app how should the step 3 be changed?

Laxmikanta Nayak
4 years ago

It depends on the app you are deploying. So provide some details on the app you want to deploy , i can give you some suggestion.