MongoDB PHP Extension
This tutorial will guide you through the installation of the MongoDB extension on Linux, Windows, and Mac platforms.
Installing MongoDB PHP Extension on Linux
Installing via Terminal
You can install the MongoDB PHP extension driver in Linux by executing the following command:
$ sudo pecl install mongodb
Using the pecl installation command for php requires a network connection and root privileges.
Installation Guide
If you prefer to compile the extension driver from source, you must manually compile the source package. The advantage of this method is that the latest bug fixes are included in the source package.
You can download the MongoDB PHP driver package from the PHP official website, download link: http://pecl.php.net/package/mongodb.
The complete installation commands are as follows:
$ wget http://pecl.php.net/get/mongodb-1.5.2.tgz
$ cd /mongodb-1.5.2
$ phpize
$ ./configure
$ make && make install
If your php is compiled by yourself, the installation method is as follows (assuming it is compiled in the /usr/local/php directory):
$ wget http://pecl.php.net/get/mongodb-1.5.2.tgz
$ cd /mongodb-1.5.2
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make && make install
After successful installation, you will see output similar to the following installation directory information:
...
Installing shared extensions: /usr/lib/php/extensions/debug-non-zts-20151012/
After executing the above commands, you need to modify the php.ini file and add the mongo configuration to it, as follows:
extension_dir=/usr/lib/php/extensions/debug-non-zts-20151012/
extension=mongodb.so
Note: You need to specify the path of the extension_dir configuration item.
You can view the directory address with the following command:
$ php -i | grep extension_dir
extension_dir => /usr/lib/php/extensions/debug-non-zts-20151012 =>
/usr/lib/php/extensions/debug-non-zts-20151012
Installing MongoDB PHP Extension on Windows
Precompiled php mongodb driver binaries for the Windows platform are available on PECL (download link: https://pecl.php.net/package/mongodb). You can download the version corresponding to your php, but you need to pay attention to the following issues:
- VC6 is for running on Apache servers.
- Thread safe (thread-safe) runs as a module on Apache. If you run PHP in CGI mode, choose the non-thread-safe mode (non-thread safe).
- VC9 is for running on IIS servers.
After downloading the binary package you need, unzip the compressed package and add the php_mongodb.dll file to your PHP extension directory (ext). The ext directory is usually located under the PHP installation directory.
Open the php configuration file php.ini and add the following configuration:
extension=php_mongodb.dll
Restart the server.
Access phpinfo through a browser. If the installation is successful, you will see information similar to the following:
Installing MongoDB PHP Extension Driver on Mac
You can install the MongoDB PHP extension driver using autoconf
.
You can install the MongoDB PHP extension driver using Xcode
.
If you are using XAMPP, you can install the MongoDB PHP extension driver with the following command:
sudo /Applications/XAMPP/xamppfiles/bin/pecl install mongo
If the above command does not work in XMPP or MAMP, you need to download a compatible precompiled package from Github.
Then add the configuration extension=mongodb.so
to your php.ini
file.