SSL routines:tls_process_server_certificate:certificate verify failed
When file_get_contents or curl suddenly stops working over HTTPS, the immediate instinct is often to disable certificate verification. This approach silences the error but leaves your application vulnerable to man-in-the-middle attacks. If you are seeing the OpenSSL error SSL routines:tls_process_server_certificate:certificate verify failed, it usually indicates a mismatch between the Certificate Authority (CA) trust store on your server and the certificate presented by the target host.
In the scenario described, a renewal of a Let's Encrypt certificate for domain.tld coincided with scripts failing to connect. The critical detail is that browsers like Firefox and Chrome successfully load the site without warnings. This confirms the certificate chain is valid in the browser environment. Since you are on CentOS 7 or Ubuntu 18 using Virtualmin, the root cause is almost certainly a version mismatch in the system's CA bundle.
The Root Cause: Trust Store Divergence
Modern PHP installations typically rely on openssl for SSL connections. By default, PHP loads the CA certificates from /etc/pki/tls/certs/ca-bundle.crt (on CentOS) or /etc/ssl/certs/ca-certificates.crt (on Ubuntu).
Let's Encrypt issues certificates that are signed by their root CAs. Historically, Let's Encrypt used ISRG Root X1. Around late 2020, they updated their intermediate and root chain structure. If your server was installed or the CA bundle was last updated before this transition, your system might still be looking for an older intermediate certificate (like DST Root CA X3 or specific Let's Encrypt intermediates from earlier in the lifecycle) that no longer exists in the chain provided by the server.
Even if you ran yum install ca-certificates.noarch and it reported the latest version, the underlying OpenSSL library or the bundled CA file might not have been refreshed to include the new root anchors required for the latest Let's Encrypt certificates. The error message explicitly states that verification failed, meaning PHP tried to build a chain of trust but could not find a trusted root anchor in its local bundle that matches the leaf certificate presented by the target domain.
Why Browsers Work But PHP Fails
Browsers handle this differently. They ship with their own bundled CA lists (Mozilla's NSS database) which are updated via OS updates and often include cross-signed roots or alternative trust paths that your minimal Linux distribution might lack. When you open https://domain.tld/script.php in Chrome, it uses its own internal trust store, bypassing the system's /etc/ssl/certs entirely. This is why the site loads fine visually while your PHP script crashes with an SSL error.
The Real Fix: Syncing the CA Bundle
Disabling verification (verify_peer => false) is a temporary band-aid that should be avoided in production unless you have a specific reason to trust the network implicitly (which is rare). To fix this permanently, you need to ensure your server's CA bundle contains the latest roots.
On CentOS 7 and RHEL-based systems, the package ca-certificates is often managed by a system tool that can automatically refresh the bundle upon installation. However, sometimes the command updates the individual certificate files in /etc/pki/tls/certs/ but does not regenerate the main ca-bundle.crt.
Run the following command to ensure the bundle is rebuilt with the latest certificates:
sudo update-ca-trust extract
# OR for older CentOS versions where update-ca-trust might not exist:
sudo ca-certificates --bundle-file=/etc/pki/tls/certs/ca-bundle.crt
On Ubuntu or Debian systems, run:
sudo update-ca-certificates
After running the appropriate command for your OS, restart your web server or PHP-FPM service. Once the bundle is updated, your PHP scripts should be able to verify the new Let's Encrypt certificate correctly without needing to disable peer verification.
Preventing Future Occurrences
To prevent this from happening again after future CA updates:
- Automate Bundle Updates: Ensure your server management script or cron job runs
update-ca-certificates(or the equivalent) periodically, perhaps monthly, or immediately upon any security update involving OpenSSL or CA bundles. - Pin to Specific CAs (Advanced): If you are connecting to a specific internal service that uses a self-signed cert, you can add that specific certificate to your bundle manually so it doesn't rely on external trust chains. However, for public domains like Let's Encrypt, relying on the system CA bundle is best practice.
- Monitor OpenSSL Versions: Occasionally, an upgrade to the OpenSSL library itself (e.g., moving from a legacy version to 1.1.1 or 1.2.x) can change how certificates are parsed. Keep your
opensslpackage updated alongside your CA bundles.
By synchronizing your system's trust store with the latest standards set by Certificate Authorities, you eliminate the need to disable security checks in your PHP configuration.
