AkiRoss AkiRoss - 1 year ago 696
Linux Question

Apache CGI in user directory "End of script output before headers"

I know there are some questions about this topic, but none seems to solve my issue. See this or this or this.

I'm on Linux, Fedora21, and I'm trying to enable per user directory CGI script. I followed these instructions, but without success.

I get the error:

[cgi:error] End of script output before headers: test.cgi

is an executable sh file, containing a very simple script:


echo "Content-type: text/plain"
echo ""
echo "Hello"

which has executable flag and runs without problems from shell.
I also tried with Python: same result.

I also disabled selinux for good measure.

I also tried setting the
level to Apache's ErrorLog, but all I get is only "granted" permissions before the error above.

I also configured the
file with

<Directory "/home/*/public_html">
AllowOverride All
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require all granted

<Directory /home/*/public_html/cgi-bin/>
Options ExecCGI FollowSymLinks
SetHandler cgi-script
AddHandler cgi-script .cgi .exe .pl .py .vbs
Require all granted
AllowOverride All

and restarted the server. No success. Everything looks fine to me, I can't understand... What's wrong??


I forgot to add that the issue is just for per-user directory: if I move the same script to
directory, it works as expected.


The shell does exist:

$ ls /usr/bin/sh

Answer Source

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_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.