如何在DigitalOcean上的Ubuntu 16.04上部署一个带有安装了Apache的Golang应用程序?

huangapple go评论87阅读模式
英文:

How do I deploy a golang app with Apache installed on Ubuntu 16.04 on digitalocean?

问题

我目前正在学习Go语言,并且按照一些教程使用net/http包构建了一些非常简单的Web应用程序。我创建了一个简单的愿望清单,可以添加物品,并将其显示在一个简单的表格中,非常简单。

现在我想将这个应用程序部署到我的Digital Ocean droplet上,但我不知道如何操作。我已经有一些使用Apache的不同域名的php网站。

对于这个"服务器配置"的事情,我真的是一个初学者,通常在web主机上使用php非常容易,我不需要这么多经验。你能指导我如何使我拥有的Go应用程序在我拥有的域名上可用,而不需要端口吗?最好使用Apache。

谢谢 如何在DigitalOcean上的Ubuntu 16.04上部署一个带有安装了Apache的Golang应用程序?

英文:

I am learning Go at the moment and I have built really simple webapps following some tutorials with the net/http package. I have created a simple wishlist, where I add an item and than it does to a simple table of things I want, pretty simple.

Now I want to deploy this app to my Digital Ocean droplet, but I just don't know how. I have some php websites with different domains already with Apache behind it.

I am really a begginer on this "servers configuration" thing, usually with php is pretty easy on webhosts and I didn't need this much experience. Can you point me on the right direction to make my Go app available at a domain I own, without the ports bit? Preferably with Apache.

Thanks 如何在DigitalOcean上的Ubuntu 16.04上部署一个带有安装了Apache的Golang应用程序?

答案1

得分: 14

注意:这个回答中的几乎所有内容都需要根据你的具体情况进行自定义。这篇文章假设你的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

记得在DNS中将myapp.example.com这个名称可用。

就是这样了!

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

英文:

Note: Almost everything in this answer needs to be customized to your specific circumstances. This is written with the assumption that your Go app is called "myapp" and you have made it listen at port 8001 (and many others).

You should make a systemd unit file to make your app start up automatically at boot. Put the following in /etc/systemd/system/myapp.service (adapt to your needs):

[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

For documentation of these settings see: man systemd.unit, man systemd.service and man systemd.exec

Start it:

systemctl start myapp

Check that it is ok:

systemctl status myapp

Enable automatic startup:

systemctl enable myapp

Then it is time to configure Apache virtualhost for your app. Put the following in /etc/apache2/sites-available/myapp.conf:

&lt;VirtualHost *:80&gt;
        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 &quot;/&quot; &quot;http://localhost:8001/&quot;
&lt;/VirtualHost&gt;

Documentation of the proxy related settings: https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

Enable the configuration:

a2ensite myapp

Make sure you did not make mistake in Apache configuration:

apachectl configtest

In case the proxy modules are not previously enabled you will get an error at this point. In that case enable the proxy modules and try again:

a2enmod proxy
a2enmod proxy_http
apachectl configtest

Reload Apache configuration:

systemctl reload apache2

Remember to make the name myapp.example.com available in DNS.

That's it!

EDIT: Added pointers to documentation and instructions for enabling Apache modules if needed. Use apachectl for config test.

huangapple
  • 本文由 发表于 2016年10月27日 18:49:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/40282697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定