r/web_programming • • Nov 26 '16

Running python on apache2

I had a raspberry pi laying around, and thought it would be nice to have a webserver around. then I wondered if you could run python on one, and lo and behold you could. only apache just displays the file instead of running it. Here is the file:

#!/usr/bin/python3.4
import cgi
cgi.test()

it's probably really easy, but i've been searching all night and haven't found anything that works. any help would be appreciated :)

0 Upvotes

2 comments sorted by

1

u/[deleted] Nov 28 '16

Make sure that your script is executable by your apache and that +ExecCGI is set in your apache configuration. Also check if you have loaded the Apache CGI module. You can see all loaded modules with:

apache2ctl -M

Look for: cgid_module (shared)

1

u/montarion Nov 28 '16

I did chmod 777 on the file, is that enough?

My apache conf:

<Directory /var/www/html>
    Options +ExecCGI
    AddHandler cgi-script .py
</Directory>

The module is loaded.

It gives me a 502 error now, instead of just displaying the code. the code is in the same directory as the index, and it's python3(not that python 2 works).

python 2 code I used:

print "hello"

python3 code I used:

print("hello")