Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
18.09.2025

How to Create MongoDB on VPS

Set Up MongoDB on Your AlexHost VPS: A Step-by-Step Guide

Why run MongoDB on AlexHost? MongoDB’s NoSQL flexibility and JSON-like documents make it a go-to for dynamic apps, and AlexHost’s VPS offers the perfect environment—root access, SSD speed, and robust security—to run it like a champ. This guide walks you through installing, securing, and managing MongoDB on an Ubuntu 20.04 VPS, ensuring a scalable, high-performance database setup.

Prerequisites

Before you start, make sure you have the following:

  1. A VPS with root access.
  2. Ubuntu 20.04 or a similar Linux distribution.
  3. SSH access to your VPS.
  4. At least 2GB RAM for MongoDB (recommended).
  5. Basic knowledge of the command line interface (CLI) and Linux commands.

Step 1: Update the System

First, make sure your VPS is up to date. Log in over SSH and run the following commands to update your system’s package list and install pending updates:

sudo apt update
sudo apt upgrade -y

Once the system has been updated, it is good practice to restart the server to ensure that all updates have been properly applied:

sudo reboot

Step 2: Install MongoDB

MongoDB is not included in the default Ubuntu repositories, so you need to add its official repository before installing it. Here’s how to install MongoDB:

Adding the MongoDB Repository

  1. Import the MongoDB public GPG key:
    wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
  2. Create a list file for MongoDB:
    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
  3. Update your local package list:
    sudo apt update

Installing MongoDB Packages

Now, install the MongoDB packages by running the following command:

sudo apt install -y mongodb-org

This installs MongoDB along with other necessary components (such as mongod, MongoDB daemon).

Step 3: Launch and Activate MongoDB

Once installed, you need to start MongoDB and get it running at startup:

sudo systemctl start mongod
sudo systemctl enablemongod

Check the status of MongoDB to verify that it is running:

sudo systemctl status mongod

You should see MongoDB listed as active (running). If everything looks good, you are ready to proceed with the configuration.

Step 4: Secure MongoDB

By default, MongoDB allows unauthenticated access, which is not ideal for a production environment. To secure MongoDB, you must enable authentication.

Create Administrator User

  1. First, access the MongoDB shell:
    mongo
  2. Switch to the administrator database:
    use manager
  3. Create an admin user by running the following command and replacing adminuser and password with the desired username and password:
    db.createUser({
    kullanıcı: "adminuser",
    pwd: "Şifre",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    })
  • Exit the MongoDB shell:
    eXIT

Enable Authentication

  1. Open the MongoDB configuration file with a text editor:
    sudo nano /etc/mongod.conf
  2. Find the following line in the configuration file:
    #safety
  3. Remove the comment and add the following line below it:
    security
    empowerment: effective
  4. Save the changes and exit the editor (press Ctrl X on Nano, then Y and Enter ).
  5. Restart MongoDB to apply the changes:
    sudo systemctl restart mongod

Step 5: Configure MongoDB Remote Access (Optional)

By default, MongoDB only listens on localhost (127.0.0.0.1), meaning it can only be accessed from the VPS itself. If you need remote access, you need to configure MongoDB to allow connections from external IP addresses. Here’s what we will do:

  1. Open the MongoDB configuration file:
    sudo nano /etc/mongod.conf
  2. Find the line below:
    bindIp: 127. 0. 0.0.1
  3. Replace it with this one:
    bindIp: 0. 0. 0.0.0

    This ensures that MongoDB accepts connections from all IP addresses.

  4. Save the changes and exit the editor.
  5. Restart MongoDB:
    sudo systemctl restart mongod

Secure Remote Access with Firewall

Configure your VPS firewall to allow only trusted IP addresses to connect to MongoDB. If you are using UFW (Uncomplicated Firewall), here’s how to allow remote connections on MongoDB’s default port (27017):

  1. Allow access from a specific IP (replace your_ip with your IP address):
    sudo ufw allow from your_ip to any port 27017
  2. To enable UFW (if not enabled):
    sudo enableufw
  3. Check the UFW status to verify that the rule has been added:
    sudo ufw status

Step 6: Test MongoDB Installation

To verify that MongoDB is working correctly, you can access the MongoDB shell and authenticate using the user you created earlier:

  1. Connect to MongoDB:
    mongo -u adminuser -p --authenticationDatabase admin
  2. You should now be logged into the MongoDB shell as an admin user.

Step 7: Backup and Maintenance

It is very important to back up your MongoDB databases regularly, especially in a production environment. You can use the mongodump tool to back up your data:

mongodump --out /path/to/backup/directory

You can restore the data using mongorestore:

mongorestore /path/to/backup/directory

Conclusion: MongoDB + AlexHost = Database Powerhouse

Setting up MongoDB on your AlexHost VPS is straightforward—install, secure, and back up for a robust NoSQL database ready for your apps. With AlexHost’s SSDs and root access, you get top-tier performance and control. Run mongo, create users, and automate backups to keep your data safe. Whether it’s a web app or big data project, you’re now set to dominate. AlexHost support’s got your back—happy coding!

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills