Skip to content

Raspberry Apache run py cgi scripts

How to enable .py scripts in Apache2

By default Python is configured to only execute scripts with specific endings such as .cgi. In order to make Apache execute .py files, you need to edit a configuration file. In a terminal type this command:

sudo leafpad /etc/apache2/sites-enabled/000-default &

This file contains settings for the default site (you can create other sites on the same server). Look for a section of code that looks like this:

<Directory “/usr/lib/cgi-bin”>


Add the following line inside the above section of code:

AddHandler cgi-script .py

In a terminal window, type this command to reload Apache’s configuration files:

sudo service apache2 reload

Now type this code into a text editor, and save it as /usr/lib/cgi-bin/

#!/usr/bin/env python
print “Content-type: text/html\n\n”
print “<h1>Hello World</h1>”

Make the file executable using this command:

$ sudo chmod +x

As in the previous post, you can execute this script by accessing it through your browser. The IP address of my Raspberry Pi is, so I typed this address into my browser: Once again, you should see the words “Hello World” in large letters.

Raspberry serial

Disable Serial Port Login

To enable the serial port for your own use you need to disable login on the port. There are two files that need to be edited

The first and main one is /etc/inittab

This file has the command to enable the login prompt and this needs to be disabled. Edit the file and move to the end of the file. You will see a line similar to

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Disable it by adding a # character to the beginning. Save the file.

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Monitor Raspberry temperature online

The simplest way to be able to see the temperature of your raspberry is to write it in the /var/www/index.html.

You will need to set up the webserver as described in the previous post. Then write a simple script that reads in the temperature and then prints it in the index.html. Then run the script automatically as frequently as you wish with the help of the crontab software. You can set it to every minute, every hour, every two days or whatever.


  1. nano /usr/local/bin/

cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTempM=$(($cpuTemp2 % $cpuTemp1))

echo CPU temp"="$cpuTemp1"."$cpuTempM"'C" >> /var/www/index.html
echo GPU $(/opt/vc/bin/vcgencmd measure_temp) >> /var/www/index.html
timestamp() {
date +"%T"
echo "Time: $(date)" >> /var/www/index.html
echo "<br>"/var/www/index.html

2. Then make it executable:

chmod a+x /usr/local/bin/

3. Then add it to crontab:

crontab -e
and add to the file:
*/5 * * * * sudo /usr/local/bin/

to run it every 5 minutes.

Raspberry Pi webserver

This is mostly a note to myself about what I do when setting up a web server on the pi.

  • Install one
    • sudo apt-get install apache2 -y
  • This time I won’t need php, so I will install that later.
  • The wbserver started running automatically after installation, so just punch in the Pi’s IP address to the browser and it should send you to the index page of the Webserver.

After this, I want to make it available from the web too, not only my local network. It is connected to the router via an ethernet cable, but I think the router would assign a new IP to it after each restart.

So I need to give a static IP to it so it will be the same after every restart, so in the router settings I can set up port forwarding for it.

Setting the PI’s ip to static (more info):

    • sudo nano /etc/network/interfaces
    • or sudo vi /etc/network/interfaces depending on editor preference

auto lo

iface lo inet loopback
iface eth0 inet static

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Then it should take effect after restart:

  • sudo reboot

Now for this IP we need to set up port forward in the routers setting page. Get the user manual of your router and set up http portforward for the Pi’s ip (in my case on port 80.

After that, get your outside IP, type in the browser get my IP and then type that in to the browser.

Mine is:

You should see what is running on my PI’s webserver, by clicking on it.

Python list comprehension avagy lista kód rövidebben és hatékonyabban

This is how to do it the long way:


for i in itotal:


The short way:

panelTemp=[float(d)+(i*40/1000) for d,i in zip(dryBulbTemperature,itotal)]


For average of a list:

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l) / len(l)