Kaveh Shahbazian
Kaveh Shahbazian

Reputation: 13513

Deploying Go web applications with Apache

I can not find a mod_go for deploying Go web applications. Is there any other way to run web applications in Go with an Apache web server (or even IIS)?

Update: Now after doing Go full time for nearly a year; doing this (Go with Apache) is nullifying the very purpose of Go (Performance in terms of being highly concurrent). And I'm using nginx as a reverse proxy for http/https and having my Go backends behind it nicely. Though you have to change your mindset on webapps a bit when using Go.

Upvotes: 30

Views: 39253

Answers (6)

Mateus Lopes
Mateus Lopes

Reputation: 86

This would proxy the domain and subdomain requests to the specified ports.

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName www.yourdomain.com
    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName subdomain.yourdomain.com
    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass / http://127.0.0.1:8081/
    ProxyPassReverse / http://127.0.0.1:8081/
</VirtualHost>

Upvotes: 4

Mostafa
Mostafa

Reputation: 28086

There's no mod_go. (At least I've not heard of such a thing.)

A go web app by itself is a capable web server. You can listen to port 80 in your app and then directly run it on your server. Trust me: it really works!

But if you are not doing that (for reasons such as having other virtual servers on the same machine, load balancing, etc.), you can use a HTTP server such as nginx or Apache as a HTTP proxy in front of your Go app. I use nginx and it's great. Here's an outdated but still very useful guide on how to do that with nginx. I haven't done it with Apache, but this should help.

I recommend your Go web app by itself or nginx as a HTTP proxy.

Upvotes: 31

user1784334
user1784334

Reputation: 191

For those interested there is a recently released mod_go for Apache in here:

https://github.com/idaunis/mod_go

Upvotes: 15

abbot
abbot

Reputation: 27850

I just use the web server's proxy facility and run my app as a regular daemon (using daemonize) on the server. On apache that would be ProxyPass + ProxyPreserveHost.

Upvotes: 2

Evan Shaw
Evan Shaw

Reputation: 24547

In addition to the other options, there's also the net/http/fcgi package. This is similar to the CGI option, but it uses FastCGI and your application can keep state if it needs to.

Here's the FastCGI version of jimt's example. Note that only two lines are different. Depending on how you configure Apache, you may have to change the first argument to something different, but nil is the common case.

package main

import (
    "fmt"
    "net/http"
    "net/http/fcgi"
)

func hello(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello from Go!")
}

func main() {
    http.HandleFunc("/", hello)
    fcgi.Serve(nil, nil)
}

Upvotes: 24

jimt
jimt

Reputation: 26370

While not ideal, you can run Go programs as CGI scripts by placing them in the cgi-bin directory. You can invoke them like any other page through server.com/cgi-bin/myapp?foo=bar

An example program would look like this:

package main

import (
    "fmt"
    "net/http"
    "net/http/cgi"
)

func hello(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello from Go!")
}

func main() {
    http.HandleFunc("/", hello)
    cgi.Serve(nil)
}

The reason this is not as optimal as running the program as its own server, is that with a cgi approach, the program is invoked for every single request. So any state inside it does not persist.

For clarity: You should place the compiled binary in the cgi-bin directory. Not the program source.

Upvotes: 10

Related Questions