PHP Module is Already Loaded: Root Cause and Remediation
When you run php -v on the command line, seeing a flood of Module 'PDO' already loaded warnings can be distracting. While these messages don't stop your CLI tool from executing or break web requests handled by Apache/Nginx, they indicate a misconfiguration in your environment that warrants investigation. The core issue is not that PHP doesn't know how to use the extension; it's that the extension is being loaded twice during the startup sequence of the CLI interpreter.
In standard PHP installations, extensions are typically managed through .ini files located in /etc/php/{version}/cli/conf.d/. When you encounter these warnings, it usually means a specific directiveâoften extension=pdo.so or similarâis present in an INI file that is being sourced by the CLI binary. However, PHP also automatically loads certain core extensions at runtime if they are not explicitly disabled or if they are compiled as part of the SAPI (Server Application Interface).
The confusion often stems from how the PHP process initializes. When you invoke php directly from the shell, it starts a fresh process that reads configuration files listed in php.ini. If your system has multiple configuration stanzas defined for CLI or if a global php.ini includes an extension=... directive that conflicts with what the SAPI provides automatically, you get this duplicate load warning. The fact that the module is marked "already loaded" confirms that the underlying code is present and functional; the warning is purely cosmetic noise generated by the loader detecting a redundant initialization attempt.
Is Removing the INI Line a Permanent Fix?
Removing the extension={extname}.so line from /etc/php/{version}/cli/conf.d/{extname}.ini is technically a workaround to silence the warnings, but it is often the correct administrative action depending on your deployment strategy.
The real question is whether that extension is actually necessary for your CLI tools. For most core extensions like PDO, calendar, or sysvsem, these are built-in components of the PHP runtime in many distributions (such as Debian/Ubuntu). They do not require an external .so file to be explicitly loaded via INI if they are compiled into the binary or provided by the SAPI. If you manually add a directive to load them, or if your environment's default php.ini includes them alongside a conflicting loader script, you create this redundancy.
If you remove the line from the INI file, PHP will simply use the version of the extension that is already available in memory (if any) or rely on the SAPI-provided implementation. There are generally no negative side effects to removing an instruction to load a core module that is already active; the functionality remains intact because the code is not being stripped from the build, it's just being prevented from being re-initialized unnecessarily.
However, you must ensure that the extension isn't actually missing before assuming the INI line is redundant. You can verify this by checking if the extension file exists in your extension_dir (often found via phpinfo() or get_extension_dir()). If the .so file exists but the SAPI doesn't automatically load it, and you remove the manual load instruction, that specific functionality could break.
The Socket Exception: A Different Beast
One specific warning in your output stands out from the rest:PHP Startup: Unable to load dynamic library '/usr/lib/php/20151012/sockets.so' - /usr/lib/php/20151012/sockets.so: undefined symbol: php_network_gethostbyname
This is not a "already loaded" warning; this is a fatal loading failure. While the other messages are harmless noise, this one indicates that PHP attempted to load sockets.so but failed because of an undefined symbol (php_network_gethostbyname). This usually happens when mixing incompatible PHP versions or using extensions compiled against a different Zend Engine version than the one running in CLI.
In your output, you see PHP 7.0.3. If that sockets.so file was intended for PHP 5.x (suggested by the path structure or age) or a different build variant, it is incompatible. Leaving this warning active can sometimes lead to silent failures if the socket extension is critical for your CLI scripts. Unlike the "already loaded" warnings, you should investigate the source of sockets.so. It might need to be removed from the load list entirely if PHP 7 does not require it or if a compatible version isn't available in your package repositories.
Best Practice for Production Environments
The most robust solution is to align your CLI configuration with your web server configuration. If you are using packages like php7.0-fpm and php7.0-cli, they often share the same pool of extensions but might have different default configurations. The goal is to ensure that every extension needed for your application logic is loaded exactly once per process.
- Audit the Load Order: Use
php -i | grep enabledorphpinfo()to see which modules are active. - Clean Up Conflicts: If an extension is listed as "already loaded," remove the manual
extension=directive from the CLI-specific INI files unless you are overriding a default behavior that the SAPI doesn't provide by default. - Fix Broken Loads: For the
sockets.soerror, locate the file and either replace it with a version compatible with PHP 7.0 or remove theextension=sockets.soline from your INI files if the sockets functionality is already handled by the core SAPI.
By cleaning up these configuration files, you eliminate the warnings and ensure that your CLI environment reflects a clean, intentional state rather than a collection of conflicting loader attempts. This practice reduces confusion during debugging and ensures consistent behavior across different execution contexts.