PHP unable to load dynamic library (mongo.so)
When attempting to integrate RockMongo with an existing PHP environment, you may encounter a frustrating error message stating that the php_mongo module is missing. Even if you have successfully installed the driver via PECL using sudo pecl install mongo, the application often refuses to start because the PHP runtime cannot locate or load the compiled extension. This specific failure usually stems from a mismatch between the directory where the shared object resides and the version string embedded in your PHP binary's configuration path.
In the scenario described, the system reports two distinct directories within /usr/lib/php5/: 20090626+lfs and 20100525+lfs. The former contains the compiled extension file (mongo.so), but the error logs explicitly point to the latter. This discrepancy indicates that your PHP binary was built with a specific build ID (20100525), yet the extension is being loaded from a directory corresponding to a different build ID (20090626).
The root cause of the cannot open shared object file error lies in how the PHP interpreter resolves library paths. When you configure an extension using extension=mongo.so in your php.ini, the loader searches for that file relative to the path hardcoded into the binary at compile time, not necessarily the current filesystem location unless a full path is provided.
To resolve this without manually editing every configuration file or relying on fragile relative paths, you must align the extension directive with the actual location of the shared object. The most robust approach is to specify the absolute path directly in the php.ini file:
extension=/usr/lib/php5/20090626+lfs/mongo.so
However, before applying this fix, a critical syntax error must be addressed. Your provided configuration snippet includes a line (\[mongo\]) that appears on the third line of the file. In standard php.ini files, section headers (like [mongo]) are generally ignored by the core loader and can often cause confusion or unexpected behavior in certain PHP versions. More importantly, if there is whitespace or invisible characters preceding this line, it can trigger a "syntax error" at startup.
Ensure that your configuration file strictly follows these rules:
- Remove any leading spaces or tabs before the
extensiondirective. - Delete the
[mongo]section header unless you are using specific directives that require them and know exactly which ones they map to. The standard MongoDB extension often does not require a named section for basic connection settings; those settings can be placed directly in the file or handled by the application layer if needed.
Once the extension line points to the correct absolute path (/usr/lib/php5/20090626+lfs/mongo.so) and the syntax is clean, restart your PHP service (e.g., apache2, php-fpm, or cli). You should verify that the warnings regarding the shared object file disappear.
If you are managing a Laravel project and need to ensure this extension loads consistently across different environments, consider adding the absolute path directly in your .env file if using a framework-specific loader, or ensuring the global php.ini is correctly modified for the CLI environment where RockMongo might be initialized. The key takeaway is that relative paths like mongo.so often fail when the PHP binary expects them in a specific legacy directory structure defined during compilation.
Finally, note that the standard MongoDB extension (mongo) has been deprecated in favor of the official mongodb driver starting with PHP 5.6. If you are running a modern version of PHP (7.x or 8.x), the PECL mongo package will not install at all, and you must use pecl install mongodb. However, given the specific directory names (+lfs) in your logs, you are likely running a legacy system where the older extension is still required for compatibility with RockMongo.