【发布时间】:2018-07-28 22:19:56
【问题描述】:
很遗憾,我无法在生产服务器上部署基本的 Golang WebApp。在阅读了许多文档和教程后,我了解到我需要将 Golang WebApp 作为守护程序运行。
首先要做的事:生产服务器是运行 Ubuntu 16.04 的单个 IP,带有基于 Apache 的多个 VirtualHosts /etc/apache2/sites-enabled/。
Golang 环境变量
# set golang environment vars
export GOROOT=/usr/local/go
# set multiple gopaths seperated by ":"
export GOPATH=/var/www/go_projects/gotest.domain2.com
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
# set PATH so it includes user's private bin directories
PATH="$HOME/bin:$HOME/.local/bin:$PATH"
Systemd 守护进程文件
[Unit]
Description=GoTest Webserver
[Service]
Type=simple
WorkingDirectory=/var/www/go_projects/gotest.domain2.com
ExecStart=/var/www/go_projects/gotest.domain2.com/main #binary file
[Install]
WantedBy=multi-user.target
虚拟主机会议
<VirtualHost *:80>
ServerName gotest.domain.com
DocumentRoot /var/www/go_projects/gotest.domain2.com
<Directory /var/www/go_projects/gotest.domain2.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
去文件
package main
import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
不幸的是,http://gotest.domain2.com 上的程序没有被执行。而是列出DocumentRoot的内容
手动运行返回
admin@xyz:/var/www/go_projects/gotest.domain2.com$ ./main
2018/02/18 15:52:58 listen tcp :8080: bind: address already in use
我缺少什么或者我的部署方法主要是错误的? 干杯!
编辑: 根据 Michael Ernst 的建议,我尝试更改端口/代理设置,结果如下:
http://gotest.domain2.com leads to 503 Service Unavailable
以下是sudo netstat -talpen的结果
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 16367 1250/sshd
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 0 473536 26340/master
tcp 0 0 0.0.0.0:143 0.0.0.0:* LISTEN 0 16604 1417/dovecot
tcp 0 0 0.0.0.0:2000 0.0.0.0:* LISTEN 5001 17289 1652/asterisk
tcp 0 xx.xx.xx.xx:22 xx.xx.xx.xx:6126 ESTABLISHED 0 615988 13025/sshd: admin [
tcp6 0 0 :::22 :::* LISTEN 0 16369 1250/sshd
tcp6 0 0 :::25 :::* LISTEN 0 473537 26340/master
tcp6 0 0 :::3306 :::* LISTEN 111 17564 1391/mysqld
tcp6 0 0 :::143 :::* LISTEN 0 16605 1417/dovecot
tcp6 0 0 :::80 :::* LISTEN 0 612412 12554/apache2
tcp6 0 0 xx.xx.xx.xx:80 xx.xx.xx.xx:6128 FIN_WAIT2 0 0 -
tcp6 0 0 xx.xx.xx.xx:80 xx.xx.xx.xx:6129 ESTABLISHED 33 615029 12561/apache2
知道问题出在哪里吗?
【问题讨论】:
-
你为什么使用 apache 来托管 Golang 应用程序?
-
干杯 Anuruddha,有两个原因,1) 服务器已经托管了几个 PHP 框架 2) 我只熟悉 Apache (PHP)。欢迎提出新建议。
标签: apache go webdeploy virtual-hosts