Tested on:Raspberry Pi 2; Raspberry Pi B+; Python 2.7; Raspbian pre Jessie
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)

Apache Web Server with cgi for python scripts

Apache is old, but powerful, well-documented, robust and secure. In here, an apache web server is setup on a Raspberry Pi with cgi support for running python scripts.

Install apache
Type in the terminal

Apache starts right after the installation.

Control Apache manually
After changing some configuration, either restarting the server with stop and start or reload is necessary.

Trigger bootup behaviour
Apache starts after every reboot per default. To stop that, type

Entering that without the option -r reactivates the start of apache after booting.

Sample Website

The file index.html is in /var/www.
Open the file with sudo nano /var/www/index.html and add before the line /body>:

To display the website, type the Rasperry Pis’ IP into the web browsers address bar(from the Raspberry Pi or a machine in the same network) when Apache is running.
One option for implementing interactions is creating HTML objects like buttons in index.html and link them over JavaScript with a python function. Find more information about Apache on the Apache Software Foundation website.

Executing python scripts
Thanks to Raspberrywebserver.com for their great article on python cgi scripts.

Add permission

Below the line


and save the file.
Then, reload apache:

Add python test script

The file should contain

Make it executable:

Test the script
In the browser, goto the adress

with RASPI_IP being the IP adress of the Raspberry Pi.
The browser displays a fat SPAM.

Apache Foundation Website: https://www.apache.org
Raspberrywebserver.com Website: http://www.Raspberrywebserver.com/writing-cgi-scripts-in-python.html

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


  1. I need to run a python script when I press the button. For example, sending a Serial value or recieving one.


Time limit is exhausted. Please reload CAPTCHA.