← ServerPilot Docs

How to Install MongoDB

To install the MongoDB NoSQL database server, SSH to your server as the root user and run the following commands:

apt-get install gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor

Determine which version of Ubuntu the server is running.

cat /etc/lsb-release

Referencing the output from above, run the command below that matches your version of Ubuntu.

# Ubuntu 24.04:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

# Ubuntu 22.04:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

# Ubuntu 20.04:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

Now the MongoDB package can be installed and started:

apt-get update
apt-get install -y mongodb-org
systemctl daemon-reload
systemctl enable mongod
systemctl start mongod

Once installed and running, MongoDB will listen on port 27017.

Accessing MongoDB from PHP

To interact with the MongoDB server from your PHP scripts, you need to install the PHP Mongo client extension.

Customizing Your Firewall for MongoDB

The firewall configured by ServerPilot will block all access to MongoDB from outside of your server. If you need to have other servers access MongoDB, you'll need to customize your firewall.

Learn more about customizing your firewall.

Last updated: December 23, 2024

Launch your first site in 5 minutes