There are several ways to use Python to create a web ap­pli­ca­tion, or generate web content. In this tutorial we will cover the simplest and most basic form of viewing the output of a Python script in a browser.

In some sit­u­a­tions, running Python as a CGI script may be a good option:

  • The script is small and light­weight.
  • You are just starting out learning Python, and want to start with a simple solution.
  • You only need to do the most basic level of testing in a browser.
Tip

There are several better and more robust al­ter­na­tives to running a Python script as a CGI script. Based on your needs, we recommend either using Apache's mod_wsgi, or in­stalling a Python web framework like CherryPy.

Re­quire­ments

  • A Cloud Server with Apache con­fig­ured to allow CGI scripts.
VPS Hosting
VPS hosting at un­beat­able prices on Dell En­ter­prise Servers
  • 1 Gbit/s bandwidth & unlimited traffic
  • Minimum 99.99% uptime & ISO-certified data centers
  • 24/7 premium support with a personal con­sul­tant

Run Python as a CGI Script

After verifying that your server is con­fig­ured to allow CGI scripts, you can upload the Python script to your des­ig­nat­ed cgi-bin directory.

  • CentOS 7: The default CGI directory is var/www/cgi-bin/
  • Ubuntu 16.04: The default CGI directory is /usr/lib/cgi-bin

Give the file ex­e­cutable per­mis­sions:

CentOS 7:

sudo chmod 755 /var/www/cgi-bin/example.cgi

Ubuntu 16.04:

sudo chmod 755 /usr/lib/cgi-bin/example.cgi

You can now view the script in a browser, using either the domain name or IP address:

http://example.com/cgi-bin/example.cgi
http://192.168.0.1/cgi-bin/example.cgi

File Ex­ten­sions

To run your Python script as a CGI script, you can either:

  • Name your script with a .cgi file extension (example.cgi).
  • Configure Apache to recognize and allow the .py file extension as a CGI script.

To add the .py con­fig­u­ra­tion to Apache, edit the Apache con­fig­u­ra­tion file. On Ubuntu 16.04, this is already set by default. You will not need to make any changes to run a .py file as a CGI script.

On CentOS 7, open the httpd.conf file for editing:

sudo nano /etc/httpd/conf/httpd.conf

Find this section:

#
#
;Directory "/var/www/cgi-bin"
    AllowOverride None
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
    Require all granted
/Directory

Add .py to the Ad­dHan­dler con­fig­u­ra­tion:

#
#
Directory "/var/www/cgi-bin"
    AllowOverride None
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl .py
    Require all granted
/Directory

Save and exit the file. Then restart Apache so that the changes take effect:

sudo systemctl restart httpd

Trou­bleshoot­ing

For testing purposes, we recommend you use the following example script:

#!/usr/bin/env python
print "Content-Type: text/html"
print
print ';h1;Hello world./h1

Save this as example.py and upload it to your server's des­ig­nat­ed cgi-bin directory for testing. Then view the script in a browser, using either the domain name or IP address:

http://example.com/cgi-bin/example.py
http://192.168.0.1/cgi-bin/example.py

You will see "Hello world."

404 error: This means that the file cannot be found at the URL you specified. Be sure that the script is in the right directory.

  • CentOS 7: The default CGI directory is var/www/cgi-bin/
  • Ubuntu 16.04: The default CGI directory is /usr/lib/cgi-bin

Server 500 error: This usually indicates that the file's per­mis­sions are wrong. Be sure the script has ex­e­cutable (chmod 755) per­mis­sions:

jdoe@localhost:/etc/apache2# ll /usr/lib/cgi-bin/test.cgi
rwxr-xr-x 1 jdoe jdoe 85 Jul 22 16:53 /usr/lib/cgi-bin/test.cgi*

The correct per­mis­sions for the file are rwxr-xr-x. If not, give the file ex­e­cutable per­mis­sions:

CentOS 7:

sudo chmod 755 /var/www/cgi-bin/example.cgi

Ubuntu 16.04:

sudo chmod 755 /usr/lib/cgi-bin/example.cgi
$1 Domain Names – Register yours today!
  • Simple reg­is­tra­tion
  • Premium TLDs at great prices
  • 24/7 personal con­sul­tant included
  • Free privacy pro­tec­tion for eligible domains
Go to Main Menu