Reputation: 2218
I created a tiny Go program on my computer:
package main
import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Using the Terminal on Mac:
$ cd $HOME/go/src/hello
$ ./hello
The server is starting. And using Safari:
http://localhost:8080/hello
I get "Hi there, I love hello!"
So far so good. But my challenge is to put this binary on the server and run the same.
On the server
Debian 8 using webmin creating a folder in root Go and put the binary in this folder
[root@server ~]# cd /go/
[root@server go]# ./hello
sh: 1: ./hello: Permission denied
[root@server go]#
The permissions for the Go folder and hello binary are 0644. AFAIK, the same as most files.
How do I deploy the Golang to the server and start the golang on the server? Lots of documentations on previous step, but I found none on the server side.
Upvotes: 1
Views: 1872
Reputation:
Go to folder where your code located, and type in terminal GOOS=linux GOARCG=amd64 go build
after it golang wiil create file for you, upload it on server and type chmod +x your_file_name
after it run it using ./your_file_name
Upvotes: 2
Reputation: 1366
Permission denied
usually points to a missing executable permission on the file.
The hello binary should have 755
or 700
on it (the former making it executable for everyone, the latter only for your user).
Run
chmod 755 ~/go/hello
or alternatively
chmod -R +x ~/go
The first sets explicit permission bits on just the server executable file, the second adds just the executable permission bit on the whole directory, as well as all contained files (and subdirectories). (Directories usually also have the x
-Bit set, otherwise you wouldn't be able to list it's contents)
Upvotes: 1
Reputation: 357
I think that you'll need to use cross-compiling.
When you launch go build -o hello .
on the mac, it's building a binary for you Mac.
But for running it on linux, you need to use cross-compilation: GOOS=linux go build -o hello .
. It will build the binary for your server.
You can read this article from Dave Cheney: https://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5
Upvotes: 4