Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble running python script as cgi under apache (ubuntu 12)

Disclosure: I searched a lot, and I don't think my question (for my configuration) is answered here. For example run python script as cgi apache server doesn't answer it.

So: I have a simplest script possible:

#!/usr/bin/env python

print "Content-type: text/html"
print ""

print "<h1>Hello from Python!</h1>"

When I run it in a browser, it literally displays itself instead of expected Hello from Python!

I did the following to make it run:

a) it is executable by everyone; It runs in a shell perfectly.

b) it is in a virtual directory that has the following configuration (in/etc/apache2/sites-available/my_cgi_dir):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/
    <Directory />
           Options FollowSymLinks
           AllowOverride None
    </Directory>
   <Directory /var/www/my_cgi_dir/>
           Options Indexes +ExecCGI FollowSymLinks MultiViews
           AddHandler cgi-script .cgi .py
           AllowOverride None
          Order allow,deny
          allow from all
   </Directory>

   ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
   <Directory "/usr/lib/cgi-bin">
           AllowOverride None
           Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
           Order allow,deny
           Allow from all
   </Directory>

   ErrorLog ${APACHE_LOG_DIR}/error.log
   LogLevel warn
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

As you see it has

       Options Indexes +ExecCGI FollowSymLinks MultiViews

and

       AddHandler cgi-script .cgi .py

c) I made sure apache has python support by running sudo apt-get install libapache2-mod-python

d) Yes I did restart apache.

Still, I just see the script's source instead of "Hello Python".

What am I missing?

Please help.


PS: if that might help, here is what I am running:

Linux ip-172-31-37-178 3.2.0-40-virtual #64-Ubuntu SMP Mon Mar 25 21:42:18 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

Server Version: Apache/2.2.22 (Ubuntu)

Python 2.7.3

like image 965
bzdjamboo Avatar asked Oct 24 '13 22:10

bzdjamboo


1 Answers

What could also cause these symptoms, is that you don't have the apache2 cgi module loaded. This will also generate a log message in /var/log/apache2/access.log with an HTTP 304 error:

192.168.2.3 - - [26/Jul/2014:11:56:34 +0200] "GET /cgi-bin/hello.py HTTP/1.1" 304 179 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"

Check loaded modules with:

apache2ctl -M

Look for:

cgid_module (shared)

If it's not loaded, load it with:

a2enmod cgid

Then restart apache2:

service apache2 reload

Then refresh your browser and purge your browser cache (CTRL + F5). And/or restart your browser, to be sure it's requesting the actual page, instead of using the browser cache.

like image 65
Raoul Avatar answered Oct 04 '22 17:10

Raoul