Rachel Wood

Rachel Wood

1574137331

How to Deploy a MERN Stack Application to Amazon EC2?

In this tutorial we’re going to setup a production ready web server from scratch on the Amazon EC2 (Elastic Compute Cloud) service, then deploy a custom MERN Stack application to it that supports user registration and authentication.

What is a MERN Stack App?

A MERN Stack application is made up of a front-end app built with React that connects to a back-end api built with Node.js + Express + MongoDB, hence the name MERN Stack (Mongo, Express, React, Node). Other variations of the stack include the MEAN Stack that has an Angular front-end, and the MEVN Stack that has a Vue.js front-end.

Scope of this tutorial

This tutorial will be focused on setting up the server on AWS EC2, then deploying and configuring the front-end and back-end pieces of the MERN stack app to work together.

Tutorial Contents

  1. Create a new Ubuntu server on AWS EC2
  2. Connect to Ubuntu EC2 instance via SSH
  3. Setup server with Node.js + MongoDB + NGINX
  4. Deploy Node.js + MongoDB back-end API
  5. Deploy React + Redux Front-end app
  6. Configure NGINX to serve API and front-end
  7. Test the MERN Stack app in a browser

1. Create a new Ubuntu Server on AWS EC2

Before doing anything we need a server that we can work on, follow these steps to spin up a new Ubuntu 18.04 server instance on AWS EC2.

  1. Sign into the AWS Management Console at https://aws.amazon.com/console/. If you don’t have an account yet click the “Create a Free Account” button and follow the prompts.
  2. Go to the EC2 Service section.
  3. Click the “Launch Instance” button.
  4. Choose AMI - Check the “Free tier only” checkbox, enter “Ubuntu” in search box and press enter, then select the “Ubuntu Server 18.04” Amazon Machine Image (AMI).
  5. Choose Instance Type - Select the “t2.micro” (Free tier eligible) instance type and click “Configure Security Group” in the top menu.
  6. Configure Security Group - Add a new rule to allow HTTP traffic then click “Review and Launch”.
  7. Review - Click Launch
  8. Select “Create a new key pair”, enter a name for the key pair (e.g. “my-aws-key”) and click “Download Key Pair” to download the private key, you will use this to connect to the server via SSH.
  9. Click “Launch Instances”, then scroll to the bottom of the page and click “View Instances” to see details of the new Ubuntu EC2 instance that is launching.

2. Connect to Ubuntu EC2 Instance via SSH

Once the EC2 instance reaches a running state you can connect to it via SSH using the private key downloaded in the previous step.

  1. Open a terminal window and update the permissions of the private key file with the command chmod 400 <path-to-key-file> e.g. chmod 400 ~/Downloads/my-aws-key.pem, the key must not be publicly viewable for SSH to work.
  2. Copy the “Public DNS (IPv4)” property from the instance description tab in the AWS Console, then connect to the instance from the terminal window with the command ssh -i <path-to-key-file> ubuntu@<domain name> e.g. ssh -i ~/Downloads/my-aws-key.pem ubuntu@ec2-52-221-185-40.ap-southeast-2.compute.amazonaws.com
  3. Enter yes to the prompt “Are you sure you want to continue connecting (yes/no)?” to add the url to your list of known hosts.

NOTE: If you’re using Windows you can connect to your instance via SSH using the PuTTY SSH client, for instructions see Connect Using PuTTY in the AWS docs.

3. Setup Web Server with Node.js + MongoDB + NGINX

The below command executes a script to automatically setup and configure a production ready MERN Stack web server on Ubuntu that includes Node.js, MongoDB, PM2, NGINX and UFW.

For more details about how the script works see Setup Node.js + MongoDB Production Server on Ubuntu.

While connected to the new AWS EC2 instance in the terminal window, run the following command:
curl https://gist.githubusercontent.com/cornflourblue/f0abd30f47d96d6ff127fe8a9e5bbd9f/raw/e3047c9dc3ce8b796e7354c92d2c47ce61981d2f/setup-nodejs-mongodb-production-server-on-ubuntu-1804.sh | sudo bash

4. Deploy Node.js + MongoDB Back-end API

Follow these steps to setup the Node.js API on the server and configure NGINX to enable access to it.

  1. Clone the Node.js + MongoDB API project into the /opt/back-end directory with the command sudo git clone https://github.com/cornflourblue/node-mongo-registration-login-api /opt/back-end
  2. Navigate into the back-end directory and install all required npm packages with the command cd /opt/back-end && sudo npm install
  3. Start the API using the PM2 process manager with command sudo pm2 start server.js

