dongling0519
2018-01-27 18:27
浏览 237
已采纳

在Ubuntu / Centos上部署和运行Go API服务器

I just finished my first backend with Go using Iris framework but now I need to put it on production so I can use it in the Slack app I built.

In order to test the code locally I only run my file with go run main.go and ngrok to test with the Slack API, it's working and it's finished.

I have a droplet with Ubuntu 16.04.3 and other one with Centos 7... I was searching for something like pm2 for go, running the server and using nginx to point that port but I read that with Go it's different and I have to use something like this https://fabianlee.org/2017/05/21/golang-running-a-go-binary-as-a-systemd-service-on-ubuntu-16-04/

But that's a very long configuration for a simple server and my questions are:

  • Is this the usual way to config the APIs with Go?
  • Apart of DigitalOcean, do you recommend to use a different service to run my API?

This is really my first time with Go and I just want to learn more, I am a backend developer with Laravel and NodeJS.

图片转代码服务由CSDN问答提供 功能建议

我刚刚使用Iris框架完成了Go的第一个后端,但现在我需要将其投入生产,以便可以使用

为了在本地测试代码,我只使用 go run main.go ngrok < / code>使用Slack API进行测试,它可以正常工作,并且已经完成。

我在Ubuntu 16.04.3上有一个Droplet,在Centos 7上有一个...我在寻找东西 就像 pm2 一样,运行服务器并使用nginx指向该端口,但是我读到Go内容就不同了,我必须使用类似 https://fabianlee.org/2017 / 05/21 / golang-runing-go-binary-as-a-systemd-service-on-ubuntu-16-04 /

但这是一个非常 一个简单服务器的长时间配置,我的问题是:

  • 这是平常的吗
  • 作为DigitalOcean的一部分,您是否建议使用其他服务来运行我的API?

    这真的是我第一次使用Go,我想了解更多,我是Laravel和NodeJS的后端开发人员。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhe6287 2018-01-27 19:40
    已采纳

    I think most people use Supervisor for this purpose, including me.

    To make it very easy for you, just take a look at my Golang project, isaac-racing-server and use it as a template for yours by replacing isaac-racing-server with the name of your app. (The Supervisor files are in a subdirectory.)

    已采纳该答案
    打赏 评论
  • dongsun2789 2018-01-29 16:20

    You can use pm2 if you want. When you build a go project it creates a binary executable, lets say backend-server, which you can run from terminal and will start the app like this:

    $ ./backend-server
    

    If it's not executable or has permission denied issue, add the executable permission to it.

    $ chmod +x backend-server
    

    You binary should be ready to run. I like to do it with a json config file (process.json) so that I can pass extra env variables as well and don't have to type a lot in terminal. My process.json looks something like this:

    {
      "apps" : [{
        "name"        : "backend-app",
        "script"      : "./backend-server",
        "env": {
          "DB_USER": "db_user",
          "PORT": 8080
        }
      }]
    }
    

    Finally you can start the app using pm2 like this:

    $ pm2 start process.json
    

    More details about json config can be found in official doc

    打赏 评论

相关推荐 更多相似问题