Running a Python script on an Apache web server

If you attempt to access a Python script on a site using the Apache webserver software and simply see the Python code, you may need to place the script in a cgi-bin directory, if you wish to use the Common Gateway Interface (CGI) method of running scripts - see HOWTO Use Python in the web for drawbacks of using this method, though it may be the simplest way to run such scripts on a web site that won't have a lot of load from Python scripts on the site.

Assuming you are running your own server and have access to Apache's httpd.conf file, look for a line similar to the following line in the VirtualHost section for the website:

ScriptAlias /cgi-bin/ "/home/jdoe/public_html/cgi-bin/"

If there is not such a line, you may need to add it and restart Apache, e.g., with apachectl restart.

You can have multiple ScriptAlias directories in the VirtualHost section for a site - see the Configuring CGI Scripts section of 1.4 Web Server Configuration from CGI Programming with Perl1x1px. E.g.:

ScriptAlias /cgi-bin/ "/home/jdoe/public_html/cgi-bin/"
ScriptAlias /cgi2 "/home/jdoe/public_html/test/cgi-bin"

If you can run a Python script from a command line interface (CLI), aka shell prompt, without errors, e.g., python example.py, but receive the error message below when running the script on an Apache web server, you may need to change the permissions on the script to make it "executable".

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@moonpoint.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

You can change the permissions on a file with the chmod command. The command chmod 755 somefile.py will make the file somefile.py executable for all accounts on the system or you can use chmod u+x,g+x,o+x - see Linux File Permissions for information on setting file permissions on Unix/Linux systems.

A test script that you could place in a cgi-bin directory for testing is shown below:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

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

print "Hello World!"

You could name the file test.cgi or you may find that test.py will work. Another example is shown below:

#!/usr/bin/env python

print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n'
print '<html>\n'

print '<head>'
print '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
print '<title>Ads</title>'
print '<meta name="description" content="Ads">'
print '<meta name="keywords" content="ads">'
print '<link rel="stylesheet" href="/css/style.css" type="text/css">'
print '</head>\n'

print '<body>\n'

print '<p>Testing</p>'

print '\n</body>\n'

print '</html>'

Note: you need to include the "pound-bang" line, i.e., #!, informing the system to use Python for the script or you will get an internal error page displayed. The \n on the lines are to put "newline characters at the ends of lines, i.e., to put spacing between lines to make the code more readable if you view the source code for the page in the browser.

In the first example, there are cgitb lines, i.e.:

# enable debugging
import cgitb
cgitb.enable()

As explained at HOWTO Use Python in the web, these make it possible to display a traceback for debugging problems with a script initially instead of just crashing and displaying an “Internal Server Error” in the user's browser. Though this is useful for debugging, it may risk exposing some confidential data to the user, so you should not use cgitb in production code for this reason. You should always catch exceptions and display error pages rather than have the web server display “Internal Server Errors” in visitors' browsers.

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px

Valid HTML 4.01 Transitional

Created: Sunday November 1, 2015