1

I'm trying to call a Python script from a webpage, and apache2 is not actioning the script. The files are:

test.py

#!/usr/bin/env python
print "Content-type: text/html\n\n"
print "<h1>Hello World</h1>"

index.html

<form action="./test.py" method="POST">
    <input type="text" size="70" name="data" value=""><br>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
</form>

I've enabled a2enmod cgi and edited apache settings as follows:

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
...
<directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
    AddHandler cgi-script .py
    Require all granted
</directory>
</VirtualHost>

Webpage files ownerships are:

# ls -lh /var/www/html/
total 16K
-rw-r--r-- 1 root root  342 May 30 13:34 index.html
-rwxr-xr-x 1 root root   88 May 30 14:11 test.py

Running apachectl -M brings up a list with line cgid_module (shared).

When I bring up the page I can see the form, but when I click 'Submit' the browser either prompts to download test.py (FF) or views it (Chrome). Any idea what I'm getting wrong here?

5
  • A good way to check if your meta info tag works is to use Ctrl+Shift+J and check the network tab and the returned content-type of your py script. What does chrome say you're getting, text/plain file? I'm guessing you've reloaded the servers configuration? Commented May 31, 2016 at 16:55
  • Screen shots for what you ask in Chrome and FF - imgur.com/a/lGkN5 Commented May 31, 2016 at 17:07
  • I'm on the phone so can't write a proper answer yet. Your mime info is wrong, the content-type for your py file is document and there for py script isn't registered as a handler. Have a look at httpd.apache.org/docs/2.4/mod/mod_mime.html for now Commented May 31, 2016 at 17:10
  • 1
    Also are you sure you placed the file under /usr/lib/cgi-bin? What does apache say in the logs? Just to be clear changing the mime type manually won't make the script execute, just to show you why you're getting document :) Commented May 31, 2016 at 17:14
  • Ah that's it! I needed to point <directory..> to /var/www/html.. Commented May 31, 2016 at 17:21

1 Answer 1

1

As discussed in the comments. This directory configuration looks like a plain copy from an example, and your Web root is probably somewhere else.

<VirtualHost *:80>
    <directory "/var/www/htdocs">
        AllowOverride None Options
        +ExecCGI -MultiViews 
        +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all 
        AddHandler cgi-script .py
        Require all granted
    </directory>
</VirtualHost>

For instance where directory is changed. That's the default path usually, if yours is /var/www/html change accordingly.

I've gotten shit for this earlier but this question might also be better off on serverfault considering this is strictly related to how apache should be configured.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Torxed. I agree serverfault is ideally better, but I was persuaded by the 3X more apache posts in SO..
@geotheory i was too at first, but I'm trying to get a more frequent user base there so the searches related to server stuff ends up there hehe. Glad it worked out this time tho :) and you're more than welcome!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.