Installing MongoDB on cPanel / WHM / CentOS along with the PHP extension
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 customized by cPanel. Many commands were unavailable in bash, and some package installations were disabled in /etc/yum.conf (e.g., php* in the 'exclude' list blocked the installation of php-devel package, etc.). This document outlines the key steps to install MongoDB on such a system.
I referred to the official documentation on mongodb.com to install MongoDB on CentOS. At the time of writing this, the version provided in the documentation to install was 4.4.
Ref. https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
-
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 -
Install the MongoDB packages:
yum install -y mongodb-org -
Configure MongoDB to auto-start on system startup / reboot:
chkconfig mongod onOutput:
Note: Forwarding request to 'systemctl enable mongod.service'. -
Start MongoDB:
service mongod startOutput:
Redirecting to /bin/systemctl start mongod.service -
Install MongoDB PHP Extension
Note that cPanel has tools like
pecl,phpize, etc., located in/opt/cpanel/ea-php74/root/usr/bin/. Change the version in "ea-php*" according to your PHP version. Multiple versions of PHP might be installed, so choose the version used for the project.To run
phpizefollowed by./configurewhile compiling a package yourself, use:/opt/cpanel/ea-php74/root/usr/bin/phpize && ./configure --with-php-config=/opt/cpanel/ea-php74/root/usr/bin/php-configPHP extension installation with the
pecltool:/opt/cpanel/ea-php74/root/usr/bin/pecl install mongodbRef. https://www.php.net/manual/en/mongodb.installation.pecl.php
Note that some references suggest installing
'mongo'as the extension, but it's the old extension. With PHP 7, you need to install'mongodb'as the extension.A successful installation should end with:
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.iniInfo: Location of
php.inion cPanel:/opt/cpanel/ea-*/root/etc/php.ini -
Restart Apache:
service httpd restart