I got a LEMP stack running, but do not understand why a html page is displayed. I got the following in info.php:
<?php
phpinfo();
?>
I get the following when executing it directly:
php info.php | head -n 2
phpinfo()
PHP Version => 7.0.18-0ubuntu0.16.04.1
But when I access info.php through nginx with fastcgi accessing php, a full blown html page is returned, incuding links and a php logo. The start for example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #fff; color: #222; font-family: sans-serif;}
Whenever I use other php scripts that echo some text, this does not happen and only text is returned.
Can anyone explain why this happens?
That's the way PHP was built. From the documentation:
Note: phpinfo() outputs plain text instead of HTML when using the CLI mode.
PHP detects what context it's running in, and acts appropriately. Nothing more to it.