Apache CGI in user directory "End of script output before headers"
Finally I solved that. Thanks to @JimB, because in his comment he pointed out SUEXEC, which I didn't know about (or simply ignored till now).
After reading a bit the suEXEC documentation, I understood the the problem had to be there. So, I took a look at the configuration:
# suexec -V
-D AP_DOC_ROOT="/var/www"
-D AP_GID_MIN=1000
-D AP_HTTPD_USER="apache"
-D AP_LOG_SYSLOG
-D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
-D AP_UID_MIN=1000
-D AP_USERDIR_SUFFIX="public_html"
and everything looked Ok (good uid/gid for my user, userdir_suffix is fine, etc). So I took a look at the system logs:
# journalctl -b | grep "suexec"
May 22 11:43:12 caladan suexec[5397]: uid: (1000/user) gid: (1000/user) cmd: test.cgi
May 22 11:43:12 caladan suexec[5397]: directory is writable by others: (/home/user/public_html/cgi-bin)
and that's the problem: my cgi-bin
directory was writable by others.
I fixed by simply changing the permissions to 755
.
This sometimes comes up when you try to call other Python module methods from your cgi where you might have left some 'print' statements (perhaps for debugging). So scan your code for any 'print' statement, sometimes this fixes the problem easily.
For me, it worked when I changed the shebang line (#!/usr/bin/sh
) to #!/usr/bin/env sh
. I found that any shebang lines from What is the preferred Bash shebang? seemed to work (however note that sh
is different from bash
so if you want to use sh
stick with it).
So this code worked for me:
#!/usr/bin/env sh
echo "Content-type: text/plain"
echo ""
echo "Hello"
Also, according to the post mentioned above, it seems /usr/bin/env sh
seems preferred over /bin/sh
. I have no idea about the per directory stuff.