Installing MongoDB on cPanel / WHM / CentOS along with the PHP extension

  • by

Attempts to install MongoDB on a cPanel based CentOS VPS

While trying to install MongoDB on a cPanel based CentOS server, I found the system configurations were customised by cPanel. And because of which a lot of commands were not available in bash. Also the installation of some packages was disabled in the /etc/yum.conf (e.g. php* in the ‘exclude’ list blocked the installation of php-devel package, etc.). This is a documentation of the key steps to install MongoDB on such a system.

I referred the official doc on mongodb.com to install mongodb on CentOS. So at the time of writing this, the version provided in the doc to install was 4.4.

Please note that I did the installation as ‘root’ user.

Ref. https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/

1.Create a repo file in /etc/yum.repos.d called mongodb-org-4.4.repo with this content:

[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

2. Install the MongoDB packages

yum install -y mongodb-org

3. Configure MongoDB to auto-start on system startup / reboot

chkconfig mongod on

Output:

Note: Forwarding request to 'systemctl enable mongod.service'.

4. Start MongoDB

service mongod start

Output:

Redirecting to /bin/systemctl start mongod.service

5. Install MongoDB PHP Extension

Note that cPanel has the tools like pecl, phpize, etc. in the following location. Change the version in “ea-php*” as per your PHP version. There could be multiple versions of PHP installed, so choose the version you are using for the project.

/opt/cpanel/ea-php74/root/usr/bin/

So to run phpize followed by ./configure while compiling a package yourself, use something like:

/opt/cpanel/ea-php74/root/usr/bin/phpize && ./configure --with-php-config=/opt/cpanel/ea-php74/root/usr/bin/php-config

PHP extension installation with the pecl tool:

/opt/cpanel/ea-php74/root/usr/bin/pecl install mongodb

Ref. https://www.php.net/manual/en/mongodb.installation.pecl.php

Note that some references ask to install ‘mongo‘ as the extension, but it is the old extension, and with PHP 7 you need to install ‘mongodb‘ as the extension.

A successful installation should end with something like:

Build process completed successfully
Installing '/opt/cpanel/ea-php74/root/usr/lib64/php/modules/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.9.0
Extension mongodb enabled in php.ini

Info: Location of php.ini on cPanel:

/opt/cpanel/ea-*/root/etc/php.ini

6. Restart Apache

service httpd restart

Leave a Reply

Your email address will not be published. Required fields are marked *