如何在 digitalocean 上的 Ubuntu 16.04 上部署安装了 Apache 的 golang 应用程序?
How do I deploy a golang app with Apache installed on Ubuntu 16.04 on digitalocean?
我目前正在学习 Go,我已经按照 net/http 包的一些教程构建了非常简单的 Web 应用程序。我创建了一个简单的愿望清单,我在其中添加了一个项目,然后它就添加了一个简单的 table 我想要的东西,非常简单。
现在我想将此应用程序部署到我的 Digital Ocean droplet,但我不知道如何操作。我有一些 php 个不同域的网站已经使用 Apache 支持。
我真的是这个 "servers configuration" 的初学者,通常 php 在虚拟主机上很容易,我不需要这么多经验。你能给我指明正确的方向,使我的 Go 应用程序在我拥有的域中可用,而无需端口位吗?最好使用 Apache。
谢谢:)
注意:此答案中的几乎所有内容都需要根据您的具体情况进行定制。这是在假设你的 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.unit
、man systemd.service
和 man 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 进行配置测试。
我目前正在学习 Go,我已经按照 net/http 包的一些教程构建了非常简单的 Web 应用程序。我创建了一个简单的愿望清单,我在其中添加了一个项目,然后它就添加了一个简单的 table 我想要的东西,非常简单。
现在我想将此应用程序部署到我的 Digital Ocean droplet,但我不知道如何操作。我有一些 php 个不同域的网站已经使用 Apache 支持。
我真的是这个 "servers configuration" 的初学者,通常 php 在虚拟主机上很容易,我不需要这么多经验。你能给我指明正确的方向,使我的 Go 应用程序在我拥有的域中可用,而无需端口位吗?最好使用 Apache。
谢谢:)
注意:此答案中的几乎所有内容都需要根据您的具体情况进行定制。这是在假设你的 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.unit
、man systemd.service
和 man 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 进行配置测试。