Go web应用程序从服务器角度如何运作?

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

How do Go web apps function from a server perspective?

问题

我按照使用Go创建Web应用程序的指南进行操作,并成功使应用程序运行良好。

但有一件事让我困惑,当你运行应用程序(./8.out)时,终端会一直监听8080端口,直到有人访问页面。

终端需要一直保持运行状态来运行Web应用程序吗?应用程序是否像Apache一样运行?是否需要在此应用程序旁边运行Apache?目前,我对在服务器环境中设置这个应用程序感到非常困惑,因为我不明白最佳的操作方式是什么。

编辑
谢谢回复。所以,如果Go应用程序本质上像Apache一样运行,是否有一个预先制作好的Go服务器应用程序,具有类似Apache的详细信息?

英文:

I followed the directions on how to <a href="http://golang.org/doc/codelab/wiki/">create web applications</a> using Go, and I was able to get an application working great.

One thing I am confused about though is, when you run the application (./8.out), the terminal will sit there and listen on port 8080 until somebody accesses a page.

Does the terminal need to stay up all of the time to run the web application? Does the application act like apache? Does apache need to be run next to this app? Setting this up on a server environment seems very confusing to me right now because I dont understand what the best way to do this is.

EDITED<br />
Thanks for the replies. So if the Go app essentially acts like apache, is there a premade Go server app that has the verboseness of apache?

答案1

得分: 8

终端需要一直保持开启才能运行Web应用程序吗?

如果你从终端正常运行它,那么是的。更好的选择是通过在命令行末尾添加"&"来在后台运行它(可以),从init启动它(更好),或者使用像supervise这样的进程监视器(最好)。

这个应用程序是否像Apache一样工作?

基本上是的。它监听HTTP请求并对其进行响应。

Apache需要与此应用程序一起运行吗?

不需要,Go应用程序可以自己处理请求。有些人在前端运行Apache或其他服务器(监听端口80),然后使用mod_proxy将请求转发到他们的应用程序(监听端口8080或其他端口)。

这样做的一个优点是,您可以在它们自己的进程中运行多个不同的应用程序服务器。例如,您可以在主站点上使用Ruby on Rails,并使用Go程序处理API请求。

另一个优点是,您的程序不需要以root身份启动以监听端口80。您可以以普通用户身份运行,而无需担心在打开连接后降低权限。

是否有一个预制的Go服务器应用程序具有与Apache类似的冗长性?

据我所知,目前没有与Apache相媲美的Go服务器。Go还比较新,可能还需要一段时间才会有类似的东西。

不过,有一些框架可以使用内置的HTTP服务器更轻松地编写Web应用程序。我熟悉的唯一一个是web.go

英文:

Does the terminal need to stay up all of the time to run the web application?

If you run it normally from a terminal, then yes. Better options are to run it in the background by adding a "&" to the end of the command line (okay), start it from init (better), or use a process monitor like supervise (best).

Does the application act like apache?

Essentially, yes. It listens for HTTP requests and responds to them.

Does apache need to be run next to this app?

No, the Go application can handle the requests by itself. Some people run apache or some other server on the front end (listening to port 80) and forward requests to their application (listening on port 8080 or some other port) using mod_proxy.

One advantage to doing that are that you can run several different application servers in their own process. For example, you could use Ruby on Rails for the main site, and handle API requests with a Go program.

Another advantage is that your program doesn't need to be started as root to listen to port 80. You can just run as a normal user without worrying about dropping privileges after you open the connection.

Is there a premade Go server app that has the verboseness of apache?

As far as I know, there are no go servers that would compare to Apache. Go is new enough that it will probably be awhile for something like that.

There are some frameworks that makes it easier to write web applications using the built-in HTTP server though. The only one I'm familiar with is web.go.

答案2

得分: 4

我对一件事情感到困惑,就是当你运行应用程序(./8.out)时,终端会一直在8080端口上监听,直到有人访问页面。

在Linux中,每个进程都有一个父进程。当你在终端中运行命令,比如./8.out或者find,一个新的进程会被创建,它是shell进程的子进程。当命令行末尾没有加上&amp;时,shell会等待进程完成后才允许你输入更多的文本。如果加上了&amp;,则可以在进程在后台运行的同时继续使用shell。然而,当你关闭终端时,你会注意到服务器进程也会关闭。这是因为当你终止一个父进程时,它的所有子进程也会被终止。

