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:
[bash]yum groupinstall “Development Tools”[/bash]
The above will install about 75 new development packages!
Next would be to install PHP mongo extension:
[bash]pecl install mongo[/bash]
Next add mongo.so to php.ini as a PHP extension:
[bash]vi /etc/php.ini[/bash]
Add the following lines and to a Apache restart:
[bash][mongo]
extension=mongo.so[/bash]
Now you have to test whether PHP mongo extension is working:
[bash]vi info.php[/bash]
Add the following lines of code:
[php]<?php
ini_set(‘display_errors’, ‘1’);
$m = new Mongo();
print_r($m);
?>[/php]
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:
[bash]/usr/sbin/setsebool -P httpd_can_network_connect 1[/bash]
Don’t worry this command takes about 1/2 a minute to execute, but it works!
Below is what you get as output:
Mongo Object
(
[connected] => 1
[status] =>
[server:protected] =>
[persistent:protected] =>
)
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!
Leave a Reply