Mongo web based admin UI

January 28, 2023

mongo-express is a web-based MongoDB admin interface written with Node.js, Express and Bootstrap3. Link : https://github.com/mongo-express/mongo-express

Installation

You could use either a docker or npm. In this post I will use my favorite tool : npm 🤓 Run the following command to install globally:

npm install -g mongo-express

You will need to do some changes in the config.js, first you can run the following command to find the installation folder:

npm -g list

Then we will copy the defaut config (on my server, the installation path is : /usr/lib/node_modules/mongo-express):

cp /usr/lib/node_modules/mongo-express/config.default.js /usr/lib/node_modules/mongo-express/config.js

In config.js you will need to modifiy at least the basicAuth:

  basicAuth: {
    username: getFileEnv(basicAuthUsername) || 'yourSuperUsername',
    password: getFileEnv(basicAuthPassword) || 'yourSuperPassword',
  },

Note that you can also use an .env file:

touch .env

Add the following environement variables:

ME_CONFIG_MONGODB_ADMINUSERNAME: 'yourSuperUsername'
ME_CONFIG_MONGODB_ADMINPASSWORD: 'yourSuperPassword'

Finally test your installation by running :

mongo-express --url mongodb://127.0.0.1:27017
curl http://localhost:8081

PM2

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Installation:

npm install pm2 -g

We will now run mongo-express with pm2:

pm2 start `/usr/lib/node_modules/mongo-express/app.js` --name mongo-express

Reverse proxy

Now that you have installed and run mongo-express, how do you access it ? If you're running it locally it is pretty straight forward, simply visit : http://localhost:8081

But if mongo-express is installed on a remote server, you can use a nginx reverse proxy by configuring : /etc/nginx/sites-available/example.com; replace example.com by your domain. Imagine that you want to access mongo-express from https://example.com/mongo-express :

location /mongo-express {
                proxy_pass http://localhost:8081;
                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;
                #Lines below are optional but recommanded. 
                #Replace by your WAN IP to restrict access.
                allow X.X.X.X 
                deny all;
}

Let's check that our new config is ok:

sudo nginx -t

Finally restart nginx :

sudo systemctl restart nginx

Oh snap ! If you try to visit 'https://example.com/mongo-express' you get the following error : Cannot GET /mongo-express

To fix this we have to chagne the baseUrl in mongo-express config.js:

baseUrl: process.env.ME_CONFIG_SITE_BASEURL || '/mongo-express',

🎉 Have fun 🎉


Profile picture

Written by Olivier Bonnet who lives and works in Montreal writing a blog that nobody reads 🤪...

© Bonneto 2024