"bind: address already in use" when trying to create my own api and using apache2

Akavall asked:

I am using apache2 webserver on ec2 instance and I am trying to use a simple go script that I can do a get request on.

My go scipt looks like this:

package main

import (
    "net/http"
    "fmt"
)

func say_hi(w http.ResponseWriter, r *http.Request) {
        fmt.Printf("33[0;32m%v33[0mn", r)
        fmt.Fprint(w, fmt.Sprint("Hi"))
}

func main() {
    http.HandleFunc("/say_hi", say_hi)

    ip_address := "0.0.0.0:8080"

    fmt.Printf("Set up ip address: %sn", ip_address)
    error := http.ListenAndServe(ip_address, nil)
        if error != nil {
        fmt.Println(error)
    }

}

When I run the script on my ec2 server I get the following error:

listen tcp 0.0.0.0:8080: bind: address already in use

I really don’t understand why I get this error.

I tried to investigate further

ubuntu@ip-172-31-37-75:~$ socklist
type  port      inode     uid    pid   fd  name
tcp     22       8895       0      0    0  
tcp   3306       9160     106      0    0  
tcp     22       9569       0      0    0  
tcp  44403          0       0      0    0  
udp  18341       7860       0      0    0  
udp     68       7939       0      0    0 

And it does not seem like anything is using those ports.

I have ports 80 and 8080 open:

ubuntu@ip-172-31-37-75:~$ netstat -nlp | grep "80"
(No info could be read for "-p": geteuid()=1000 but you should be root.)
tcp6       0      0 :::8080                 :::*                    LISTEN      -               
tcp6       0      0 :::80                   :::*                    LISTEN      -  

and my ports.conf file looks like this:

Listen 80
Listen 8080

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

I am complete beginner, so I might be missing some really obvious.

My answer:


You already have a running program listening on port 8080, as evidenced by your netstat output. You seem to indicate that it is Apache. You will need to reconfigure Apache so that it does not listen on port 8080, or choose a different port on which to run your Go program.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.