【问题标题】:How do I deploy a golang app with Apache installed on Ubuntu 16.04 on digitalocean?如何在 digitalocean 上的 Ubuntu 16.04 上部署安装了 Apache 的 golang 应用程序?
【发布时间】:2017-03-10 00:36:03
【问题描述】:

我现在正在学习 Go,并且我已经按照一些使用 net/http 包的教程构建了非常简单的 web 应用程序。我创建了一个简单的愿望清单,我在其中添加了一个项目,然后将它添加到我想要的简单表格中,非常简单。

现在我想将此应用程序部署到我的 Digital Ocean 液滴中,但我不知道如何操作。我有一些具有不同域的 php 网站已经使用 Apache。

我真的是这个“服务器配置”的初学者,通常使用 php 在 webhosts 上很容易,我不需要这么多经验。您能否指出正确的方向,让我的 Go 应用程序在我拥有的域中可用,而无需端口位?最好使用 Apache。

谢谢:)

【问题讨论】:

  • 强烈反对“离题”的近距离投票。关于 Apache 配置的问题似乎很适合 SO。

标签: apache go deployment digital-ocean ubuntu-16.04


【解决方案1】:

注意:此答案中的几乎所有内容都需要根据您的具体情况进行定制。这是在假设您的 Go 应用程序名为“myapp”并且您已使其侦听端口 8001(以及许多其他端口)的情况下编写的。

您应该制作一个systemd 单元文件,以使您的应用在启动时自动启动。将以下内容放入/etc/systemd/system/myapp.service(根据您的需要调整):

[Unit]
Description=MyApp webserver

[Service]
ExecStart=/www/myapp/bin/webserver
WorkingDirectory=/www/myapp
EnvironmentFile=-/www/myapp/config/myapp.env
StandardOutput=journal
StandardError=inherit
SyslogIdentifier=myapp
User=www-data
Group=www-data
Type=simple
Restart=on-failure

[Install]
WantedBy=multi-user.target

有关这些设置的文档,请参阅:man systemd.unitman systemd.serviceman systemd.exec

开始吧:

systemctl start myapp

检查是否正常:

systemctl status myapp

启用自动启动:

systemctl enable myapp

然后是时候为您的应用程序配置 Apache 虚拟主机了。将以下内容放入/etc/apache2/sites-available/myapp.conf

<VirtualHost *:80>
        ServerName myapp.example.com
        ServerAdmin webmaster@example.com
        DocumentRoot /www/myapp/public
        ErrorLog ${APACHE_LOG_DIR}/myapp-error.log
        CustomLog ${APACHE_LOG_DIR}/myapp-access.log combined

        ProxyPass "/" "http://localhost:8001/"
</VirtualHost>

代理相关设置文档:https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

启用配置:

a2ensite myapp

确保您没有在 Apache 配置中出错:

apachectl configtest

如果代理模块之前未启用,此时您将收到错误消息。在这种情况下,启用代理模块并重试:

a2enmod proxy
a2enmod proxy_http
apachectl configtest

重新加载 Apache 配置:

systemctl reload apache2

记得让名称myapp.example.com 在 DNS 中可用。

就是这样!

编辑:添加了指向文档的指针和启用 Apache 模块的说明(如果需要)。使用 apachectl 进行配置测试。

【讨论】:

    猜你喜欢
    • 2017-08-11
    • 2016-08-22
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2017-03-23
    相关资源
    最近更新 更多