What is difference between PHP cli and PHP cgi?

What is difference between PHP cli and PHP cgi?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What is difference between PHP cli and PHP cgi?

When you explore the bin folder of a local server stack like WAMP, you will often encounter two distinct executable files: php.exe and php-cgi.exe. While they both execute PHP scripts, they serve fundamentally different roles in the request lifecycle. Understanding which one to use is critical for development workflows, debugging, and deploying applications.

The primary distinction lies in their execution models and intended environments. The file named php.exe (often symlinked or simply referred to as the CLI interpreter) is designed specifically for the Command Line Interface. It expects input directly from standard input (stdin), processes commands line by line, and outputs results to standard output (stdout). When you run a command like php -r "echo 'Hello';" in your terminal, the CLI process spawns independently, executes the logic, and exits immediately after writing to the console. This makes it ideal for running scripts that do not require a web server, such as cron jobs, data migrations, or generating reports via php artisan commands in Laravel projects.

Conversely, php-cgi.exe stands for Common Gateway Interface. It is designed to listen on a socket or port and accept HTTP requests from a web server software like Apache or Nginx. Unlike the CLI version, which treats input as a stream of text, the CGI/FastCGI process parses an HTTP request, identifies the script being executed (often via the SCRIPT_FILENAME environment variable), and returns an HTTP response with headers to the client browser. The web server itself does not execute PHP logic directly; it hands off the request to the PHP-CGI binary, which acts as a middleman between the web server and your application code.

The naming convention can be confusing because many distributions ship php binaries that are actually CGI-enabled by default in certain contexts, or vice versa. However, on Windows systems with WAMP, the separation is explicit. If you attempt to run a PHP script using php.exe via a web request without proper configuration, it will likely fail or output raw code because the CLI interpreter does not generate HTTP headers automatically. Similarly, trying to use php-cgi.exe directly from the command line without piping specific CGI input will result in errors or empty outputs.

Practical implementation requires knowing how to invoke the correct binary. For local testing of a web application before deploying to a server, developers often configure their environment to use the CLI version for tasks like clearing caches (php artisan cache:clear) or seeding databases. This ensures that scripts run in the same PHP interpreter version as your web app without needing to spin up the full web server stack just to run a console command.

In production environments, the php-cgi process is almost always wrapped by a Process Manager like Phusion Passenger (Modulus), OpenLiteSpeed, or FastCGI wrappers provided by Nginx. These managers handle the spawning and killing of PHP processes based on request load, preventing resource exhaustion that would occur if a single long-running CGI script kept a process open indefinitely.

If you are integrating with external services or need to run headless scripts, always ensure you are calling the CLI binary (php.exe on Windows) rather than the CGI binary. Mixing these up is a common source of "500 Internal Server Error" responses during development that resolve once the correct binary path is configured in the web server's configuration files.

Our network