środa, 7 listopada 2018

How to deploy simple Angular page on AWS with MEAN AMI and secure it with HTTPS

Let assume you have:
  • Your Angular app built with   ng build --prod
  • AWS account
  • Registered domain


Go to  https://console.aws.amazon.com/ec2/ , launch new instance.

On page Choose an Amazon Machine Image search for angular, select MEAN Certified by Bitnami.



Run this instance.

Obtain password for your linux instance from Instances - Actions - Instance settings - Get System log - look for

[   35.807016] bitnami[1112]: #########################################################################
[   35.832098] bitnami[1112]: #                                                                       #
[   35.853029] bitnami[1112]: #        Setting Bitnami application password to *************************         #
[   35.872600] bitnami[1112]: #        (the default application username is 'root')                   #
[   35.894473] bitnami[1112]: #                                                                       #
[   35.908585] bitnami[1112]: #########################################################################

Username is ubuntu however.

Login to your instance with Filezilla, go to /opt/bitnami/apache2/htdocs and copy there angular files from /dist/your-app folder.

Edit /opt/bitnami/apache2/conf/bitnami/bitnami.conf:
In <VirtualHost _default_:80> section add:

 RewriteEngine On
 RewriteCond %{HTTP:X-Forwarded-Proto} =http
 RewriteRule . https://barfitter.net[L,R=permanent]

And comment out all SSL lines, like Listen 443, <VirtualHost _default_:443> section, etc.

Save and restart apache:
sudo /opt/bitnami/ctlscript.sh restart apache

Now go to AWS page, Route 53, create or change your domain settings:
Type A, Alias: Yes, Alias target: choose your instance.

Go to EC2 - Load balancer - create new Application Load Balancer and follow https://docs.bitnami.com/aws/how-to/configure-elb-ssl-aws/



Redirect www.domain.com to domain.com

Edit /opt/bitnami/apache2/conf/bitnami/bitnami.conf:

In <VirtualHost _default_:80> section add:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]

and in Route 53, create new record set:
Name: www.domain.com,
Type A, Alias: Yes, Alias target: choose your instance.