top of page

Set up a web server on the Raspberry Pi

This project creates a website that allows web users to send text over the internet to be displayed on a LCD connected to the Raspberry Pi.

 

  1. Allow access to your RPi from the internet:

  2. Connect the RPi to its router

  3. On the router, record your current IP address and DNS server address

  4. Reserve the RPi’s DHCP IP address, so it doesn’t change (usually 192.168...)

  5. Use port forwarding to point to that IP address

    1. Port 80 for standard web access

    2. I used port 181 because my router already has its own web server at port 80

    3. Also forward port 443 for ssl access

 

  1. Setup a Dynamic IP domain because the IP number your ISP gives you may change:

  2. Go to noip.com and setup an account.  You will need to activate it by email.

  3. Remember the new domain name, your account user name, and your noip.com password

  4. Set it to the current IP address that you recorded in step A2

  5. If you are not using port 80, set up ‘Web Redirect’ on noip.com

    1. Enter a website name that is different from the domain name from step B2

    2. For URL, enter your domain name from step B2, plus “:181”, where 181 is the port number you choose in step A4b.  This will redirect port 80 (web) accesses to your website name (B4a) to port 181 of your domain name.

  6. If you are using port 80, then the website name is the same as your domain name

 

  1. Setup the Dynamic DNS client on the RPi:

  2. ‘sudo apt-get update’

  3. ‘sudo apt-get upgrade’                              # makes sure you are on the latest Raspbian version

  4. ‘sudo apt-get install ddclient libjson-any-perl’

    1. just skip through the install questions, we’ll be overwriting them later

  5. Copy the file ‘ddclient-3.8.3.tar’ to the Raspberry Pi

  6. ‘tar -xvf ddclient-3.8.3.tar’

  7. ‘sudo cp -f ddclient-3.8.3/ddclient /usr/sbin/ddclient’

  8. ‘sudo mkdir /etc/ddclient’

  9. ‘sudo mv /etc/ddclient.conf /etc/ddclient’

  10. ‘sudo nano /etc/ddclient/ddclient.conf’

    1. use=web, web=checkip.dyndns.com/, web-skip='IP Address'

    2. ssl=yes

    3. protocol=dyndns2

    4. server=dynupdate.no-ip.com

    5. login=your_username  [from B2]

    6. password=your_password  [from B2]

    7. your.domain.com  [from B2]

  11. ‘sudo /etc/init.d/ddclient restart’

 

  1. Look for a IP address update every week:

  2. ‘sudo nano /etc/default/ddclient’

    1. run_daemon="true"

    2. run_dhclient="false"

    3. run_ipup="false"

  3. ‘sudo service ddclient start’

  4. ‘sudo nano /etc/cron.weekly/ddclient’

    1. #!/bin/sh

    2. /usr/sbin/ddclient -force 

  5. ‘sudo chmod +x /etc/cron.weekly/ddclient’

  6. ‘sudo service ddclient status’                   # to check that it is working

  7. To force a refresh of the IP address, ‘sudo ddclient -daemon=0 -debug -verbose -noquiet’

 

  1. Install a web server on the RPi:

  2. ‘sudo apt-get install apache2’

  3. If you are not using port 80 (A4a),

    1. ‘sudo nano /etc/apache2/port.conf’ and replace ‘Listen 80’ with your port number (A4b)

    2. ‘sudo nano /etc/apache2/sites-enabled/000-default.conf’ and replace ‘*:80’ in the first line with your port number (A4b)

  4. ‘sudo service apache2 restart’

  5. Check if this works by using a browser to access your website address (step B4a).  You may have to wait a few hours for the DNS information propagate through the internet.

  6. You can replace the default website with your own website by going to /var/www/html and replacing index.html (as superuser) with your own html file

 

  1. Enabling Python in Apache2 (the web server):

  2. Create a place to store the python scripts

    1. ‘sudo mkdir /var/www/cgi-bin’                # you can choose a different directory

  3. Add the following lines to the end of the apache2.conf file, ‘sudo nano /etc/apache2/apache2.conf’

    1. ‘ServerName localhost’

    2. ‘ScriptAlias /cgi-bin/ /var/www/cgi-bin/

    3. ‘Options +ExecCGI’

    4. ‘AddHandler cgi-script .cgi .pl .py’

    5. The web server can now run cgi, perl, and python scripts

  4. Modify the serve-cgi-bin.conf file, ‘sudo nano /etc/apache2/conf-available/serve-cgi-bin.conf’

    1. Find ‘ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/’  and replace with the following:

    2. ‘ScriptAlias /cgi-bin/ /var/www/cgi-bin/’

    3. ‘<Directory “/var/www/cgi-bin/”>’

    4. ‘             AllowOverride None’

    5. ‘             Options +ExecCGI’

    6. ‘</Directory>’

  5. sudo a2enmod cgi

  6. ‘sudo service apache2 restart’

  7. You can put your python script in the /var/www/cgi-bin/ directory, but make sure they are executable with ‘sudo chmod 777 *.py’

  8. Debug problems with your python script by looking at ‘var/log/apache2/error.log’

 

  1. Add an LCD display to the Raspberry Pi:

  2. You will need to connect a I2C LCD to the Raspberry Pi

    1. The LCD uses 5V, not 3.3V

  3. You will need to enable the I2C interface on the RPi

    1. In terminal, “sudo raspi-config” then enable the I2C interface

    2. In terminal, “sudo i2cdetect -y 0” to get the LCD’s address.  Our code uses address 0x3F

    3. ‘sudo /etc/udev/rules.d/99-com.rules’, change the permission for i2c from 0660 to 0666

    4. Reboot the RPi

  4. There are three files you need to put in /var/www/cgi-bin/

    1. i2c_lib.py – this teaches the RPi how to talk to the LCD Display

    2. lcddriver.py – these contain the actual commands that the LCD understands

    3. lcd.py – this is the code you run for displaying the text message to the LCD

    4. Make sure they are all executable with ‘sudo chmod 777 *.py’

  5. The html file goes in the /var/www/html directory

    1. Index.html – called automatically by the web browser and will transfer to the python script when needed

  6. There is a potentiometer behind the LCD.  You may have to adjust it to get a good contrast to see the display.

© 2017 by Prompt Box.

  • Facebook - Grey Circle
  • LinkedIn - Grey Circle
  • Google+ - Grey Circle
bottom of page