终端需要一直保持开启来运行Web应用程序吗?

如果你希望进程在没有终端的情况下运行,你可能不希望这样,但如果你真的希望:

jobs
disown %jobid

第一个命令会给出shell的子进程列表,第二个命令将%jobid的父进程更改为init。init是所有进程的父进程。

这个应用程序是否像Apache一样工作?

Apache和这个应用程序都监听操作系统提供的端口,但除此之外它们是不同的。

Apache需要与这个应用程序一起运行吗?

不需要,这个应用程序完全独立于Apache运行。Apache并不是什么神奇的东西,在基本层面上,它只是在监听一个端口,就像这个应用程序一样。

在服务器环境中设置这个应用程序对我来说似乎非常困惑,因为我不知道最好的方法是什么。

这应该是令人困惑的。编写Web服务器/服务非常困难,这个例子只是在localhost上部署的示例。

谢谢回复。所以如果Go应用程序本质上像Apache一样工作,是否有一个预先制作的Go服务器应用程序具有与Apache类似的详细程度?

据我所知,目前没有用Go编写的生产质量的服务器,因为Go是一门非常新的语言。

英文:

One thing I am confused about though is, when you run the application (./8.out), the terminal will sit there and listen on port 8080 until somebody accesses a page.

In Linux, every process has a parent. When you run a command in a terminal, such as ./8.out, or find, a new process is spawned that is the child of the shells process. When it is run without the ampersand at the end of the line (./8.out &amp;), the shell waits for the process to complete before it lets you input further text. If it is run with the ampersand, it lets you continue to work with the shell while the process runs in the background. However, when you close the shell, you will notice that the server process also shuts down. This is because when you terminate a parent, all of its children are also terminated.

Does the terminal need to stay up all of the time to run the web application?

If you want the process to run without a terminal, and you probably don't want this, but if you really did:

jobs
disown %jobid

The first command gives you a list of the child processes of the shell, the second command changes the parent process of %jobid to init. Init is the process that is the parent of all processes.

Does the application act like apache?

Both apache and this application listen to a port provided by the operating system, but beyond that they are dissimilar.

Does apache need to be run next to this app?

No, this application works entirely separate from Apache. Apache is in no way magical (?), on a basic level it is just listening on a port, just like this app does.

Setting this up on a server environment seems very confusing to me right now because I dont understand what the best way to do this is.

It should seem confusing. Writing web servers/services is very hard, and this is by no means an example that is meant to be deployed beyond localhost.

Thanks for the replies. So if the Go app essentially acts like apache, is there a premade ?Go server app that has the verboseness of apache?

There are no production quality servers written in Go that I am aware of, at this point, Go is a very new language.

答案3

得分: 3

我认为你的问题是“运行Go Web应用程序的好方法是什么?”

在终端中运行它是可以的,只要你保持终端会话的持久性。像screen这样的程序可以做到这一点。

另一种方法是利用操作系统的启动脚本。例如,大多数Linux系统允许您将脚本添加到/etc/init.d中,以启动Web应用程序。这就是apache的启动方式。

在我看来,最好的方法是使用像monitdaemontools这样的软件。在一些初始配置之后,它们会不断监视Web应用程序,并在应用程序关闭时重新启动。这是大多数生产环境的运行方式。

如果你想在同一台服务器上运行多个Go Web应用程序,你可以使用apache或lighttpd作为网关,并使用像fastcgi或scgi这样的协议来提供请求。

英文:

I think your question is what is a good way to run a Go web application?

Running it in the terminal is fine, as long as you persist your terminal sessions. Programs like screen can do this.

Another way is to leverage your operating system's startup scripts. Most Linux systems, for instance, let you add scripts to /etc/init.d which spawn the web application. This is how apache starts.

The best way, in my opinion, is to use software like monit or daemontools. After some initial configuration, these constantly monitor the web application, and restarts the application if it goes down. This is how most production environments operate.

If you want to run multiple Go web applications on the same server, you could use apache or lighttpd as the gateway, and use a protocol like fastcgi or scgi to serve requests.

huangapple
  • 本文由 发表于 2010年5月19日 02:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/2859862.html
匿名

发表评论

匿名网友

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

确定