Script 1 below is bash, and is at https://example.com/cgi-bin/test
. It produces the output 'Under construction' when fetched. It echos Status
and Content-type
headers, and some HTML. If I instead try to echo an entire HTML doc Apache just complains about an invalid header.
Script 2 below is php, and is at https://example.com/cgi-bin/test2.php
. Unlike the bash script, this one returns an HTML document.
How is it that script 2 can send an entire HTML doc, but script 1 can't?
Script 1
#!/bin/bash
cat <<'EOF'
Status: 200 OK
Content-type: text/html
<p>Under construction.</p>
EOF
Script 2
<?php
print <<<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
...etc
</head>
<body>
...etc
</body>
</html>
EOF;
?>
EDIT
php
comes in 2 flavours: the CLI and CGI versions. If you just run Script 2 from the command line as php test2.php
then the only output produced by php
is exactly what you see: the HTML doc. php-cgi
is the CGI version (install on Ubuntu/Deb as apt install php-cgi
). Apache (effectively) runs the CGI version (in real life, it does this slightly differently, but with the same results):
$ php-cgi test2.php
Content-type: text/html; charset=UTF-8
<!DOCTYPE html>
...rest of doc
CGI scripts have to return at least Content-type
to Apache (but can return more headers, including Status
). So the answer is that both scripts work because Script 1 explicitly returns Content-type
, while the under-the-hood CGI version of php
does the same.
The bash script can return the entire HTML document, as long as it also returns the Content-type
.