The API is now running on Node.js under the PM2 process manager and listening on port 4000. Only port 80 (HTTP) is publicly accessible on the server so we can’t hit the API yet, this will be possible after we’ve configured NGINX as a reverse proxy to pass through HTTP traffic to the api (more on this shortly).

5. Deploy React + Redux Front-end app

Follow these steps to setup the React application on the server.

  1. Clone the React + Redux project into the /opt/front-end directory with the command sudo git clone https://github.com/cornflourblue/react-redux-registration-login-example.git /opt/front-end
  2. Navigate into the front-end directory and install all required npm packages with the command cd /opt/front-end && sudo npm install
  3. Update the app to use real backend API:
    • Run the command sudo nano /opt/front-end/src/index.jsx to open the main react entry file in the nano text editor.

    • Delete the following lines from the file to remove the fake backend that the react app uses by default:

      // setup fake backend
      import { configureFakeBackend } from './_helpers';
      configureFakeBackend();
      
    • Save the file by pressing ctrl + x and selecting Yes to save.

  4. Configure the path to API:
    • Run the command sudo nano /opt/front-end/webpack.config.js to open the webpack config file in the nano text editor.

    • Change the apiUrl config property to '/api' like below so it points to the public path we configured in NGINX in the previous section:

      // global app config object
      config: JSON.stringify({
          apiUrl: '/api'
      })
      
    • Save the file by pressing ctrl + x and selecting Yes to save.

  5. Build the front end app with the command sudo npm run build

The React app is now built and ready to be served from the /opt/front-end/dist directory, in the next step we’ll configure our NGINX web server to enable access to it.

6. Configure NGINX to serve the Node.js API and React front-end

Since our MERN Stack application is made up of two separate projects that both need to be accessed via the same port (HTTP on port 80), we’re going to use NGINX as our public facing web server to receive requests for both the front-end and back-end, and decide where to send each request based on its path. Requests beginning with the path /api/* will be proxied through to the Node.js api running on port 4000, while other requests will serve the React front-end app and associated files (js/css/images).

Follow these steps to configure NGINX for the MERN stack app.

  1. Delete the default NGINX site config file with the command sudo rm /etc/nginx/sites-available/default

  2. Launch the nano text editor to create an new default site config file with sudo nano /etc/nginx/sites-available/default

  3. Paste in the following config:

    server {
      listen 80 default_server;
      server_name _;
    
      # react app & front-end files
      location / {
        root /opt/front-end/dist;
        try_files $uri /index.html;
      }
    
      # node api reverse proxy
      location /api/ {
        proxy_pass http://localhost:4000/;
      }
    }
    
  4. Save the file by pressing ctrl + x and selecting Yes to save.

  5. Restart NGINX with the command sudo systemctl restart nginx

NGINX Config Reference

server { ... } defines a server block which contains the configuration for a virtual server within NGINX.

listen 80 default_server; uses the listen directive to configure the virtual server to accept requests on port 80 and sets it as the default virtual server on this NGINX server.

server_name _; uses the server_name directive to set the server name to an underscore (_) to make this server block a catch-all block that matches any domain name that doesn’t match another more specific server block. Since this example has only one server block it will match all domain names.

location / { ... } defines a location block which contains the configuration for requests that have a URI beginning with a forward slash (/), unless the request URI matches another more specific location block.

root /opt/front-end/dist; uses the root directive to set the root directory to the front end dist folder (/opt/front-end/dist) for requests matching this location block.

try_files $uri /index.html; uses the try_files directive to first check for the existence of a file matching the request URI ($uri) and returning it if there is one. If no file matches the request URI then it defaults to returning /index.html.

location /api/ { ... } defines a location block which contains the configuration for requests that have a URI beginning with /api/.

proxy_pass http://localhost:4000/; uses the proxy_pass directive to proxy requests beginning with /api/ through to the Node.js API running at http://localhost:4000/.

7. Test your new MERN Stack application running on AWS

Enter the hostname of your AWS EC2 instance in a browser to access and test your new MERN stack application.

The hostname is the “Public DNS (IPv4)” property located on the instance description tab in the AWS Console.

#MongoDB #Reactjs #Nodejs #Express #AWS

What is GEEK

Buddha Community

How to Deploy a MERN Stack Application to Amazon EC2?

Ajay Kapoor

1626068978

Top MERN Stack Development Company in India

PixelCrayons - Get MERN stack development services from certified full stack developers having 5+ years of experience. You can also hire dedicated team as your team extension on hourly or full time basis.

2X Faster Delivery
Strict NDA Terms
Flexible Engagement Models

Our MERN Stack Development Services

MERN stack includes the best JavaScript technologies. We have expertise in all four of them that enables us to deliver optimum MERN stack development services.

Stay ahead of competition with our professional, tailor-made & enterprise-grade MERN Stack development services. Our MERN Stack web development company combines development expertise with modern frameworks and technologies to address critical needs of global clients across industries.

#mern stack web development services #mern stack web development #mern stack development company #mern stack web development company #mern stack development services #mern stack companies

Ajay Kapoor

1625045880

Top MERN Stack Development Company in India

PixelCrayons: Get MERN stack development services from certified full stack developers having 5+ years of experience. You can also hire dedicated team as your team extension on hourly or full-time basis.

MERN stack includes the best JavaScript technologies. Our MERN stack web development company has expertise in all four of them that enables us to deliver optimum MERN stack development services.

Stay ahead of competition with our professional, tailor-made & enterprise-grade MERN Stack development services. Our MERN Stack development company India combines development expertise with modern frameworks and technologies to address critical needs of global clients across industries.

With 16+ years of domain expertise, 13800+ successful MERN Stack projects, & 6800+ happy customers, we have carved a niche in the MERN Stack development services.

Mern stack development company India

#mern stack companies #mern stack development company #mern stack development services #mern stack web development #mern stack web development company

Ajay Kapoor

1619588781

Mern Stack App Development: What Does the Future Hold?

Decided to go with Mern Stack but confused about its speed, productivity, and other things. Take off all the worries and go through the following analysis, giving you a clear vision to choose a mern stack for web applications development.

Mern stack is one of the most used stacks app development that makes a great impression on the app development technology solutions. It complies with ReactJS to deliver dynamic web applications.

Do you know? According to a 2020 developer survey, 42% of software developers used React.

Mern stack is very popular nowadays amongst developers who love using the stack after Mean stack. There is only a difference between both, and that is the use of Angular and React to create front-end web applications. Mean stack uses Angular, whereas Mern stack uses React.

React offers you the freedom to pick tools, libraries, and architecture for developing web applications, whereas Angular gives you less freedom and flexibility to build the applications.

If you are techno-friendly, you know that a stack is nothing but an amalgamation of frameworks, coding languages, tools, and libraries that developers use to create robust web applications.

Mern stack undoubtedly offers efficiency and productivity that help to build dynamic web applications. The future of Mern stack app development is bright as it is being used by developers worldwide. Moreover, it provides all the essential tools at a platform that helps to create swift and dynamic web applications.

It is more famous nowadays owing to the inclusion of the Javascript language.

Developers can use it to develop both sides, i.e., frontend and backend web applications. Hence the code is written in the same language, and that eliminates the feasibility of context switching.

You can also avail yourself benefits of the Mern stack by contacting a top Mern stack development company and provide your audience an exhilarating user experience.

Read the full blog here

#mern-stack-development #mern-stack-app-development #mern-stack-future #mern-stack-trends

Hertha  Mayer

Hertha Mayer

1595334123

Authentication In MEAN Stack - A Quick Guide

I consider myself an active StackOverflow user, despite my activity tends to vary depending on my daily workload. I enjoy answering questions with angular tag and I always try to create some working example to prove correctness of my answers.

To create angular demo I usually use either plunker or stackblitz or even jsfiddle. I like all of them but when I run into some errors I want to have a little bit more usable tool to undestand what’s going on.

Many people who ask questions on stackoverflow don’t want to isolate the problem and prepare minimal reproduction so they usually post all code to their questions on SO. They also tend to be not accurate and make a lot of mistakes in template syntax. To not waste a lot of time investigating where the error comes from I tried to create a tool that will help me to quickly find what causes the problem.

Angular demo runner
Online angular editor for building demo.
ng-run.com
<>

Let me show what I mean…

Template parser errors#

There are template parser errors that can be easy catched by stackblitz

It gives me some information but I want the error to be highlighted

#mean stack #angular 6 passport authentication #authentication in mean stack #full stack authentication #mean stack example application #mean stack login and registration angular 8 #mean stack login and registration angular 9 #mean stack tutorial #mean stack tutorial 2019 #passport.js

Best Mern Stack Development Company India & USA | Orion eSolutions

Bothe MEAN and MERN are full stack frameworks with Java coded components. The difference is that MEAN uses Angular JS while MERN uses the React JS developed by Facebook. Both aids developers to make reactive and intuitive UI. To understand which stack is the better one, we need to understand the underlying differences between them.

This is image title

DIFFERENCES BETWEEN MEAN AND MERN
MEAN: Components include Mongo DB, Angular JS, Express, and Node.
MERN: Components include Mongo DB, React JS, Express, and Node.
MEAN: JavaScript development stack.
MERN: Open source JavaScript library.
MEAN: Uses Typescript language.
MERN: Uses JavaScript and JSX.
MEAN: Copnent based architecture.
MERN: None.
MEAN: Regular DOM.
MERN: Virtual DOM.
MEAN: Steep learning curve.
MERN: Better documentation.
MEAN: Bidirectional data flow.
MERN: Unidirectional dataflow.
Both tech has high class features and immense functionality. The slight upper hand that MERN enjoys is in the learning curve. MERN is easier to grasp because the learning curve differs between Angular JS and React JS. Let us take a deeper dive into the benefits of MEAN and MERN stacks to understand the power of each of these stacks fully.

BENEFITS OF MEAN AND MERN
MEAN STACK
All types of applications can be developed easily.
Various plug ins and widgets have compatibility with this stack. For development that has a stricter time frame, this comes in handy.
The functionality skyrockets due to the availability of plug ins.
Developers enjoy community support since the framework is open source.
Real time testing is possible with the built-in tools.
A single language is used for back end and front end. This increases coordination and gets applications to respond faster.
MERN STACK
Front end and back end are covered by a single coding script.
The entire process can be completed using only JAVA and JSON.
Seamless development through the MVC architecture.
Real time testing through built-in tools.
Runs on an open source community and the source code can be easily modified.
According to Hacker Rank development skill report, 30% of developers went with Angular JS while 26% stayed with React JS. The report also mentions that 30% of the programmers wanted to learn React JS and 35.9% of developers prefer to develop using React JS, thus MERN stands slightly above MEAN when it comes to popularity.

As far as we know, in terms of ease of understanding and popularity, MERN is at the forefront now. Let us take a detailed comparison to understand who will win the race in 2021.

MEAN vs MERN : A DETAILED COMPARISON
Scalability, Security: Both MEAN and MERN are equally secure. However, in terms of scalability, MERN is at the forefront.

MVC: For enterprise level apps, a complete architecture needs to be maintained. MEAN is the better option for this.
UI: For an advanced and simple UI, MERN is the go-to stack. MERN facilitates user interaction.
CRUD: For CRUD (create, read, update, delete), MERN is the ideal stack. The React JS handles data changes quickly and has a good user interface as well.
Support: The Angular JS in MEAN supports HTTP calls and unites the back-end. Enterprise level app development will require third party.
libraries. On the other hand, React JS improves functionality through its supplementary libraries. MEAN scores slightly above in this section.
MEAN enhances the experience through the use of third party extensions while MERN would require additional configurations to do this.

In aspects of the learning curve, UI, scalability, and CRUD, MERN stack scores more than MEAN stack. However, in the aspects of community support and MVC MEAN stays ahead. In terms of security both are at par. However, the application of the stacks depend entirely on the business needs.

MEAN is more affordable, and is the first choice for startups and SMEs. Switching between clients and servers is easier. For real time web apps, MEAN is definitely the best choice. In MERN, the Virtual DOM enhances user experience and gets the developer’s work done faster. A stable code is maintained by React JS due to a unidirectional data flow. For coding for Android and IOS using JavaScript, MERN is definitely the way to go.

TAKE AWAY
Companies like Accenture, Raindrop, Vungle, Fiverr, UNIQLQ, and Sisense among others use MEAN in their tech stacks. Brands such as UberEats, Instagram, and Walmart use MERN stack. Both the stacks provide an incredible user experience. Stability and scalability can be achieved with both stacks.

From this we can conclude that enterprise level projects require MEAN over MERN. MERN makes rendering UI simpler. Both are reliable for a quick front end development.

MEAN is good for large scale application. MERN is good for faster development of smaller applications.

At Orion we have an excellent team that can help you with all your MEAN and MERN stack development needs.

Source Link

#full stack frameworks #mern stack development #mern stack development #mean stack application development #mean stack development services