Project

General

Profile

[Solved] Cant get a simple cgi-bin script to run

Added by draenog over 5 years ago

Looking for some help here getting CGI working on lighttpd

I am running running lighttpd on a Raspberry Pi (Raspian Stretch). As a basic http server, it works perfectly serving up html files.

However, when I try to run some basic hello-world python script, I get the 404 Not-Found error.

My script (in var/www/cgi-bin directory) called "hello.py" is:

#! /usr/bin/python

print "Content-Type: text/html\n\n" 
print '<html><head><meta content="text/html; charset=UTF-8" />'
print '<title>Rapsberry Pi</title><p>'
print 'Hello&nbsp;World...'
print "</p></body></html>" 

Which is executable by user www-data:

peter@jumpbox:/var/www/cgi-bin $ ls -lsa
total 12
4 drwxr-xr-x 2 www-data www-data 4096 Sep 29 18:13 .
4 drwxr-xr-x 4 www-data www-data 4096 Aug 23 20:37 ..
4 -rwxr-xr-x 1 www-data www-data  214 Sep 29 18:13 hello.py

It runs OK from the commend line (proving python script works), but when I run from a web browser with the address: "http://jumpbox/cgi-bin/hello.py", I get the 404 error.
(jumpbox is the local name of the machine)

My lighttpd.conf file (in /etc/lighttpd) is:

server.modules = (
    "mod_access",
    "mod_alias",
    "mod_compress",
     "mod_redirect",
)

server.document-root        = "/var/www/html" 
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log" 
server.pid-file             = "/var/run/lighttpd.pid" 
server.username             = "www-data" 
server.groupname            = "www-data" 
server.port                 = 80

index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/" 
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl" 
include_shell "/usr/share/lighttpd/include-conf-enabled.pl" 

## enable debugging
#debug.log-request-header     = "enable" 
#debug.log-response-header    = "enable" 
#debug.log-request-handling   = "enable" 
debug.log-file-not-found     = "enable" 
#debug.log-condition-handling = "enable" 

and in the conf-enabled subdirectory is only 10-cgi.conf, which contains:
(there is nothing else in this directory)

# /usr/share/doc/lighttpd/cgi.txt

server.modules += ( "mod_cgi" )

$HTTP["url"] =~ "^/cgi-bin/" {
        alias.url += ( "/cgi-bin/" => "/var/www/cgi-bin" )
        cgi.assign = (
                ".py"  => "/usr/bin/python",
        ".pl"  => "/usr/bin/perl",
        ".sh"  => "/bin/sh",
        )
}

Any assistance would be greatly appreciated, I am totally stuck and feeling frustrated.
Even any pointers on how to turn on debugging and the log files. I feel like I have made some really basic error, but just cant see what is wrong.

Cheers
Peter


Replies (2)

RE: Cant get a simple cgi-bin script to run - Added by gstrauss over 5 years ago

DebugVariables has additional lighttpd.conf debugging variables

My preference is to use strace to see what syscalls lighttpd is making about the file.

You should also check if SELinux is enabled and (temporarily) test with it disabled:

selinuxenabled && echo enabled || echo disabled

https://www.revsys.com/writings/quicktips/turn-off-selinux.html
https://www.raspberrypi.org/forums/viewtopic.php?t=16738

RE: Cant get a simple cgi-bin script to run - Added by draenog over 5 years ago

Thanks so much for the advice.
Firstly, I confirmed that I have no SELinux.

Tried doing an strace, and I think this revealed the issue. The critical trace lines are:

20:34:06.727486 ioctl(8, FIONREAD, [433]) = 0
20:34:06.727725 read(8, "GET /cgi-bin/hello.py HTTP/1.1\r\nHost: jumpbox\r\nConnection: keep-alive\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\r\nDNT: 1\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9\r\n\r\n", 4159) = 433
20:34:06.728243 stat64("/var/www/cgi-binhello.py", 0x7e8fe738) = -1 ENOENT (No such file or directory)

For some reason, it is stripping out the backslash after cgi-bin.
Next to figure out why!!!!

Update: solved the issue in my conf file, changed the line:

alias.url += ( "/cgi-bin/" => "/var/www/cgi-bin" )

to

alias.url += ( "/cgi-bin/" => "/var/www/cgi-bin/" )

and it works - yay!

Cheers, and thanks for the excellent strace tip

    (1-2/2)