Locate the PHP CLI binary on the server.
Please read the Caveats section thoroughly before using this library.
Important
Developing and maintaining world-class software is neither easy nor free. The development of this software is subsidised by sales of our commercial offerings. If you like this software and would like to see it maintained in the future, please consider purchasing a subscription to one of our commercial offerings. Thank you!
This library locates the PHP CLI binary from the web (Apache mod_php, CGI, or FastCGI) execution context.
The environments currently supported are:
- cPanel with EasyPHP
- cPanel on CloudLinux
- Plesk on Linux, and Windows
- XAMPP on Linux, Windows, or macOS
- MAMP on Windows, and macOS
- HomeBrew on macOS, and Linux
- WAMPServer on Windows
- Generic Linux server (works with Debian, Red Hat, Arch Linux, and their derivative distributions)
- Generic BSD server
- Generic Windows server, including PHP installations in C:\PHP
- Generic macOS server
Security note. Unless you disable the validation options, this library executes every candidate binary it finds (with -v) to check its version and that it is the PHP CLI. That includes binaries found in well-known directories which may be writable by other users, e.g. d:\PHP or d:\PHP8.3 on any Windows drive, C:\PHP, or the Program Files directories. On a server where untrusted users can create files in those locations, disable the usePHPCommonWinDirsSearch option (on UNIX-like systems, the useFixedPaths option, which searches /usr/bin, /usr/local/bin and /opt/bin; and keep osSpecific and softwareSpecific off if you do not need them), or make sure only administrators can write there.
Windows. The library runs Windows' own tools (where, wmic, fsutil, PowerShell) by their full paths in the Windows directory (%SystemRoot%), through Windows' own cmd.exe, and each candidate PHP binary directly with proc_open(). Only .exe files which are Windows programs (a PE image: the MZ and PE signatures) are candidates, and nothing else is ever run as a PHP binary, whichever search strategy found it: where php also lists php.bat, php.cmd, php.com and an extensionless php, which are not PHP's CLI binary, and PHP's is_executable() is TRUE for a text file named php.exe. Nothing runs by its bare name, which Windows would look for in the current directory (under IIS, the script's directory) first. Where proc_open() is disabled it falls back to exec(), which runs everything through cmd.exe: then it skips any candidate whose path contains a % or a ! (cmd.exe would expand %NAME%, and !NAME! when its delayed expansion is turned on in the registry, and could run another program), and a cmd.exe in the current directory would run instead of Windows' own, since PHP starts it by its bare name. A 32-bit PHP on 64-bit Windows runs the 32-bit copies of those tools, which Windows puts at the same paths for it (SysWOW64, seen as System32); they work the same (tested on Windows Server 2022).
You can try to locate a specific version of PHP (by major.minor or major.minor.patch version specification), or just any available PHP version.
When looking for a specific version, the version reported by each PHP binary must match the one you asked for on whole version components, from the start: asking for 8.3 accepts PHP 8.3.0 or 8.3.12, but not PHP 8.30.0 or 7.8.3; asking for 8.3.1 accepts PHP 8.3.1 (or 8.3.1-dev), but not 8.3.10.
We have mass-distributed web applications which are meant to be installed and operated by non-technical end users.
Oftentimes we provide CLI scripts or integrations with such (e.g. integration with Joomla! CLI, or WP-CLI). The user typically needs very precise instructions to execute these CLI scripts, either directly on a terminal or inside a CRON job.
This requires two things: the path to the PHP CLI executable, and the path to the script they need to execute. The latter is straightforward to determine from within the web application. The former is not. This is what this library does. It tries to locate the PHP CLI binary on the server.
At a minimum you can do:
$cliPath = \Akeeba\PHPFinder\PHPFinder::make()->getBestPath(PHP_VERSION);This will try to locate the PHP CLI path for the current PHP version. If it fails, it will return NULL.
Create and set up a Configuration object:
// Check the source comments of the Configuration class to understand what each setting does.
$configuration = \Akeeba\PHPFinder\Configuration::make()
->setExtendedBinaryNameSearch(false)
->setSoftwareSpecific(false);Create a PHPFinder object using the configuration object:
$finder = \Akeeba\PHPFinder\PHPFinder::make($configuration);Search for any available PHP version, or a specific one:
$anyPHP = $finder->getBestPath();
$php83 = $finder->getBestPath('8.3');You can also get all auto-detected PHP folders for a given PHP version:
$anyPHPArray = $finder->getPossiblePaths();
$php83Array = $finder->getPossiblePaths('8.3');Server hangs. The default behaviour of this library is to run the executables discovered (with exec(), or proc_open() on Windows) to validate that the version number returned by the executable is the expected, as well as validate that the binary is the PHP CLI (and not PHP CGI/FastCGI). This may cause some servers to abort execution, resulting in an HTTP 500 Internal Server Error status code or, worse, a blank page with an HTTP 200 OK status. You can prevent that by disabling these features in the configuration object:
// Check the source comments of the Configuration class to understand what each setting does.
$finder = \Akeeba\PHPFinder\PHPFinder::make(
\Akeeba\PHPFinder\Configuration::make()
->setValidateVersion(false)
->setValidateCli(false)
);Another approach is to have your software store a persistent flag before and after using this library so that you can set the correct configuration options. For example:
$validationFlag = $myAppStorage->get('phpfinder_validate') ?? true;
$myAppStorage->set('phpfinder_validate', false);
$finder = \Akeeba\PHPFinder\PHPFinder::make(
\Akeeba\PHPFinder\Configuration::make()
->setValidateVersion($validationFlag)
->setValidateCli($validationFlag)
);
$phpPath = $finder->getBestPath(PHP_VERSION);
$myAppStorage->set('phpfinder_validate', true);If the attempt to use this library fails the flag is set to false since the server kills the script before we reach the final statement which resets the flag to true. Therefore, when the user reloads the page the flag will be false, and the library won't fail to execute in this or any subsequent execution. The downside of this approach is that a stark minority of users will experience an error the first time you try to determine the PHP CLI execution path with this library.
Performance. Searching for a specific PHP CLI can be slow in some use cases. We strongly recommend caching the results after the first run.
Accuracy. This library is trying to strike a balance between speed and accuracy. This may result in a perceived "wrong" PHP path to be returned. We know of the following use cases where this might happen:
- Searching for a PHP version by major and minor (but no patch) version MAY return an older patch version of PHP. This could happen if multiple PHP versions with the same minor version but different full version (including patch), are installed at the same time. For example, if you have PHP 8.3.1 and PHP 8.3.4 installed at the same time, and you search for PHP 8.3, there is no guarantee that PHP 8.3.4 will be returned.
- If you have multiple installations of the same PHP version across the system, there is no guarantee that the PHP binary you will be returned will be the one belonging in the same installation. For example, you may have a Linux server with PHP installed globally, but also as part of XAMPP. If you run a web script under XAMPP and use this library, you MAY be returned the globally installed PHP CLI version of the same version as your XAMPP PHP version.
- Likewise, if you have multiple PHP installations, and you don't specify a PHP version, there is no guarantee that the path returned will be the latest PHP version, or any particular PHP version. It will be a PHP version found on the server.
Search by major version. Searching for a PHP version just by its major version is NOT supported for practical reasons and because minor PHP versions introduce backwards incompatible changes. Searching for PHP 8 could for example return 8.4, or 8.0. You wouldn't know which one it would return in advance, and it might be a version that's incompatible with your software! If you want to simulate this very inefficient and inaccurate search, you can look for PHP x.6, x.5, x.4, x.3, x.2, x.1, and x.0 in this order, returning whichever is found first. Do keep in mind that just because you can doesn't mean you should.
PHP configuration. Most servers have a separate configuration for PHP CLI than the one used for the web SAPI (mod_php, CGI, or FastCGI). This is a non-obvious caveat which needs to be communicated to your users. There is no good, fool-proof way to automate using the same configuration across PHP and the web in those environments.
PHPFinder is free and open-source software released under the GPLv3 license. It is developed and distributed on a purely non-commercial basis: there is no charge for the software or any version of it, no paid tier or edition, no bundled or gated services, and no plan to monetize it in the future. It is not tied to, bundled with, or a dependency of any commercial product or service offered by Akeeba Ltd or any other party. On this basis, it falls outside the scope of Regulation (EU) 2024/2847 (the Cyber Resilience Act), which exempts free and open-source software supplied outside the course of a commercial activity. This statement reflects our assessment as of 27 August 2026 and will be revisited if the project's distribution model changes.