Run Python as a CGI Script

There are several ways to use Python to create a web application, 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 situations, running Python as a CGI script may be a good option:

  • The script is small and lightweight.
  • 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 alternatives to running a Python script as a CGI script. Based on your needs, we recommend either using Apache's mod_wsgi, or installing a Python web framework like CherryPy.

Requirements

  • A Cloud Server with Apache configured to allow CGI scripts.

vServer (VPS) from IONOS

Low-cost, powerful VPS hosting for running your custom applications, with a personal assistant and 24/7 support.

100 % SSD storage
Ready in 55 sec.
SSL certificate

Run Python as a CGI Script

After verifying that your server is configured to allow CGI scripts, you can upload the Python script to your designated 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 executable permissions:

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 Extensions

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 configuration to Apache, edit the Apache configuration 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 AddHandler configuration:

#

#
<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

Troubleshooting

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 designated 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 permissions are wrong. Be sure the script has executable (chmod 755) permissions:

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 permissions for the file are rwxr-xr-x. If not, give the file executable permissions:

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 great TLDs for less than $1 for the first year.

Why wait? Grab your favorite domain name today!

Matching email
SSL certificate
24/7/365 support

Wait! We’ve got something for you!
Discover our special deals at IONOS!
Our most popular products, including cloud storage, hosting,
domains and more - now on sale.