Skip to content

Instantly share code, notes, and snippets.

@boseji
Last active May 1, 2019 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boseji/7cfd363ef45b19009a705b781a8668e3 to your computer and use it in GitHub Desktop.
Save boseji/7cfd363ef45b19009a705b781a8668e3 to your computer and use it in GitHub Desktop.
Raspberry Pi 3 Steps for LAMP Server

LAMP Server Setup on Raspberry Pi 3

Install the Raspbian

First we need to load the latest Image of Raspbian

Next in case you have a display :

Waveshare 7inch HDMI LCD (B), 800×480

** 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.

Install Nginx PHP and MySQL for LEMP stack

Follow instructions from

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04

More on Multidomain Hosting

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts

Details about Nginx Configuration Files

https://www.digitalocean.com/community/tutorials/understanding-the-nginx-configuration-file-structure-and-configuration-contexts

PHPMyAdmin Setup:

https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-an-ubuntu-14-04-server

Make sure to use alternative linking

sudo ln -s /usr/share/phpmyadmin /var/www/html/
# Works better

DO NOT restart the Raspberry Pi yet

Check status of Nginx server running

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.

Adjust FSTAB for Faster Boot and Use on Raspberry Pi 3

/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

Install Golang

# 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.

A basic Golang Server

~/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.

Golang App as a service

/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

Golang App endpoint to Nginx

/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

Some additionals

https://howtoraspberrypi.com/install-nginx-raspbian-and-accelerate-your-raspberry-web-server/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment