As the title suggests, we are going to see how to host a MEAN stack app on the AWS EC2 instance server for free.
Before we get started with the tutorial, I will briefly introduce the technology stacks that we are going to use.
What is MEAN?M MongoDB – NoSQL database program.
E Express – Node.js web application framework.
A Angular – front-end web application platform.
As per Stack Overflow’s Developer Survey Results 2017, all the technologies of MEAN stack are among TOP 5 in Most Popular Technologies and we are going to host it on AWS which is also very popular among developers.
Amazon Web Services(AWS) provides on-demand cloud computing services. Services are free to join, pay only for what you use.
Amazon has the largest market share in the cloud business, leading Microsoft, Google, and IBM by a huge margin.
Once you sign up for AWS account you can start using their free tier services. On sign up, they will ask for your debit/credit card information. AWS debits small amount from your account which is credited back to your account after verification.
- AWS account
- Putty – SSH client on your local computer
- FileZilla – FTP client on your local computer
- MEAN App (GitHub https://github.com/linnovate/mean or you can use your own)
Let’s start with the tutorial.
1. Creating EC2 instance on AWS
- Sign in to your console
- Services → EC2
- Click Launch Instance
- Select suitable instance (I am going with Ubuntu Server 16.04 LTS (HVM), SSD Volume Type for this tutorial)
- You can keep all the defaults and jump to Configure security groups
- You will need to configure your inbound traffic rules. Click Edit & Add Rule.
Note: The below configuration will suffice for this tutorial purpose.
Please note that this configuration is not intended for production purpose.
You can read more about how to configure the security groups for production release.
|Custom TCP Rule||TCP||8080||0.0.0.0/0|
- Click review → click Launch
- A pop up asking you to download key pair will appear.
- Choose ‘Create a new key pair’ → give it a proper name
- Download key pair (Keep it in a safe location on your local drive.)
- Click Launch.
Your EC2 instance is now ready.
2. SSH into your EC2 instance using Putty
Using Putty we are going to access the terminal of the server and run few commands.
We are going to use the ‘key pair’ file that we earlier downloaded to access the server via Putty. The key pair file is in ‘.pem’ format, but we need it in ‘.ppk’ format. Putty comes with PuTTYgen where we can make the conversion.
- Open PuTTYgen → Click ‘Load’
- Select ‘.pem’ key pair file that you downloaded from AWS.
Note: when browsing for your pem file be sure to select All Files in the dropdown list that is located to the right of the File name field.
- Click ‘Save private key’.
- You will receive a warning message asking if you want to save this key without a passphrase. Select Yes.
- Provide a name for your ppk file and click save.
- Now, open your AWS account, Go to Services → EC2
- Click on running instances → Click on the instance you created in the earlier step.
- At bottom of your browser you can see many tabs, on the first tab with title ‘description’ → Find & Copy IPv4 Public IP
- Now open PuTTY.
- Paste the ‘IPv4 Public IP’ into the ‘Host Name (or IP address)’ input box.
- Enter ‘22’ into ‘Port’ input box.
- Now on the left side panel expand SSH → Click ‘Auth’
- Click browse in the ‘Private key file for authentication’ input box.
- Select the ‘.ppk’ key pair that you created using PuTTYgen.
- Click open
- Again Click open on PuTTY configuration Dialog box. You will see the terminal window, where it will ask you to enter the username ‘login as:’
- Sign in to your instance with username (generally ‘ubuntu’ for Ubuntu instance & ‘ec2-user’ for Linux instance) ‘login as: ubuntu’ hit enter.
Congrats! You have logged into your AWS server terminal.
3. Installing Node.js on AWS server
Now that you have access to your server’s terminal, we need to set up our environment to run our MEAN App. First, we need to install Node.js which will also install NPM
We have to run few commands for this installation as below.
- sudo apt-get update
- sudo apt-get install build-essential libssl-dev
- curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh
- source ~/.profile
- nvm ls-remote
- nvm install 8.6.0
- To check installation was correct run node -v
- It also installs npm , check it with npm -v
Now we have Node and NPM installed on our server.
4. Installing MongoDB
Again we have to run commands to install MongoDB.
- sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
- echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
- sudo apt-get update
- sudo apt-get install -y mongodb-org
- To start MongoDB as a service → sudo service mongod start
- To check status → sudo service mongod status
- To Stop Service → sudo service mongod stop
Great! MongoDB is up and running.
5. Clone your MEAN App repo onto the server.
- I am using this official MEAN stack repo: https://github.com/linnovate/mean, run following command to clone this repo on your server.
- git clone https://github.com/linnovate/mean.git
- cd mean
- npm install
Your MEAN App is now on the server and ready for deployment.
6. Transfer prod build dist on server.
It will not be possible to build prod dist on the server with 1GB ram, So we will build locally and transfer our dist to the server via FTP (FileZilla) .
- So make a build locally by running npm run build:prod
- Open FileZilla.
- Open Site Manager. File > Site Manager
- Click New site
- Enter your server’s IPv4 Public IP in ‘host’ field
- Select ‘SFTP – SSH File Transfer Protocol’
- Logon Type: ‘Key file’
- User: ‘ubuntu’ for Ubuntu server & ‘ec2-user’ for Linux server
- Select the key file that you earlier downloaded. (.ppk)
- Click connect
- After you are connected to your server navigate to the local folder where your ‘dist’ is located.
- Transfer this ‘dist’ folder to the server by clicking ‘Upload’ from the context menu.
- Make sure MongoDB service is running.
- After the transfer is complete Open putty, make sure you are in ‘mean’ directory then run npm run server:prod
- In your browser paste the IPv4 Public IP followed by :8080
Voila!! You have hosted your app & Launched it to the World.
Note: Be sure to stop your instance on AWS after you are done with tutorial and tested your app.
You get a limited number of hours per month with the free account, so don’t keep the server running all the time. If you exhaust your free usage, billing will start.
I am listing few references below, you should go through them for in-depth knowledge.