Check if PHP is installed on Apache or IIS Server?

Check if PHP is installed on Apache or IIS Server?

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

Stefan Bogdanescu

Founder & Senior Architect

Detecting PHP Runtime and Server Environment

Determining whether PHP is installed on a web server like Apache or IIS from within the PHP environment itself is less about checking for the presence of a specific file and more about verifying the active runtime context. When you run a PHP script, the language provides built-in mechanisms to inspect the execution environment. If PHP is not installed at all, you simply cannot execute the code that attempts to check it; the request will fail at the server level before reaching your application logic. However, once the script is running, distinguishing between different server configurations becomes a standard diagnostic task.

The most direct method to confirm the current runtime environment is using the phpinfo() function or inspecting predefined constants. Calling phpinfo() generates a comprehensive report that explicitly states whether Apache or another web server (like IIS via FastCGI) is handling the request. This function is often invoked by appending ?php or accessing a specific URL, but programmatically, you can access the underlying data without generating a full HTML page.

To detect the specific web server handling the request, inspecting the $_SERVER superglobal array is essential. The $_SERVER['SERVER_SOFTWARE'] variable contains the name and version of the software that handles the HTTP request. If this value starts with "Apache", you are running on an Apache server. Similarly, if it indicates "Microsoft-IIS" or contains "IIS", you are dealing with Internet Information Services. This check works regardless of whether PHP is embedded directly into Apache via mod_php or running as a separate FastCGI process (like FPM) behind a reverse proxy.

For IIS environments, the detection logic often requires checking for additional markers because IIS sometimes masks the actual server software name depending on the module configuration. A robust check involves looking at $_SERVER['REDIRECT_STATUS'] or examining specific headers if available. However, the most reliable indicator within PHP itself remains the value of $_SERVER['DOCUMENT_ROOT']. In Apache setups with mod_php, this path usually points directly to the web root configured in httpd.conf. In IIS setups running via CGI/FastCGI, the path might reflect the application pool settings or a different virtual directory structure.

Another critical distinction lies in how the PHP process is invoked. On Apache, you might encounter scenarios where mod_php loads the engine directly, whereas on IIS, FastCGI is frequently used to communicate with an external PHP-FPM instance. You can verify this by checking if specific Apache modules are loaded or if the environment variable APACHE_RUN_USER exists. If that variable is present, you are almost certainly on an Apache server. Conversely, the absence of Apache-specific variables combined with the presence of IIS-specific headers like HTTP_X_WWW_AUTHORIZATION (often modified by IIS) can hint at the underlying infrastructure.

When building scripts that need to behave differently based on the server, avoid relying solely on string matching against SERVER_SOFTWARE. This value can be spoofed or normalized by load balancers and reverse proxies like Nginx sitting in front of Apache or IIS. Instead, check for specific process environment variables that are less likely to be altered. For instance, checking for IIS_WAS or specific Windows environment variables can provide a definitive answer on IIS servers without needing deep knowledge of the web server's internal configuration files.

If you need to implement this logic in a framework like Laravel, you might access these details through the server detection utilities provided by the underlying runtime. While Laravel abstracts much of the server interaction, accessing phpinfo() data or parsing $_SERVER remains necessary for low-level environment checks. Remember that these checks are strictly about the PHP runtime's perception of its host; they do not guarantee the security or stability of the server itself. Always validate these assumptions during development before deploying to production, as misidentifying the server can lead to incorrect path resolution or failed module loading attempts.

Our network