Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Python Scripts with Lighttpd and CGI

I am having a problem getting python scripts to execute within Lighttpd and cgi-bin. I have found similar issues within stackoverflow (i.e. Lighttpd and cgi python) and other websites, but none fully pertain to my configuration. I can execute the standalone python script by issuing "python flash.py" without any problems.

A key point that might help solve this is the fact that everything was working fine prior to me running "apt-get update" and "apt-get upgrade". I have experimented by messing with permissions on certain files, and messing with the config files, but none of which helped.

I have since put everything back to the state at which it was just after running the updates. This is new territory for me and I'm just not educated enough to find anything obvious. As it stands, here is my current configuration.

/etc/lighttpd/lighttpd.conf

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

server.document-root        = "/var/www"
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"

#auth.backend = "plain"
#auth.backend.plain.userfile = "/etc/lighttpd/.lighttpdpwd"

#auth.require = ( "/var/www" =>
#(
#.method. => "basic",
#.realm. => "Authentication required",
#.require. => "user=admin"
#)
#)

etc/lighttpd/conf-enabled/10-cgi.conf

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

server.modules += ( "mod_cgi" )

$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" )
}

## Warning this represents a security risk, as it allow to execute any file
## with a .pl/.py even outside of /usr/lib/cgi-bin.
#
#cgi.assign      = (
#       ".pl"  => "/usr/bin/perl",
#       ".py"  => "/usr/bin/python",
#)

/var/www/cgi-bin/flash.py

#Dog Treat Dispenser. Flash Code
import RPIO
import time
import cgi

FLASHER = 22
#ADD CLICKER!

RPIO.setup(FLASHER , RPIO.OUT)  #Set FLASHER pin as OUTPUT

for x in range(0, 5):                   #Flash for 2 seconds

        RPIO.output(FLASHER, True)

        #ADD CLICKER SUBROUTINE
        time.sleep(.500)

        RPIO.output(FLASHER, False)

        #ADD CLICKER SUBROUTINE
        time.sleep(.500)

# reset every channel that has been set up by this program,
# and unexport interrupt gpio interfaces
RPIO.cleanup()

print "Content-Type: text/html"
print "Location: http://10.143.141.164"
print
print "<html><head>"
print "<title>Flash!</title>"
print "</head>"
print "<body>"
print "<h1>Flash!</h1>"
print "</body>"
print "</html>"

After doing a ton of research, and getting nowhere, I'm at a loss. Any help you could provide would be greatly appreciated. If there is anything I have missed, please let me know and I will do my best to get it to you.

Thank you!

like image 446
user2909404 Avatar asked Oct 23 '13 02:10

user2909404


1 Answers

This should work:

server.modules += ( "mod_cgi" )

cgi.assign    = ( ".pl"  => "/usr/bin/perl",
                  ".py"  => "/usr/bin/python" )

What version of python are you running? Did you upgrade and install python3? If so you will need to install python2 as well and change /usr/bin/python to /usr/bin/python2

Did you set mimetypes for python in your mimetypes.conf files for Lighttpd? it should look like this:

".py" => "text/x-python",
".pyc" => "application/x-python-code",
".pyo" => "application/x-python-code",

Did you check lighttpd's error logs before make any changes? The logs are stored in /var/log/lighttpd/error.log

What is the exact issue that you are having? Do the files try to download when you navigate to the directory? This is difficult to troubleshoot without more information.

like image 134
Dolores Avatar answered Oct 01 '22 08:10

Dolores