Part 2: Installing PHP mongo extension and make it work!
Before we install PHP mongo extension, we need to install a few development packages otherwise we might get errors like:
phpize: command not found
configure: error no acceptable c compiler found in $path
So the package(s) we’ll have to install is:
yum groupinstall "Development Tools"
The above will install about 75 new development packages!
Next would be to install PHP mongo extension:
pecl install mongo
Next add mongo.so to php.ini as a PHP extension:
Add the following lines and to a Apache restart:
Now you have to test whether PHP mongo extension is working:
Add the following lines of code:
<?php ini_set('display_errors', '1'); $m = new Mongo(); print_r($m); ?>
Remember to keep display_errors ‘On’ is absolutely critical here because PHP mongo extension is working but by default in certain setups (read most setups!), Apache doesn’t allow requests to make network connections which in turns throws the following error:
Fatal error: Uncaught exception ‘MongoConnectionException’ with message ‘Failed to connect to: localhost:27017: Unknown error’.
You’ll need to fix this by running the following command:
/usr/sbin/setsebool -P httpd_can_network_connect 1
Don’t worry this command takes about 1/2 a minute to execute, but it works!
Below is what you get as output:
[connected] => 1
Now mongoDB as well as a mongo cache system is ready to use.
DISCLAIMER: I have written the exact steps I followed and issues I faced while trying to install and configure mongoDB and PHP mongo extension on CentOS 6.3, this should mostly work in general but I hold no guarantee that it would definitely work! It was sheer joy which drove me to write this article after I saw the output without errors. I thought of sharing the experience with my fellow Open Source Developers across the Globe!