PHP Startup: Unable to load dynamic library 'openssl' in Ubuntu
When a PHP application intermittently fails with a PHP Startup: Unable to load dynamic library 'openssl' error, it often feels like a race condition that defies logic. The issue is not usually that OpenSSL isn't installed or that the library file is missing from the filesystem; rather, it indicates a conflict in how the Apache server initializes PHP modules relative to its own configuration state.
The specific error message reveals the root cause immediately:
PHP Startup: Unable to load dynamic library 'openssl'
(tried: /usr/lib/php/20170718/openssl (/usr/lib/php/20170718/openssl:
cannot open shared object file: No such file or directory),
/usr/lib/php/20170718/openssl.so (/usr/lib/php/20170718/openssl.so: cannot
open shared object file: No such file or directory)) in Unknown
Although phpinfo() reports that OpenSSL support is enabled and the library version matches the system installation, the dynamic loader cannot find the .so file at the expected path during the startup phase. This discrepancy between phpinfo() output (which reflects a loaded state) and the fatal error suggests a transient configuration issue or a stale Apache process holding onto an outdated module map.
This instability frequently arises after enabling new Apache modules, such as proxy and proxy_wstunnel, or following a system update via apt-get. When these modules are added, they often trigger a restart of the Apache service. If the php7-common package is also updated during this process, it may replace the OpenSSL shared object at /usr/lib/php/20170718/openssl.so without immediately invalidating the compiled-in configuration directives that point to the old path or expecting the new layout.
The intermittent nature of the failureâwhere one request works and the next results in a 500 Internal Server Errorâis characteristic of Apache's Multi-Processing Module (MPM) behavior, particularly with mpm_prefork. Apache forks processes for each request. If an update occurred while Apache was running, some child processes might have loaded the configuration before the file system change took effect, while others pick up the new state. When a process attempts to load the OpenSSL module and fails because the file was moved or renamed by a dependency update, it triggers this fatal error before any headers are sent, leading to the HeadersAlreadySentException observed in the stack trace when the application tries to render an error page.
To resolve this deterministically, you must ensure that Apache does not attempt to load the OpenSSL module dynamically via the php.ini file if it is intended to be loaded at startup as part of the PHP binary itself, or conversely, ensure the file exists where Apache expects it. However, a more robust approach on Ubuntu systems involving Swoole or specific package managers (like the sury.org repository indicated in your version string) is to disable the explicit loading of openssl from the php.ini configuration file if the extension is compiled directly into the PHP binary or handled differently by the system.
Check your php.ini for lines resembling:
;extension=openssl
If this line exists, comment it out (;extension=openssl) and restart Apache. This prevents PHP from attempting to load a dynamic library that might be managed differently by the system's package manager or already integrated into the core. Alternatively, if you must keep it loaded dynamically, verify that /usr/lib/php/20170718/openssl.so actually exists. If an update removed it because OpenSSL is now statically linked or handled via a different mechanism (common in newer PHP versions), the configuration directive becomes obsolete.
In environments using Laravel or similar frameworks where SSL verification is critical, ensuring that php.ini settings align with the actual state of the shared object cache is vital. A mismatch here often causes the application to crash mid-request if it attempts to initialize cryptographic functions before the library is resolved. Always restart Apache after modifying module configurations or updating system libraries to clear any cached process states.
If the issue persists despite disabling the explicit extension load, investigate whether php7-common needs a specific configuration reload. In some cases, running apachectl -M and then restarting the service ensures that all loaded modules are re-evaluated against the current filesystem state.