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.
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
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 "Hello from Python!
"
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