Run a simple Python CGI on Apache Web Server

Published 6 months ago: July 20, 2020

A how-to on running a simple Python CGI script on an Apache HTTP Web Server


  • Python 3.x

First things first

First, save the script below to a file.

#!/usr/bin/env python3
import cgi
from string import Template

# enable debugging
import cgitb

print('Content-Type: text/html; charset=utf-8\n')


  1. Configure Apache's configuration file
    1. Open the config file in any editor. Usually exists in /etc/apache2/apache2.conf. Example: sudo nano /etc/apache2/apache2.conf
    2. Put this in at the end: ScriptAlias /myscript/ "/var/www/html/cgi-bin/". 'myscript' is the URL you want the script at.
  2. Put the Python script in cgi-bin (usually /var/www/html/cgi-bin/)
    1. Example: sudo cp /path/to/script/ /var/www/html/cgi-bin/
  3. Make sure Apache has permission to execute the file chmod +x

Christoffer Lybekk

Developer under development

Explore more articles with similar tags

PythonCGIApache HTTP ServerGuide

Article stats




1 minread time