First we need to load the latest Image of Raspbian
Next in case you have a display :
** Now in the /boot/config.txt
**
...
#edit for the HDMI Display Resolution - Add at the end of the file
max_usb_current=1
hdmi_group=2
hdmi_mode=87
hdmi_cvt 800 480 60 6 0 0 0
hdmi_drive=1
This is generally accesable from the FAT filesystem used for boot.
Follow instructions from
Make sure to use alternative linking
sudo ln -s /usr/share/phpmyadmin /var/www/html/
# Works better
DO NOT restart the Raspberry Pi yet
Use this command
sudo service nginx status
If you see any errors then sure there may be some configuration issue in Nginx
RESTART the Raspberry Pi NOW
Then again test the above command.
/etc/fstab
....
tmpfs /var/tmp tmpfs defaults,noatime,nosuid,mode=0700,size=50M 0 0
tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=50M 0 0
tmpfs /var/log/nginx tmpfs defaults,noatime,nosuid,mode=0755,size=10M 0 0
tmpfs /var/lib/sudo tmpfs defaults,noatime,nosuid,mode=0755,size=2M 0 0
tmpfs /var/spool/mqueue tmpfs defaults,noatime,nosuid,mode=0700,gid=12,size=30M 0 0
tmpfs /tmp tmpfs defaults,noatime,mode=1777,size=50M
.....
Note that Adding the nginx log partition is important.
RESTART the Raspberry Pi
# Download the latest golang install - Specail for Raspberry Pi
wget -c https://storage.googleapis.com/golang/go1.9.linux-armv6l.tar.gz
# Install directly to location
sudo tar -C /usr/local -xzf go1.9.linux-armv6l.tar.gz
# Remove the archive
rm -rf go1.9.linux-armv6l.tar.gz
# Create the local workspace
cd
mkdir -p go/src go/bin
Next to Setup Golang path add a file to profiles
/etc/profile.d/env-vars.sh
# In the File write the following
export ENV_VARS='LOADED'
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
export GOSERVER1_PORT=18531
Note that you can set any GOSERVER1_PORT
its for use in the program and Nginx to know how to route
RESTART the Raspberry Pi
Now to check if every thing is work use these two commands
echo $ENV_VARS
LOADED
echo $GOSERVER1_PORT
18351
If the command output show the correct values then your configuration for golang is perfect.
~/go/src/gserver/gserver.go
package main
import (
"fmt"
"net/http"
)
func handler(response http.ResponseWriter, request *http.Request) {
fmt.Fprintf(response, "Hello, %s!", request.URL.Path[1:])
}
func main() {
srvport := ":18531" // From the Envonmental variable GOSERVER1_PORT
fmt.Println("Running Server on", srvport)
http.HandleFunc("/", handler)
http.ListenAndServe(srvport, nil)
}
Note that this uses the earlier set environmental variable GOSERVER1_PORT
for calculating which port to open.
Run the Go program and You can test it out by checking the URL:
http://RaspberryPi-Ip:18531/hari would give Hello hari!
http://RaspberryPi-Ip:18531/Kirshna would give Hello Hrishna !
Now where the RaspberryPi-Ip
needs to be replaced with the actual IP address of the Raspberry Pi.
The port 18531
should be in accordance to what was set for GOSERVER1_PORT
.
If this works then the golang web aplication is indeed working.
First try to install this and then test it again to verify the generated executable's operations.
/etc/init.d/gserver.sh
#!/bin/bash
case $1 in
start)
echo "Starting Golang Web app."
/home/pi/go/bin/gserver &
;;
stop)
echo "Stopping Golang Web app."
sudo kill $(sudo lsof -t -i:18531)
;;
*)
echo "Golang Web app service."
echo $"Usage $0 {start|stop}"
exit 1
esac
exit 0
Change Permission for Excurable script:
sudo chmod 755 /etc/init.d/gserver.sh
And to make it sart at Boot time:
sudo update-rc.d gserver.sh defaults
Start the service
sudo /etc/init.d/gserver.sh start
/etc/nginx/sites-enabled/default
# Adding Path for Golang server
location /iot {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:18531;
}
Restart the service
sudo service nginx restart
https://howtoraspberrypi.com/install-nginx-raspbian-and-accelerate-your-raspberry-web-server/