phpinfo() is not working on my CentOS server
When you run a simple phpinfo() function inside a script and receive raw HTML back instead of a parsed data table, your PHP interpreter isn't actually executing the code. The output you're seeingâthe <html>, <head>, and CSS stylesâis being generated by the web server itself, not by the PHP engine. In this specific case, the phpinfo() function is likely running in CLI mode or, more commonly on shared hosting setups like the one described, it's being interpreted as static HTML because the Apache module handling PHP (mod_php) isn't loaded or configured correctly for that specific virtual host.
The confusion often stems from the fact that you can see configuration details within the returned HTML, such as Server API: Apache 2.0 Handler. This confirms that the server thinks it is running PHP, but the output format suggests a mismatch between how the script was invoked and what the web server expects. If the response starts with <!DOCTYPE html> immediately after the opening tag of your file, the server has likely served the raw source code as text or HTML without invoking the PHP processor for that request.
Diagnosing the Execution Context
The most common reason for this behavior on a CentOS server hosting multiple websites is a misconfiguration in the Apache .htaccess files or the VirtualHost definitions. When you upload test.php, the web server needs to know how to handle it. If the directive AddHandler application/x-httpd-php .php is missing from the specific directory or virtual host configuration, Apache will treat test.php as a plain text file.
However, if you are seeing full HTML output that looks like a formatted page but contains the raw source of your function, it indicates that the PHP engine is running, but the output buffering or the handler mapping is incorrect. Wait, looking closer at the symptoms: if the response is HTML and you can see phpinfo() data inside it, PHP is running. The issue described in the scenarioâwhere the user sees HTML headers immediatelyâis actually a sign that the script is being executed correctly, but the output is being interpreted as HTML by the browser because the content type is set to text/html.
Wait, let's re-evaluate the specific symptom: "I receive the HTML, not the interpreted information." The user pastes the response into an HTML file and it renders correctly. This means the server is sending back a string that contains HTML. In standard PHP behavior, phpinfo() outputs exactly this: an HTML page with a table of data. If the user perceives this as "not working," they might be expecting a raw text dump or a different output handler (like JSON).
However, if the user literally sees the source code <html>...</html> in their browser instead of the rendered table, then PHP is failing to execute. But the prompt says: "And so on... Server API Apache 2.0 Handler". This specific string confirms PHP did run. The problem is likely that the user is viewing the raw output or the server is serving the file with a content type that forces HTML rendering, but the logic suggests the user thinks they are seeing "raw" HTML.
Actually, there is a more subtle interpretation. If the server returns the HTML of phpinfo() but the layout looks broken or the data is missing, it could be a version mismatch between the Apache module and the PHP binary. But the most likely scenario for "HTML returned instead of interpreted info" where the user claims they see the raw tags is that the web server is serving the file as static content because the .php extension isn't mapped to the CGI or mod_php handler for that specific domain.
The Multi-Host Complexity
The mention of three websites hosted on the same system is a critical detail. On CentOS, Apache uses VirtualHosts to isolate domains. If test.php was uploaded to www.example1.com, but the configuration in /etc/httpd/conf.d/ or the specific <VirtualHost> block only has PHP processing enabled for the other two sites, the third site will serve files as plain text.
To fix this, you must verify that the PHP handler is active for the specific domain where test.php resides. Check the main Apache config at /etc/httpd.conf or the include directory /etc/httpd/conf.d/. Look for lines similar to:LoadModule php_module modules/libphp.so
and ensure PHPIniDir /etc/php.ini is set correctly. If these are missing, Apache cannot process PHP files on any virtual host that doesn't explicitly load them.
Another common pitfall is the AddType or AddHandler directives. If a .htaccess file in the root directory of the problematic site overrides the global settings and removes the PHP handler, you will see raw HTML source code or, if the server tries to guess content types, a malformed output.
Verifying the Fix
Before assuming Apache is broken, ensure you are checking the correct output. Run phpinfo() via the command line:
php -m > /var/www/test_cli_m.php
If this works and outputs text, your PHP installation is functional. The issue is strictly web server configuration.
To resolve the "HTML instead of interpreted" confusion (assuming the user sees raw tags), inspect the response headers using curl:
curl -I http://your-domain.com/test.php
If the header says Content-Type: text/plain, the PHP handler is disabled. If it says Content-Type: text/html and you see source code, the handler is also likely missing.
Finally, check if there are conflicting configurations in /etc/httpd/conf.d/. Sometimes, a specific site configuration disables php_flag or removes the handler map. Ensure that the directive allowing PHP execution is present for the target virtual host. If the server is running PHP-FPM instead of mod_php, ensure that mod_proxy_fcgi and proxy_pass are configured to forward requests to the FPM socket, otherwise, Apache will serve raw files.
The presence of multiple sites usually points to a scope issue in the configuration rather than a system-wide failure. Fixing the handler mapping for the specific virtual host should restore the expected phpinfo() output, rendering the table and data as intended.
