How to Connect MongoDB to cPanel

How to Connect MongoDB to cPanel

🚀 How to Connect MongoDB to cPanel

MongoDB is a flexible NoSQL database, but it’s not included by default in WHM/cPanel. You’ll need to install MongoDB manually on the server and then connect it with PHP/your application.


✅ Step 1: Add MongoDB Repository

Log in as root via SSH and create a new repo file:

# cd /etc/yum.repos.d
# nano mongodb.repo 

Paste this inside:

[mongodb]
name=MongoDB Repo
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

Save & exit (CTRL+O, CTRL+X).


✅ Step 2: Install MongoDB Packages

Run:

#  yum install mongo-10gen mongo-10gen-server -y

✅ Step 3: Enable & Start MongoDB

# chkconfig mongod on
# service mongod start 

Check if it’s running:

# ps aux | grep mongod

✅ Step 4: Install PHP MongoDB Extension

To allow PHP apps (like Laravel, custom apps, etc.) to talk to MongoDB:

# pecl install mongo

Then enable the extension:

echo "extension=mongo.so" >> /usr/local/lib/php.ini

Restart Apache:

#  service httpd restart

✅ Step 5: Verify Installation

Check if MongoDB extension is active:

# php -i | grep mongo -i

✅ Step 6: Connect PHP App to MongoDB

In your PHP code (inside cPanel user account), you can connect like this:

<?php $mongoClient = new MongoClient("mongodb://localhost:27017"); $db = $mongoClient->selectDB("mydatabase"); echo "Connected to MongoDB!"; ?>

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0