Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save noob09/d1687563e4a33d14df12a9ca2f03853c to your computer and use it in GitHub Desktop.
Save noob09/d1687563e4a33d14df12a9ca2f03853c to your computer and use it in GitHub Desktop.

Revisions

  1. @johnthillaye johnthillaye renamed this gist Aug 2, 2015. 1 changed file with 0 additions and 0 deletions.
  2. @johnthillaye johnthillaye created this gist Aug 2, 2015.
    226 changes: 226 additions & 0 deletions MEAN.JS Full Stack Deployment Process to AWS EC2
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,226 @@
    # 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](https://wiki.debian.org/Cloud/AmazonEC2Image/Jessie)

    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 [email protected] -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

    * [AWS CLI Tool Documentation](http://docs.aws.amazon.com/cli/latest/reference/)
    * [Mongo Installation](http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/)
    * [Node Installation](https://github.com/joyent/node/wiki/installing-node.js-via-package-manager#debian-and-ubuntu-based-linux-distributions-)
    * [Gulp-Angular Generator](https://github.com/Swiip/generator-gulp-angular)
    * [mean-app-kit](https://github.com/johnthillaye/mean-app-kit)

    ### AMIs


    AMI Name | AMI ID
    ------------- | -------------
    MongoNodeDebian | ami-b7d177dc
    MongoNodeNGINXDebian | ami-098d2b62
    MEAN-App-Debian | ami-3593355e