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.
VPS Hosting
Fully virtualized servers with root access
  • Unlimited traffic
  • Fast SSD NVMe storage
  • Free Plesk Web Host Edition

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 – Grab your favorite one
  • Simple registration
  • Premium TLDs at great prices
  • 24/7 personal consultant included
  • Free privacy protection for eligible domains
Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top