Web Server Setup

To view txt version of this document, click here.

To set up Apache web server on Raspberry Pi:

Official guide: https://projects.raspberrypi.org/en/projects/lamp-web-server-with-wordpress

It should work on localhost or local IP. By default Apache listens on port 80.

To connect localhost to WWW:

  1. Find Raspberry Pi’s local IP using router’s web portal. Typically, the router’s portal is 192.168.0.1 or 192.168.1.1. If you have a modem and a router, use the modem’s portal because it associates your public IP to the device. Router (the one behind modem) would only have private IP.
  2. Make RPi’s local IP static by reserving it on the router. For example, if RPi’s local IP is 192.168.0.13, reserve that IP and associate it with RPi’s MAC address. Anytime RPi reboots or rejoins the WiFi, the router will assign it the reserved local IP.
  3. Set up port forwarding. You need MAC address, local IP address, port, device name of the RPi. Local port can be 80 (default) for HTTP. External port and IP can be the same port as local port, and 0.0.0.0 respectively, meaning that any IP from WWW can access through the specific port. Examples: http: 80 https: 443 ssh: 22
  4. Set up Dynamic DNS service to connect your domain to your public IP. I use Google Domains for my domain. Google Domains > DNS > Synthetic records > Dynamic DNS > Subdomain.<domain.com> > Add. Under newly created Dynamic DNS record, clickview credentials to get Username and Password.
  5. Use the API to update your Dynamic DNS record. https://support.google.com/domains/answer/6147083?authuser=1. It can be a simple HTTPS GET request. dns_update.sh:
server_ip="$(curl checkip.amazonaws.com)"
wget https://username:password@domains.google.com/nic/update?hostname=subdomain.yourdomain.com&myip=$server_ip -O result.txt
echo "  Last run: `date`" >> result.txt

Username and password are from step 4. Also change the hostname to <subdomain>.<domain>.com.

  1. Run the script chronically using crontab job.

    crontab -e
    
    #  m h dom m dow 
       0 * * * * ~/dns_update.sh
    

    For more details, http://www.farrellf.com/projects/software/2016-05-03_Setting_Up_a_Raspberry_Pi_Web_Server/

Note IPv4 is required for the dynamic DNS to work. Somehow, the RPi is using IPv6 by default. This causes any ip checks to return IPv6 address. However, it will not work with dynamic DNS. To get IPv4 address, I added the following lines to /etc/sysctl.conf:

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv6.conf.eth0.disable_ipv6 = 1
net.ipv6.conf.[interface].disable_ipv6 = 1

Save and close. Activate with sudo sysctl -p, or reboot.

Reference: How to switch off IPv6 on RPi? https://www.raspberrypi.org/forums/viewtopic.php?t=138899