Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a test script, /var/www/etym/cgi-bin/test.py

# -*- coding: UTF-8 -*-

# enable debugging
import cgitb

print "Content-Type: text/plain;charset=utf-8"

print "Hello World!"

And I'm trying to run it by going to http://localhost/etym/cgi-bin/test.py in my browser, but it's not working--Firefox just asks me whether I'd like to open the file with gedit or save it to the disk. What do I need to do to get it to run? I already ran chmod +x on the script.

share|improve this question
Could you explain what your requirements are? Most people use wsgi for Python nowadays or simply a framework to build the applications in. Do you really need to do it plain CGI or...? And what web server are you using? (You probably don't want to run one, or you want a wsgi module for you web server.) –  gertvdijk Aug 26 '13 at 19:21
I just want to be able to run a simple python script on a web server. I'm just using the standard apache2 that comes with ubuntu and the stock configuration. I don't know the difference between cgi and wsgi, and so I'm not married to CGI, but I don't want to have to configure a big framework, apache modules, or other complicated things just to be able to see "Hello World" in my browser window. –  Jon Aug 26 '13 at 19:33
You can try flask (flask.pocoo.org/docs/quickstart/#quickstart) for simple python "hello world" web scripts. –  pondohva Aug 26 '13 at 20:17
"/var/www/etym/cgi-bin/test.py" please put those files OUTSIDE your webroot. –  Rinzwind Apr 29 at 14:59

2 Answers 2

up vote 1 down vote accepted

Make sure you have configured your webserver properly to run CGI scripts.

Refer this for more info: https://httpd.apache.org/docs/2.4/howto/cgi.html

share|improve this answer

I had to change in the directory - I am using raspberrypi (NOOB version linux)


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.

share|improve this answer
Does this answer pertain to any version of Ubuntu? If so which one? –  user68186 yesterday

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.