How to Install MEAN Stack on Ubuntu 22.04

ComputingPost
6 min readDec 4, 2022

--

The MEAN stack is a free and open-source JavaScript-based framework used for developing web applications. The MEAN is made from four key technologies MongoDB, Express, Angular, and Node. The stack is designed to make building web applications in JavaScript and handling JSON incredibly easy. You can use MEAN applications in many ways. However, it is particularly suited to real-time applications, particularly those running natively in the cloud and single-page (dynamic) web applications built in Angular.js.

In this post, we will show you how to install the MEAN stack on Ubuntu 22.04 server.

Prerequisites

  • A server running Ubuntu 22.04.
  • Valid domain name pointed with your server IP.
  • A root password is configured on the server.

Getting Started

Before starting, it is recommended to update all the system packages to the latest version. You can update all of them with the following command:

apt update -y

apt upgrade -y

Once all the packages are updated, install the required dependencies using the following command:

apt install python3 dirmngr gnupg apt-transport-https ca-certificates software-properties-common -y

Once all the dependencies are installed, you can proceed to the next step.

Install MongoDB

By default, the MongoDB package is not included in the Ubuntu default repository. So you will need to add the MongoDB repository to your server.

First, download and add the MongoDB key with the following command:

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -

Next, add the MongoDB repository to APT with the following command:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Next, download and install the libssl dependency with the following command:

wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb

dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb

Next, update the repository and install the MongoDB package with the following command:

apt-get update -y

apt-get install mongodb-org -y

After the successful installation, start and enable the MongoDB service using the following command:

systemctl start mongod

systemctl enable mongod

Next, check the status of the MongoDB service using the following command:

systemctl status mongod

You will get the following output:

? mongod.service - MongoDB Database Server

Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)

Active: active (running) since Sun 2022-10-30 07:30:10 UTC; 5s ago

Docs: https://docs.mongodb.org/manual

Main PID: 2995 (mongod)

Memory: 60.5M

CPU: 1.050s

CGroup: /system.slice/mongod.service

??2995 /usr/bin/mongod --config /etc/mongod.conf



Oct 30 07:30:10 ubuntu2204 systemd[1]: Started MongoDB Database Server.

Once you are finished, you can proceed to install Node.js.

Install Node.js

By default, the Node.js package is not included in the Ubuntu default repository. So you will need to install it from the Node source repository.

First, add the Node source repository using the following command:

curl -sL https://deb.nodesource.com/setup_18.x | bash -

Next, install the Node.js package using the following command:

apt-get install nodejs -y

Once the Node.js package is installed, you can verify the Node.js version using the following command:

node -v

You will get the following output:

v18.12.0

Next, install the Yarn, Gulp and PM2 package using the NPM:

npm install -g yarn

npm install -g gulp

npm install pm2 -g

Once you are finished, you can proceed to the next step.

Download and Install MEAN Stack

First, you will need to download the latest version of MEAN stack from the Git repository. You can download it using the following command:

git clone https://github.com/meanjs/mean

Once the download is completed, navigate to the downloaded directory and install all the required dependency using the following command:

cd mean

yarn install

Once all the dependencies are installed, you can proceed to the next step.

Create MEAN Application

In this section, we will create a simple MEAN application.

To do so, create a MEAN application inside the mean directory with the following command:

nano server.js

Add 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;

Save and close the file then start the application using the PM2:

pm2 start server.js

You will get the following output:

Runtime Edition



PM2 is a Production Process Manager for Node.js applications

with a built-in Load Balancer.



Start and Daemonize any application:

$ pm2 start app.js



Load Balance 4 instances of api.js:

$ pm2 start api.js -i 4



Monitor in production:

$ pm2 monitor



Make pm2 auto-boot at server restart:

$ pm2 startup



To go further checkout:

http://pm2.io/





-------------



[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2

[PM2] PM2 Successfully daemonized

[PM2] Starting /root/mean/server.js in fork_mode (1 instance)

[PM2] Done.

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

? id ? name ? namespace ? version ? mode ? pid ? uptime ? ? ? status ? cpu ? mem ? user ? watching ?

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

? 0 ? server ? default ? 0.6.0 ? fork ? 5644 ? 0s ? 0 ? online ? 0% ? 45.2mb ? root ? disabled ?

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Next, enable the MEAN application to start it after the system reboot:

pm2 startup

You will get the following output:

Target path

/etc/systemd/system/pm2-root.service

Command list

[ 'systemctl enable pm2-root' ]

[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service

[PM2] Making script booting at startup...

[PM2] [-] Executing: systemctl enable pm2-root...

Created symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service → /etc/systemd/system/pm2-root.service.

[PM2] [v] Command successfully executed.

+---------------------------------------+

[PM2] Freeze a process list on reboot via:

$ pm2 save



[PM2] Remove init script via:

$ pm2 unstartup systemd

By default, MEAN application listens on port 3000. You can check it with the following command:

ss -antpl | grep 3000

You will get the following output:

LISTEN 0      511                *:3000             *:*    users:(("node /root/mean",pid=5644,fd=20))

Once you are finished, you can proceed to the next step.

Install and Configure Nginx for MEAN Application

First, install the Nginx web server package with the following command:

apt-get install nginx -y

Once the Nginx is installed, create an Nginx virtual host configuration file using the following command:

nano /etc/nginx/conf.d/mean.conf

Add the following configurations:

server 

listen 80;



server_name mean.example.com;

access_log /var/log/nginx/mean-access.log;

error_log /var/log/nginx/mean-error.log;



location /

proxy_set_header X-Forwarded-Host $host;

proxy_set_header X-Forwarded-Server $host;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://localhost:3000/;

Save and close the file, then verify the Nginx for syntax errors with the following command:

nginx -t

You should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, restart the Nginx service to apply the changes.

systemctl restart nginx

You can also check the Nginx status using the following command:

systemctl status nginx

You should get the following output:

? nginx.service - A high performance web server and a reverse proxy server

Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)

Active: active (running) since Sun 2022-10-30 07:37:24 UTC; 4s ago

Docs: man:nginx(8)

Process: 6416 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)

Process: 6417 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)

Main PID: 6418 (nginx)

Tasks: 3 (limit: 4579)

Memory: 3.3M

CPU: 49ms

CGroup: /system.slice/nginx.service

??6418 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"

??6419 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

??6420 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""



Oct 30 07:37:24 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...

Oct 30 07:37:24 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

Once you are finished, you can proceed to the next step.

Access MEAN Application

At this point, Nginx is installed and configured as a reverse proxy for the MEAN application. You can now access the MEAN application using the URL http://mean.example.com. You should see the MEAN web interface on the following screen:

Conclusion

Congratulations! you have successfully installed and configured the MEAN stack on Ubuntu 22.04. You can now start developing applications using the MEAN stack and host it on the production server. Feel free to ask me if you have any questions.

https://www.computingpost.com/how-to-install-mean-stack-on-ubuntu-22-04/?feed_id=21120&_unique_id=638c75ed3d16d

--

--

ComputingPost

ComputingPost — Linux Howtos, Tutorials, Guides, News, Tips and Tricks.