How to install MEAN on Ubuntu 20.04 LTS
Introduction
MEAN is a FOSS (Free and Open Source Software) JavaScript stack for developing frontend and backend software for websites. Everything in the MEAN stack supports JavaScript software, which means only one language is required for developing on the server (backend), and website (frontend). MEAN stands for MongoDB, ExpressJS, AngularJS, and NodeJS, which are individual technologies designed to work together.
1. Deploy Ubuntu Server
- Deploy a new Ubuntu 20.04 Vultr VPS instance.
- Follow our best practices guides:
Change to your sudo user for the remaining steps.
2. Install MongoDB
Update sources and install MongoDB.
$ sudo apt update && sudo apt install -y mongodb
Start and enable MongoDB on boot.
$ sudo systemctl start mongodb
$ sudo systemctl enable mongodb
3. Install NodeJS and Dependencies
Use the official nodesource script to get the latest version of NodeJS.
$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Install NodeJS, build dependencies, and git.
$ sudo apt install -y nodejs gcc g++ make git
Use npm to install yarn and gulp.
$ sudo npm install -g yarn
$ sudo npm install -g gulp
Clone the official MeanJS repository.
$ git clone https://github.com/meanjs/mean
Change to the MeanJS directory.
$ cd mean
Install dependencies with yarn.
$ yarn install
4. Test
Open the server.js file.
$ nano server.js
Replace the contents with the following code.
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const app = express();
app.use('/', (req, res) => {
MongoClient.connect("mongodb://localhost:27017/test", function(err, db){
db.collection('Example', function(err, collection){
collection.insert({ pageHits: 'pageHits' });
db.collection('Example').count(function(err, count){
if(err) throw err;
res.status(200).send('Page Hits: ' + Math.floor(count/2));
});
});
});
});
app.listen(3000);
console.log('Server running at http://localhost:3000/');
module.exports = app;
Start the server.
$ gulp
To verify the server is running and can access the database correctly, navigate to your page URL. Substitute 192.168.0.123 with your server's IP address.
You should see "Page Hits: " with an increasing number each time you refresh the page. This verifies the MEAN stack is functioning properly.
Conclusion
You have successfully installed a MEAN stack on your Ubuntu 20.04 LTS VPS. For more information about MEAN, and further guides, see the official documentation: