Amazon Web Services (AWS)

⌘K
  1. Home
  2. Docs
  3. Amazon Web Services (AWS)
  4. Python Configuration

Python Configuration

Once the LAMP stack is installed it will be nice to use some of the features in Python, like Seaborn charts. Python is not configured by default in Apache like PHP so apache needs to be configured to run Python scripts as an external executable. This type of configuration is called CGI (Common Gateway Interface) and was how PERL and other languages were included on web pages before embedded languages like PHP were created.

We will assume Python is already installed. If not, get Python from http://python.org/download/ and follow the instructions.

Configure Apache to Run Python CGI

Edit the Apache configuration file

cd /etc/httpd/conf/
vi httpd.conf
  1. Search for “Options Indexes FollowSymLinks” and add “ExecCGI” to this line.
  2. Search for “#AddHandler cgi-script .cgi” and add “.py” to this line.

Commands in vi:

  • Search: / enter term and enter
  • Edit: a (append) or i (insert) allows modification of the current line
  • Write file and Quit: Escape (to get out of edit mode) : (colon) “wq” Enter

Restart Apache

cd /etc/init.d
sudo httpd -k restart

Configure Apache to run Python CGI

Edit the httpd.conf apache configuration file located in

cd /etc/httpd/conf/
vi httpd.conf

Search for “Options Indexes FollowSymLinks” and add “ExecCGI” to this line.
Search for “#AddHandler cgi-script .cgi” and add “.py” to this line.

Commands in vi:

  • Search: / enter term and enter
  • Edit: a (append) or i (insert) allows modification of the current line
  • Write file and Quit: Escape (to get out of edit mode) : (colon) “wq” Enter

Restart Apache

cd /etc/init.d
sudo httpd -k restart

Run a Test Python Page

The critical part of the script is the first line which tells the web server what executable to run the script with. Create a file with the .py extension (test.py) on the website. Note the two blank lines after Content-type. Print commands should also have parenthesis though the script may work on other installations without them.

#!/usr/bin/python3
print("Content-type: text/html")
print()
print()
print("<html><head>")
print ("</head><body>All y'all base are belong to us</body></html>")

Loading the page in a web browser should render only the message. Error 500 or the raw file loaded onto the page means the Apache configuration is wrong (or there is an error in the Python script).

How can we help?