You will need several programs that are not shipped with FreeBSD. Run the following command to install them:
pkg install nano wget git mercurial bzr
Download golang by running the following set of commands:
cd /tmp
wget https://storage.googleapis.com/golang/go1.3.3.freebsd-amd64.tar.gz
tar -C /usr/local -xzf go1.3.3.freebsd-amd64.tar.gz
Create a variable called GOPATH
(which will be the location for installed packages) and add it to your path:
mkdir ~/.gopkg
setenv GOPATH /root/.gopkg
set path = ($path /usr/local/go/bin /root/.gopkg/bin)
If you want to have the path set on boot, then run the following command to add it to your .cshrc
:
echo "setenv GOPATH /root/.gopkg" >> ~/.cshrc
echo "set path = ($path /usr/local/go/bin /root/.gopkg/bin)" >> ~/.cshrc
Run go
in your terminal. If you are presented with a list of options, then the installation was successful. Run the following command to install a web framework called Martini
:
go get github.com/go-martini/martini
If you don’t see any errors, then you may proceed to the next step.
Create a file called server.go
and populate it with the following lines of code:
package main
import "github.com/go-martini/martini"
func main() {
m := martini.Classic()
m.Get("/", func() string {
return "Hello from AKLWEB Host VPS :)!"
})
m.Run()
}
When done, save and run go run server.go
. Provided that you do not see any errors on your terminal, then you can proceed to the next step.
Configure Nginx to reverse proxy to the Martini server. In /usr/local/etc/nginx/nginx.conf
look for location
and replace its content within the curly brackets with the following:
expires 8d;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_read_timeout 5m;
proxy_connect_timeout 5m;
proxy_cache_key sfs$request_uri$scheme;
proxy_pass http://127.0.0.1:3000;
proxy_redirect off;
One you have added that, save and run service nginx restart
or service nginx onerestart
, then run go run server.go
. In your browser, enter http://0.0.0.0
(change the IP accordingly) and you will see a page that says:
Hello from AKLWEB Host VPS :)!
Congratulations, you have successfully setup an Nginx reverse proxy server with Golang + Martini.