13

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

asked Oct 24, 2013 at 22:44
4
  • Just to be sure: Where did you place the script? And did you enable the Apache site (a2ensite my_cgi_dir)? Commented Oct 24, 2013 at 22:50
  • chmod +x? Readable by www-data? mod_python is totally irrelevant here, btw. Commented Oct 24, 2013 at 22:55
  • @ Robin Krahl: Not sure what does a2ensite mean. I put the script to /var/www/my_cgi_dir/. It (the site) is working, except it doesn't run it as python, simply displays its text. I also put some index.html into this directory: works just fine. Commented Oct 24, 2013 at 23:28
  • 1
    @jhermann: please see a) in my OP. Commented Oct 24, 2013 at 23:29

6 Answers 6

4

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.

answered Jul 26, 2014 at 15:30
Sign up to request clarification or add additional context in comments.

Comments

2

try this

 <Directory /var/www/>
 Options Indexes FollowSymLinks MultiViews
 AllowOverride None
 Order allow,deny
 allow from all
 </Directory>

cgi script

import cgi
import cgitb; cgitb.enable()
print "Content-type: text/html\n\n"
print "<h1>Hello from Python!</h1>"

Why don't you configure like this? here.

ScriptAlias /cgi-bin/ /var/www/my_cgi_dir/
<Directory "/usr/lib/cgi-bin">
 AllowOverride None
 Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
 Order allow,deny
 Allow from all
</Directory>
answered Oct 25, 2013 at 2:02

3 Comments

I was not sure where I should add this config section; so I tried to add it before > Directory /var/www/my_cgi_dir/> --- anyhow, it didn't help, sorry.
Tried to add this section right into apache2.conf as well: with same effect (no effect).
try into "/etc/apache2/sites-enabled/000-default"
2

In my case, I made a mistake with my ScriptAlias directive. I uncommented the original one, but forgot to configure a new one.

As soon as I correctly changed and saved my sites-available/default config file from this:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">

.. to this:

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">

.. and reloaded apache2, it just worked: it stopped displaying my scripts as text, and started running them as a script. Also, it no longer displayed /var/www/cgi-bin as a directory in the browser, but now correctly displays the error:

Forbidden 
You don't have permission to access /cgi-bin/ on this server.
answered Nov 15, 2013 at 23:20

1 Comment

So OP, as you said, the ScriptAlias directive is in your /etc/apache2/sites-available/my_cgi_dir file, but is it in your /etc/apache2/sites-enabled/000-default file as well? (This is what a2ensite is used for btw.) I'm pretty sure your ScriptAlias directive isn't pointing to the directory your script is in.
0

I was having the same problem and tried all of the above. Nothing helped. In the end it turned out to be a really stupid mistake. Since many people seem to have problems with this, I will post it anyway:

I had not sym-linked my default site from "sites-available" to "sites-enabled" as described at the top of the apache.conf file:

[...] Configuration files in the mods-enabled/ and sites-enabled/ directories contain particular configuration snippets which manage modules or virtual host configurations, respectively.

They are activated by symlinking available configuration files from their respective *-available/ counterparts. These should be managed by using our helpers a2enmod/a2dismod, a2ensite/a2dissite. See their respective man pages for detailed information. [...]

Thus, all my edits to the default file were not read by apache. Once I made the symlink, it worked.

answered Jul 31, 2014 at 13:29

Comments

0

This worked for me,, as @flyking_ suggested, follow with some extra steps in the same order.

I had to change in the directory - I am using raspberrypi (NOOB version linux) /etc/apache2/sites-available/default

maintain the ones in these adding +ExecCGI and also Addhander cgi-script .cgi .py as below

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

Then restart apache

service apache2 restart

This should restart the service, and verify whether the python script runs fine without errors in the terminal, before launching it in the browser.

If no errors, then the sampe script would run fine.

import cgi
import cgitb; cgitb.enable()
print "Content-type: text/html\n\n"
print "<h1>Hello from Python!</h1>"

Imp: later my script worked fine even without both the imports.

Note: Clear the cache in the browser, or load it afresh with ctrl+F5. Hopefully this should solve.

If this doesnt solve, then try this as @user2449877 suggested Check loaded modules with:

 apache2ctl -M

Look for:

 cgid_module (shared)

If it's not loaded, load it with:

 a2enmod cgid 

Restart apache and then refresh browser

answered Aug 19, 2014 at 11:06

Comments

0

In my case (which is a shared unix machine), I also need to create a hidden file ".htaccess" with the following:

Options +ExecCGI
SetHandler cgi-script 

It is also important to set the permissions accordingly (755 - group and other with no writing but execution permissions) as well as having:

#!/usr/bin/python

as a first line of the python CGI script.

answered Jun 18, 2015 at 20:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.