Skip to content

Instantly share code, notes, and snippets.

@johnthillaye
Last active October 28, 2022 18:03
Show Gist options
  • Save johnthillaye/c9d8a1b948c03040233f to your computer and use it in GitHub Desktop.
Save johnthillaye/c9d8a1b948c03040233f to your computer and use it in GitHub Desktop.
MEAN.JS Full Stack Deployment Process to AWS EC2

MEAN.JS Full Stack Deployment Process to AWS EC2

Deploy AWS EC2 Instance

###Install & configure AWS CLI tools

$ sudo pip install awscli
$ aws configure
AWS Access Key ID [None]: YOUR_ACCESS_KEY
AWS Secret Access Key [None]: YOUR_SECRET_KEY
Default region name [None]: eu-central-1
Default output format [None]: json

Create security group and key pair, authorize SSH

We create a security group that will allow SSH on port 22 and HTTP on port 80.

$ aws ec2 create-security-group --group-name devenv-sg --description "Security group for {Project Name}" --region "eu-central-1"
{
    "GroupId": "sg-bf67d000"
}
$ aws ec2 authorize-security-group-ingress --group-name devenv-sg --protocol tcp --port 22 --cidr 0.0.0.0/0
$ aws ec2 authorize-security-group-ingress --group-id sg-b1ba09d6 --protocol tcp --port 80 --cidr 0.0.0.0/0
$ aws ec2 create-key-pair --key-name devenv-key --query 'KeyMaterial' --output text > devenv-key.pem
$ chmod 400 devenv-key.pem

Run instance with Debian Jessie Image

Official List of EC2 Debian Jessie Images

Choose the AMI matching your instance region.

$ aws ec2 run-instances --image-id ami-116d857a --count 1 --instance-type t2.micro --key-name devenv-key --security-groups devenv-sg --query 'Instances[0].InstanceId'
> "i-5c6b6000"
$ aws ec2 describe-instances --instance-ids i-5c6b6000--query 'Reservations[0].Instances[0].PublicIpAddress'
> "52.4.202.12"
$ ssh admin@52.4.202.12 -i devenv-key.pem

Install MongoDB, Node.JS, NPM, Git

MongoDB 3.0

Add the right MongoDB aptitude package list and install

sudo apt-key adv --keyserver 'keyserver.ubuntu.com' --recv '7F0CEB10'
echo 'deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.0 main' | sudo tee '/etc/apt/sources.list.d/mongodb-org-3.0.list'
sudo apt-get update
sudo apt-get install -y mongodb-org

Start Mongo Daemon

sudo service mongod start

Test

admin@ip-172-31-48-33:~$ mongo
MongoDB shell version: 3.0.5
connecting to: test

Node.JS & NPM

Add NodeSource to aptitude using Curl (as root)

sudo su
apt-get install curl
curl -sL https://deb.nodesource.com/setup_0.12 | bash -
apt-get install -y nodejs

Test

root@ip-172-31-48-33:/home/admin# node -v
v0.12.7
root@ip-172-31-48-33:/home/admin# npm -v
2.11.3

Git

sudo apt-get install git

AMI

Using the AWS CLI Tool we create an AMI that represent the exact state of our EC2 instance

aws ec2 create-image --instance-id i-5c6b6000 --name MongoNodeDebian

In the ressource section you will find links to AMIs for every "checkpoint" of this guide.

Install Node.JS Tools

  • Express: Web-app Framework (Mandatory)
  • Bower: NPM for the front-end
  • Gulp: Streaming Build System
  • YO aka YEOMAN: App-project generator
  • PM2: Process Manager

sudo npm install -g yo gulp bower express pm2

Have fun with generators

Various interesting generators are available on Yeoman public repo. For this MEAN Stack app, I will start by generating the Front-End using the very good generator-gulp-angular

npm install install -g generator-gulp-angular

Create a new directory, and install:

mkdir my-app && cd $_
yo gulp-angular

Complete installation, build and test with gulp

gulp serve

You now have a running test server on localhost:3000

You can test it using curl:

admin@ip-172-31-48-33:~$ curl localhost:3000
<!doctype html>
...

Use NGINX as a proxy to your server

Install and configure

sudo apt-get install nginx
sudo service nginx start

Create a conf file for your app

nano /etc/nginx/conf.d/myapp.com.conf

Change the server name and paste:

server {
    listen 80;

    server_name ec2-52-4-202-12.compute-1.amazonaws.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Restart Nginx service nginx restart

Your server is now available to the world !

mean-app-kit

This Angular Single Page app is not very MEAN.. Let's grab our custom mean-app-kit

git clone https://github.com/johnthillaye/mean-app-kit
cd mean-app-kit

Install everything:

npm install
bower install
gulp build

Launch our server with PM2:

pm2 start app.js --name mean-app

We now have a full MEAN app up and running

Ressources

Docs

AMIs

AMI Name AMI ID
MongoNodeDebian ami-b7d177dc
MongoNodeNGINXDebian ami-098d2b62
MEAN-App-Debian ami-3593355e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment