<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mohit Sehgal</title>
    <description>The latest articles on Forem by Mohit Sehgal (@mohitsehgl).</description>
    <link>https://forem.com/mohitsehgl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F432442%2Fa1192654-4149-4555-afc4-72a5197cb70c.jpg</url>
      <title>Forem: Mohit Sehgal</title>
      <link>https://forem.com/mohitsehgl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mohitsehgl"/>
    <language>en</language>
    <item>
      <title>How To Easily Set Up A MEVN Stack Server</title>
      <dc:creator>Mohit Sehgal</dc:creator>
      <pubDate>Sun, 27 Sep 2020 18:50:15 +0000</pubDate>
      <link>https://forem.com/mohitsehgl/how-to-easily-set-up-a-mevn-stack-server-16e3</link>
      <guid>https://forem.com/mohitsehgl/how-to-easily-set-up-a-mevn-stack-server-16e3</guid>
      <description>&lt;p&gt;MEVN stack is a tech stack where you use MongoDB as your DB, Express.JS/ Node.JS as backend, and Vue.JS frontend. Once you are done with the app development. You need to deploy it. Here is the easy guide to doing that.&lt;/p&gt;

&lt;h1&gt;
  
  
  Which OS?
&lt;/h1&gt;

&lt;p&gt;When you talk about server setup of some stack, the first thing that comes to your mind is Server OS. Which server OS do you want? In the case of MEVN Stack, Linux is most preferable due to various reasons.&lt;/p&gt;

&lt;h1&gt;
  
  
  Which Linux distro?
&lt;/h1&gt;

&lt;p&gt;There are some great options here but I personally prefer Ubuntu. It is because Ubuntu is easy to set up and has great community support. Most of the tools and libraries needed for MEVN Stack are easily available.&lt;/p&gt;

&lt;h1&gt;
  
  
  1. SSH to the Server
&lt;/h1&gt;

&lt;p&gt;SSH is the protocol used to access the server in a secure manner. You can SSH using SSH Key or using a password. SSH Key is preferred for better security.&lt;/p&gt;

&lt;p&gt;Here is the simple command in order to get SSH access to the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh -i "MyServer.pem" ubuntu@server-ip
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  2. Install MongoDB
&lt;/h1&gt;

&lt;h2&gt;
  
  
  a) Run this command
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will copy MongoDB’s public GPG Key. The version in the above command will change according to the latest version. Refer to official MongoDB docs for more on this.&lt;/p&gt;

&lt;h2&gt;
  
  
  b) Create a sources list file for MongoDB
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the official URL of MongoDB Community Edition and related packages.&lt;/p&gt;

&lt;h2&gt;
  
  
  c) Reload the local package database using the following command
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  d) Install MongoDB using the following command
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get install -y mongodb-org
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will install the latest version of Community Edition of MongoDB&lt;/p&gt;

&lt;h2&gt;
  
  
  e) Start MongoDB using the following command
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo systemctl start mongod
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will initiate MongoDB service.&lt;/p&gt;

&lt;p&gt;After this process, you need to set up user authentication on MongoDB. I will discuss this in a separate post.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Install NodeJS
&lt;/h1&gt;

&lt;p&gt;There are many ways to install NodeJS on a typical Linux Distro. But as promised in the title I will give you the easiest way. Run the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt install nodejs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Done!! Sweet.&lt;/p&gt;

&lt;p&gt;You can check the version of NodeJS using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ node -v
v13.10.1

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the latest stable version of Node.JS on Ubuntu as of this writing.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Install npm
&lt;/h1&gt;

&lt;p&gt;If you are familiar with Node.JS, then definitely you cannot do without npm.&lt;/p&gt;

&lt;p&gt;NPM is the world’s largest software repository for publishing open-source packages. It acts as a package manager when you work with the “real-world” Node.JS app.&lt;/p&gt;

&lt;p&gt;Run this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt install npm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Just like Node.JS, you can check the latest version of npm on your machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm -v
6.14.4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  5. Install PM2
&lt;/h1&gt;

&lt;p&gt;PM2 is an advanced and most preferred process manager for Node.JS Apps.&lt;/p&gt;

&lt;p&gt;Note for beginners: As you know you can run Node script using node script.js to run any Node Script. But as soon as you close the terminal window. Your node script will come to a halt. PM2 enables you to keep your script running even when you disconnect from your terminal. Although very naive, this is the most basic function of PM2.&lt;/p&gt;

&lt;p&gt;As the home page of PM2 mentions. It needs a very simple command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install pm2 -g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you want to run your Node App. Go to the base directory of your app in terminal using cd. Suppose if your script file is index.js. Then run this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pm2 start index.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will run your Node Project in the background.&lt;/p&gt;

&lt;p&gt;You can also run multiple Node Scripts on the same server. You can list all the currently running PM2 processes by this simple command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pm2 list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://practicaldev-herokuapp-com.freetls.fastly.net/blogimages/pm2-list.png" class="article-body-image-wrapper"&gt;&lt;img src="https://practicaldev-herokuapp-com.freetls.fastly.net/blogimages/pm2-list.png" alt="List all PM2 Processes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  6. Install NGINX
&lt;/h1&gt;

&lt;p&gt;NGINX is open-source software that can be used as a web server, reverse proxy, load balancer, or all of these.&lt;/p&gt;

&lt;p&gt;Installing NGINX is very simple using ubuntu’s package manager.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt install nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You need to allow it on your firewall. Assuming that you are using &lt;code&gt;ufw&lt;/code&gt;. Here is the command to do that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo ufw allow 'Nginx HTTP'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, you need to start the Nginx, using the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo systemctl start nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here’s an additional step that you need to do in order to start Nginx on each boot. (Because you want your web-server to be up and running all the time)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo systemctl enable nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now NGINX is running but it is not configured to your web-app yet.&lt;/p&gt;

&lt;p&gt;Important: Make sure your DNS server points the appropriate Domain to this server.&lt;/p&gt;

&lt;p&gt;Let’s say your Node script is running at port 3000.&lt;/p&gt;

&lt;p&gt;Open the default config file for NGINX.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo nano /etc/nginx/sites-available/default
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Scroll down using the arrow key to the server block of the file.&lt;/p&gt;

&lt;p&gt;Server block would of the form of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
//some lines of config
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you need to replace it with the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
  listen       80;
  server_name  www.mydomain.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;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Make sure you don’t override/ remove any other line of the file. Also, make sure you replace &lt;a href="http://www.mydomain.com"&gt;www.mydomain.com&lt;/a&gt; with your domain name.&lt;/p&gt;

&lt;p&gt;Save the file and exit.&lt;/p&gt;

&lt;p&gt;You can verify that your NGINX changes are fine using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nginx -t
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Tip: This command tests your current NGINX config for any errors. Make sure you do it every time you make any changes to your NGINX config.&lt;/p&gt;

&lt;p&gt;If you made changes correctly then it will give you success like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nginx -t
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;Tip&lt;/strong&gt;&lt;/em&gt; : &lt;em&gt;This command tests your current NGINX config for any errors. Make sure you do it every time you make any changes to your NGINX config.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you made changes correctly then it will give you success like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="///blogimages/nginx-conf-success.png"&gt;NGINX Conf Test Successful&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you are ready to deploy config changes to NGINX. Just restart or reload NGINX.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo service nginx reload
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That’s it your server configuration is done.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;This is the easiest way to set up your MEVN Stack server for development. If you are facing any issues then contact me on our email &lt;a href="mailto:appsyoda@gmail.com"&gt;appsyoda@gmail.com&lt;/a&gt; or comment below.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://appsyoda.com/blog/set-up-mevn-stack-server"&gt;https://appsyoda.com/blog/set-up-mevn-stack-server&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>nginx</category>
      <category>mongodb</category>
      <category>node</category>
      <category>ubuntu</category>
    </item>
  </channel>
</rss>
