How to Compile and Install the MongoDB PHP Extension
This guide walks through the step‑by‑step process of downloading, compiling, and enabling the MongoDB extension for PHP on a Linux server, covering both online and offline installation methods and how to verify the extension is loaded successfully.
The article explains how to compile and install a PHP extension, using the MongoDB extension as an example; the same procedure applies to other extensions.
Step 1: Open https://pecl.php.net and search for "mongodb".
Step 2: Download the latest stable version of the extension.
Step 3 (online): If the server has internet access, run the following commands:
wget https://pecl.php.net/get/mongodb-1.19.4.tgz
# Extract the archive
tar -zxvf mongodb-1.19.4.tgz
# Enter the directory
cd mongodb1.19
# Prepare the build environment
phpize
# Configure with php-config
./configure --with-php-config=/usr/bin/php-config
# Compile and install
sudo make && make install
# Enable the extension in php.ini
extension=mongodb.so
# Restart the web server
service apache2 restart # or appropriate restart commandStep 3 (offline): If the server cannot access the internet, download the package locally, upload it to the server, and then execute the same sequence of commands (extract, phpize, configure, make, install, enable, restart). The commands are identical to the online case.
// Extract the file
tar -zxvf mongodb-1.19.4.tgz
// Enter the directory
cd mongodb1.19
// Prepare the build environment
phpize
// Configure
./configure --with-php-config=/usr/bin/php-config
// Compile and install
sudo make && make install
// Enable in php.ini
extension=mongodb.so
// Restart the server
service apache2 restartStep 4: Verify the installation by listing loaded PHP modules and locating the php.ini file:
php -m # should list mongodb among the modules
php --ini # shows the loaded configuration fileAfter completing these steps, the MongoDB extension will be installed and ready for use in PHP applications.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.