NGINX Unit

Grafana§

Here, we install Grafana from sources so we can configure it to run in Unit.

  1. Install Unit with Go support, making sure Unit’s Go modules are available at $GOPATH.

  2. Download Grafana files:

    $ go get github.com/grafana/grafana
    $ cd $GOPATH/src/github.com/grafana/grafana # path to Grafana
    
  3. Update the code, adding Unit to the list of supported protocols. You can apply a patch (grafana.patch):

    $ cd /path/to/grafana
    $ curl -O https://unit.nginx.org/_downloads/grafana.patch
    $ patch -p1 < grafana.patch
    

    Otherwise, update the sources manually. In conf/defaults.ini:

    #################################### Server ##############################
    [server]
    # Protocol (http, https, socket, unit)
    protocol = unit
    

    In pkg/api/http_server.go:

    import (
        // ...
        "net/http"
        "nginx/unit"
        "os"
        // ...
    )
    
    // ...
    
    switch setting.Protocol {
    
    // ...
    
    case setting.UNIT:
        err = unit.ListenAndServe(listenAddr, hs.macaron)
        if err == http.ErrServerClosed {
            hs.log.Debug("server was shutdown gracefully")
            return nil
        }
    

    In pkg/setting/setting.go:

    const (
        HTTP              Scheme = "http"
        HTTPS             Scheme = "https"
        SOCKET            Scheme = "socket"
        UNIT              Scheme = "unit"
        DEFAULT_HTTP_ADDR string = "0.0.0.0"
    )
    
    // ...
    
    Protocol = HTTP
    protocolStr, err := valueAsString(server, "protocol", "http")
    // ...
    if protocolStr == "unit" {
        Protocol = UNIT
    }
    
  4. Build your Grafana app:

    $ cd /path/to/grafana
    $ go run build.go setup
    $ go run build.go build
    $ yarn install --pure-lockfile
    $ yarn start
    

    Note the directory where the newly-built grafana-server is placed; it’s needed for Unit configuration.

  5. Prepare Unit configuration for your update. To obtain an initial template, query the control API:

    # curl --unix-socket /path/to/control.unit.sock \
           http://localhost/config/ > config.json
    

    Note

    Control socket path may vary; run unitd --help or see Startup for details.

    This creates a JSON file with Unit’s current settings. In listeners, add a listener that points to your app in applications; the app must reference the path to Grafana and the executable you’ve built:

    {
        "listeners": {
            "*:3000": {
                "pass": "applications/grafana"
            }
        },
    
        "applications": {
            "grafana": {
                "executable": "/path/to/grafana/build/dir/grafana-server",
                "type": "external",
                "user": "grafanauser",
                "working_directory": "/path/to/grafana/"
             }
         }
     }
    

    See Go application options for details.

  6. Upload the updated configuration:

    # curl -X PUT --data-binary @config.json --unix-socket \
           /path/to/control.unit.sock http://localhost/config
    

    After a successful update, Grafana should be available on the listener’s IP and port:

    Grafana in Unit - Setup Screen