How do you go from serving locally to online?

xuanbao · · 425 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>This is kind of a silly question, but all of the examples I&#39;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></code> locally to running <code></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&#39;ve never deployed a web application before</p></pre>Battleroid: <pre><p>I&#39;m on my phone but in Nginx it&#39;s usually as simple as just rerouting requests for / to something like proxy_pass </p> <p>If you search say, how to host a Go or even a Python Web application with Nginx I&#39;m sure you&#39;ll find a bunch of info.</p> <p>After I get some sleep I&#39;ll try to post something a bit more constructive.</p></pre>weberc2: <pre><p>You don&#39;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&#39;t do this. It&#39;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 &#39;cap_net_bind_service=+ep&#39; /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="" rel="nofollow"></a> to proxy requests to the backend.</p></pre>weberc2: <pre><p>I didn&#39;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&#39;t host user applications below port 1023 (?, might be 1024 can&#39;t remember).</p></pre>weberc2: <pre><p>Yeah, another user corrected me as well. Evidently you shouldn&#39;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&#39;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&#39;ll likely need to run it as a privileged user). Finally, you&#39;ll need your Ubuntu server to have a static IP address and you&#39;ll need to purchase a the domain name to register your IP address to (this last point is a bit complex, and I don&#39;t know what you already know about DNS, so feel free to ask further questions).</p></pre>manwith4names: <pre><p>I&#39;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="" rel="nofollow"></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&#39;ve used rsync to deploy my payload to DO from my build server; this worked pretty well. You&#39;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></code> so after you deploy the Go app to the remote server you can access it by visiting <code></code>. Of course it&#39;s more human friendly to use domain names that lead to IPs. So if in your DNS you have that <code> -&gt;</code> then you can access the app via <code></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&#39;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></code> served from Go app1 and <code></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></code> (Go app 1) and <code>/service</code> goes to <code></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 &#34;goapp upstart conf script&#34; 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=&#34;localhost:8080&#34; </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(&#34;An important event occurred!&#34;)</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群:701969077

425 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传