Why CLI and Web PHP Behave Differently
You are likely encountering a classic configuration mismatch where your web server runs PHP with one set of extensions, while the command-line interpreter uses another. In your specific scenario, phpinfo.php served via Apache or Nginx reveals that the GD extension is installed, yet running php phpinfo.php from the terminal yields a negative result when grepping for it. This discrepancy almost always points to two separate PHP installations rather than a single misconfigured instance.
Operating systems frequently install multiple versions of PHP to satisfy different requirements. The web server typically loads an older version (e.g., PHP 7.4 or 8.0) that is stable and secure, while the system package manager might provide a newer version (e.g., PHP 8.1 or 8.2) for command-line tasks. If you installed GD via the system package manager on Debian or Ubuntu, it often registers only with the CLI binary, not the web server's FPM pool or CGI setup.
To confirm which interpreter is actually handling your request, inspect the output of the php command immediately after running it. Look for the "Loaded PHP Version" and "Build Date". Compare this string against the version displayed in your web-accessible phpinfo.php. If the versions differ, you have identified the root cause: the CLI tool is invoking a different binary than the web server.
Aligning Your Environment
The goal is to ensure that the CLI interpreter matches the one serving your web application. There are three primary approaches to resolving this, ranging from minimal changes to a full re-architecture of the environment.
1. Use the Same Binary for Both
If you installed PHP via apt or yum, check if there is an alias or symlink confusing your path. However, often the cleanest fix is to ensure the web server configuration explicitly points to the same binary used in the CLI.
For Apache, edit the .htaccess file or the virtual host configuration to set:
AddHandler .php79 application/x-httpd-php-7.4
(Adjust the version suffix to match your actual PHP versions). More commonly, you must configure Apache to use the FastCGI Process Manager (FPM) socket that corresponds to the CLI binary. If you installed a newer version of PHP with GD and want that specific version for the web server, you may need to install the corresponding libapache2-mod-php81 package alongside the CLI tools.
2. Invoke the Correct Binary Directly
If you cannot modify the web server configuration, simply call the correct path in your terminal. If /usr/bin/php is different from /usr/local/bin/php, use the full path:
/usr/local/bin/php phpinfo.php | grep GD
This bypasses the system default and ensures you are testing the exact binary that handles web requests if they point to that location.
3. The Modern Standard: Composer and Global Management
In modern development stacks, relying on system package managers for PHP is increasingly discouraged because it leads to "dependency hell" where CLI and Web versions diverge. The industry best practice is to use a global PHP manager like phpenv or rbenv.
By installing PHP 8.2 (for example) via Homebrew or pecl, and setting the PATH priority, you can ensure that both your shell session and your web server (configured to use fpm with that specific version) utilize the exact same build. This guarantees that extensions like GD, cURL, or Redis installed globally are visible in both contexts without needing complex symlink hacks.
Understanding the Trade-offs
While running a script via php script.php is convenient for cron jobs and migrations, mixing versions introduces significant risk. If you run a migration script built for PHP 8.2 on a web server running PHP 7.4, you will encounter fatal errors regarding syntax or missing classes.
The most robust solution is not to hack the existing installation but to standardize the environment. If you are building a new application, configure your php.ini and FPM pool files to point to a single, dedicated PHP version directory. This ensures that phpinfo.php on the web and the output of php -v in the terminal reflect identical configurations and loaded extensions.
If you are maintaining legacy code where changing the server configuration is impossible, accept that the CLI is for scripting tasks only, while the web server remains isolated. Use environment variables or specific paths to call the correct binary when testing functionality outside the browser.
