Forum

problem: 500 - Internal Server Error on cgi

simon
6 March 2012, 15:54
I am using Hiawatha version: 7.5

My hiawatha.conf is:
ServerId = www-data
ConnectionsTotal = 150
ConnectionsPerIP = 10
SystemLogfile = /var/log/hiawatha/system.log
GarbageLogfile = /var/log/hiawatha/garbage.log

Binding {
Port = 80
}

CGIhandler = /usr/local/bin/python:py
CGIextension = py

Hostname = 127.0.0.1
WebsiteRoot = /var/www/root
StartFile = index.html
AccessLogfile = /var/log/hiawatha/root_access.log
ErrorLogfile = /var/log/hiawatha/root_error.log

VirtualHost {
Hostname = my.website
WebsiteRoot = /var/www/my_website
StartFile = index.py
AccessLogfile = /var/log/hiawatha/my_website_access.log
ErrorLogfile = /var/log/hiawatha/my_website_error.log
ExecuteCGI = yes
}


My index.py is:
#!/usr/local/bin/python
print "Content-Type: text/html"
print """\
<html>
<head>
<title>My CGI Webpage</title>
</head>
<body>
<h1>Welcome to my CGI webpage.</h1>
</body>
</html>"""

ls -l /var/www/my_website/index.py
-rwxr-xr-x www-data www-data index.py

I've got in the browser "500 - Internal Server Error"
I've got in the my_website_error.log "index.py|no output"

What is going wrong here?

Hugo, can you help?
Hugo Leisink
7 March 2012, 00:50
First, don't use the same file extension for both CGIextension and CGIhandler. Read the CGI/FastCGI HOWTO for this.

Second, make sure that after the Content-Type line an empty line is written. This is to 'close' the CGI header. Preferably, the EOL in the CGI header is \r\n, not just \n.
simon
12 March 2012, 15:09
Hi hugo,

Thanks for replying, i did what you suggested,
1. I changed CGIextension = py to CGIextension = cgi
2. I add \r\n (print "Content-Type: text/html\r\n") to my index.py and index.cgi
3. Restarted hiawatha

But still get the above message in browser and error.log when try to run index.py or index.cgi on my virtual host.
Hugo Leisink
12 March 2012, 15:38
Not only each CGI header must end with \r\n, also the empty line between the header and the body must end with \r\n. You can use only \n (because Apache accepts this buggy CGI behaviour, many applications were written like this, so I was forced to support it as well) but you cannot mix them. If you use \r\n to end the header lines, also use \r\n for the empty line. If you use only \n for the header lines, also use only \n for the empty line.

Your Content-Type line ends with \r\n, your empty line uses only \n. Add another \r\n after the Content-Type line and it should work. The result HTML then starts with an empty line, which you probably want to remove. But I'm not familiar with Python and I have no idea what the tripple-quote does. But I'm sure you do
simon
13 March 2012, 10:08
Hi hugo,

I tried this, but without success. The same error messages.
#!/usr/bin/python

print "Content-type:text/html\r\n"
print "<html>\n"
print "<head>\n"
print "<title>Hello Word - First CGI Program</title>\n"
print "</head>\n"
print "<body>\n"
print "<h2>Hello Word! This is my first CGI program</h2>\n"
print "</body>\n"
print "</html>\n"

Damnn, Hiawatha doesn't except anything i tried! I think there is something wrong with cgi/configuration or maybe a bug/code design/implementation.
On the otherhand i tried all the code in my Lighttpd webserver and it work seamlessly even without
newline and carridge return characters and execute rights (in this case Lighttpd is very flexible).
Hugo Leisink
13 March 2012, 10:17
It looks like the print statement in Python adds a \n itself. And again, you forgot the empty line between header and body. Therefor, the following should work:
print "Content-type: text/html\r"
print "\r"
print "<html>"
print "<head>"
print "<title>Hello Word - First CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello Word! This is my first CGI program</h2>"
print "</body>"
print "</html>"
simon
13 March 2012, 11:09
Hi hugo,

I tried the above code you suggested but still get the same errors.

For the future releases of Hiawathe is it possible to be more specific with the error messages because
"no output" for me is very short.
Hugo Leisink
13 March 2012, 11:38
I have you idea what you are doing, but you're doing something wrong. The proof that the code above works can be found here [www.leisink.org].

"no output" means that the script generates no output, only a CGI header. What goes wrong in your script is that there is no correct empty line between the CGI header and the CGI body to mark the end of the header.
simon
13 March 2012, 13:22
Hi hugo,

I have good news, my cgi scripts are working now! only if Hiawatha is running by the init process.
I had Hiawatha running on svscan (Daemontools) with the following command:

exec envdir ./env softlimit -d300000 /usr/local/sbin/hiawatha -c /usr/local/etc/hiawatha

But found out that the cgi part isn't working only static html files.
I am wondering why this is the case.
simon
13 March 2012, 15:38
Hi hugo,



Thanks for your help. Problem solved.



Close topic.
This topic has been closed.