我正在ec2实例上使用apache2 webserver,我正在尝试使用一个简单的go脚本,我可以做一个get请求。
我的scipt看起来像这样:
package main import ( "net/http" "fmt" ) func say_hi(w http.ResponseWriter, r *http.Request) { fmt.Printf("\033[0;32m%v\033[0m\n", 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: %s\n", ip_address) error := http.ListenAndServe(ip_address, nil) if error != nil { fmt.Println(error) } }
当我在我的ec2服务器上运行脚本时,出现以下错误:
listen tcp 0.0.0.0:8080: bind: address already in use
我真的不明白为什么我得到这个错误。
我试图进一步调查
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
而且似乎没有任何东西在使用这些端口。
我有端口80和8080打开:
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 -
和我的ports.conf文件看起来像这样:
Listen 80 Listen 8080 <IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule>
我是完整的初学者,所以我可能会错过一些非常明显的。
您已经有一个正在运行的程序在端口8080上侦听,如netstat输出所certificate。 你似乎表示它是Apache。 您需要重新configurationApache,以便它不会侦听端口8080,或者select其他端口来运行您的Go程序。