<p>This is kind of a silly question, but all of the examples I've seen of deploying a Go project use App Engine and I just want to deploy on an Ubuntu server using Digital Ocean. How do I go from running <code>127.0.0.1:8080/index</code> locally to running <code>example-site.com/index</code> on a remote server?</p>
<hr/>**评论:**<br/><br/>Battleroid: <pre><p>I would imagine like you do so with any other Web application. Probably use Nginx to proxy requests to it.</p></pre>manwith4names: <pre><p>Any examples of using Nginx to proxy requests? I've never deployed a web application before</p></pre>Battleroid: <pre><p>I'm on my phone but in Nginx it's usually as simple as just rerouting requests for / to something like proxy_pass 127.0.0.1:8000. </p>
<p>If you search say, how to host a Go or even a Python Web application with Nginx I'm sure you'll find a bunch of info.</p>
<p>After I get some sleep I'll try to post something a bit more constructive.</p></pre>weberc2: <pre><p>You don't need Nginx for this AFAIK; you can just make your app listen on port 80 and run it as sudo.</p>
<p>EDIT: never mind. Don't do this. It's a bad security practice. Thanks to those who corrected me.</p></pre>flexd: <pre><p>Please do not encourage people to run things as root.</p>
<p>You could do <code>setcap 'cap_net_bind_service=+ep' /path/to/program</code> to give it the <code>CAP_NET_BIND_SERVICE</code> capability to be able to bind to ports under 1024 without root.</p>
<p>But the recommended way is to use nginx/apache/Caddy <a href="https://caddyserver.com/docs/proxy" rel="nofollow">https://caddyserver.com/docs/proxy</a> to proxy requests to the backend.</p></pre>weberc2: <pre><p>I didn't know that. Thanks for the heads up.</p></pre>Battleroid: <pre><p>Yeah that too, but I would just use Nginx anyway. More control over how you proxy requests and junk like that. That and you shouldn't host user applications below port 1023 (?, might be 1024 can't remember).</p></pre>weberc2: <pre><p>Yeah, another user corrected me as well. Evidently you shouldn't run your application as root, but instead use a reverse proxy.</p></pre>weberc2: <pre><p>Not a silly question. You need to have a remote server and upload your program to that server (FTP, rsync, scp, etc). You'll also need some way to connect port 80 to whatever port your application is listening on (you can even make your app listen on 80, though you'll likely need to run it as a privileged user). Finally, you'll need your Ubuntu server to have a static IP address and you'll need to purchase a the domain name to register your IP address to (this last point is a bit complex, and I don't know what you already know about DNS, so feel free to ask further questions).</p></pre>manwith4names: <pre><p>I'm planning to use digital ocean for my remote server and I believe I need to use Nginx?</p></pre>flexd: <pre><p>You can use nginx, or apache, or caddy (written in go). You have lots of options!</p>
<p><a href="https://caddyserver.com" rel="nofollow">https://caddyserver.com</a></p></pre>weberc2: <pre><p>Evidently you should use nginx or caddy; my advice about running your application as root and listening on 80 is insecure. In the past I've used rsync to deploy my payload to DO from my build server; this worked pretty well. You'll also want to configure your server to use upstart or whatever Ubuntu is using these days; this will make sure your app runs on server reboot and it will restart it if it crashes, etc.</p></pre>neoasterisk: <pre><p>First just transfer your Go app to the remote server. You can do that either by uploading the binary and any static files via <code>ssh</code>, particularly with the <code>scp</code> command or alternatively:</p>
<ul>
<li>install Go to the remote server</li>
<li><code>go get</code> your app </li>
<li>deploy with<code>go install</code>.</li>
</ul>
<p>The remote server has an IP say <code>104.1.2.3</code> so after you deploy the Go app to the remote server you can access it by visiting <code>http://104.1.2.3:8080/index</code>. Of course it's more human friendly to use domain names that lead to IPs. So if in your DNS you have that <code>example-site.com -> 104.1.2.3</code> then you can access the app via <code>http://example-site.com/index</code>. You need to check the settings of your DNS provider for that.</p>
<p>Another thing you need to be aware of is port forwarding which practically means that you need to have port 8080 open in the firewall. It's more common for security reasons to have 8080 disabled and access every app from port 80. If the only thing you need to run is your Go app then just configure it to listen on 80 and you are done. The flag package is ideal for that so that when you start your app in the remote server you can do something like <code>goapp -http=localhost:80</code>. That gives you the flexibility to change the ports that your apps are using without recompiling.</p>
<p>If you want to run more than one apps then you need to configure another server to act as a reverse proxy like <code>nginx</code> or <code>apache2</code>. That way you could for example have <code>http://example-site.com/index</code> served from Go app1 and <code>http://example-site.com/service</code>served from Go app2.</p>
<p>In this case, you need to configure the reverse proxy in such a way so that <code>/index</code> goes to <code>0.0.0.0:8080</code> (Go app 1) and <code>/service</code> goes to <code>0.0.0.0:8081</code> (Go app2).</p>
<p>Now after you are done with your tests, you can turn your Go app into a service so that it automatically starts when the Ubuntu server starts. That can be done with Upstart or Systemd depending on your version. Check out a sample Upstart configuration file:</p>
<pre><code># goapp upstart conf script
description "goapp upstart conf script"
start on (net-device-up and local-filesystems and runlevel [2345])
stop on runlevel [!2345]
respawn
respawn limit 10 5
console log
exec /home/yourname/go/bin/goapp -http="localhost:8080"
</code></pre>
<p>Modify and write to a file <code>/etc/init/goapp.conf</code> and then start the service by <code>sudo service goapp start</code>.</p>
<p>Lastly, if you log stuff in your application like with the standard log package <code>log.Println("An important event occurred!")</code> then you can find those messages under <code>/var/log/upstart/goapp.log</code>.</p>
<p>Happy hacking!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传