Executing Python Scripts with Lighttpd and CGI
Asked Answered
B

2

6

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!

Barrel answered 23/10, 2013 at 2:2 Comment(0)
C
3

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.

Contract answered 23/11, 2013 at 22:14 Comment(0)
P
0

OP mentions putting script in /var/www/cgi-bin/flash.py whereas on my system (Ubuntu trusty), I put scripts in directory /usr/lib/cgi-bin/script.py and access them via browser using URL: http://localhost/cgi-bin/script.py

In anycase, I landed on this SO question while trying to make cgi python script work in lighttpd so putting this info as reference - otherwise @Dolores's answer should suffice - again in my case the config was

$HTTP["remoteip"] =~ "127.0.0.1" {     
  alias.url += ( "/cgi-bin/" => "/usr/lib/cgi-bin/" )
  $HTTP["url"] =~ "^/cgi-bin/" {
    cgi.assign = ( ".sh" => "/bin/sh" )
  }
}

as put in /etc/lighttpd/conf-enabled/10-cgi.conf , enabling cgi module using cmd: lighty-enable-mod cgi and then doing a reload of lighttpd using cmd: /etc/init.d/lighttpd force-reload

Following is the script.py:

import time
print("Content-Type: text/html\n\n")  # html markup follows

timeStr = time.strftime("%c") # obtains current time

htmlFormat = """
<html>
<Title>The Time Now</Title>
<body>
<p>The current Central date and time is:  {timeStr}</p>
</body>
</html> """

print(htmlFormat.format(**locals())) # see embedded %s ^ above
Protoxylem answered 18/4, 2017 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.