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/cgiscripting

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 was just looking for this info for some time. After 6 hours of continuous Googleing, finally I got it in your website.
    I wonder what is the lack of Google strategy that do not rank this
    type of informative sites in top of the list. Normally the top web sites
    are full of garbage.

    • Thanks for the kind words!
      Actually, it’s my “fault”:
      This is a non-commercial project, i’m not planting links and optimizing for seo…

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

    • Under Add python test script, a sample python script is created and run by pressing the HTML button. You can put any Python code into that script.


Time limit is exhausted. Please reload CAPTCHA.