Let's try installing the driver on a Windows machine running PHP 5.2 on Apache:
1. Download the ZIP archive http://downloads.mongodb.org/mongo-latest-php5.2vc6ts.zip on your machine and extract it.
2. Copy the
php_mongo.dll
file from the extracted folder to the PHP extension directory; this is usually the folder nameext
inside your PHP installation.3. Open the
php.ini
file inside your PHP installation and add the following line:extension=php_mongo.dll
4. Save the file and close it. Restart the Apache web server.
5. Open up your text editor and add the following code to a new file:
<?php phpinfo();
6. Save the file as
phpinfo.php
inside the DocumentRoot of the Apache web server (thehtdocs
folder).7. Execute the
phpinfo.php
script in your browser (http://localhost/phpinfo.php
). Scroll down to find the section mongo to see all the MongoDB driver-specific information.
Congratulations! You have successfully installed the PHP driver for MongoDB.
In step 1, we download the ZIP file containing the DLL file php_mongo.dll
for the PHP-MongoDB driver for PHP 5.2 (for the PHP 5.3 specific version, download http://downloads.mongodb.org/mongo-latest-php5.3vc6ts.zip instead). In step 2, we copy the php_mongo.dll
file to the PHP extensions directory. If the installation directory of PHP on your machine is C:\php
, the extension directory should be C:\php\ext
. Then we edit the php.ini
file (located under C:\php
as well) to add the line extension=php_mongo.dll
to it and restart Apache for the changes to take effect. Next we create and execute a one-line PHP script to invoke the phpinfo()
method. If we are able to see the MongoDB driver specific information in the phpinfo()
output, listed under section mongo, this means the driver was installed without a glitch.
Note
If you are running PHP on IIS, you should download the thread-safe VC9 version of the driver instead. Get it from the URL http://downloads.mongodb.org/mongo-latest-php5.3vc9ts.zip.
In a Unix-based system, the PHP driver for MongoDB can be installed using the pecl (PECL - PHP Extension Community Islam) program. You need to have it installed on your machine, which can be done by using the following command:
sudo pecl install mongo
When the installation is finished, edit the php.ini
file (usually found at /etc/php.ini)
to add the line:
extension=mongo.so
and then restart Apache.
In case you don't have pecl installed on your machine, you can download the driver source code from GitHub, build it, and install it manually:
$ tar zxvf mongodb-mongdb-php-driver-<commit_id>.tar.gz $ cd mongodb-mongodb-php-driver-<commit_id> $ phpize $ ./configure $ sudo make install
Check out the Mongo driver installation page http://www.php.net/manual/en/mongo.installation.php on the PHP official website to get operating system specific detailed information.