Building and run go scripts on Apache under fcgi

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

Building and run go scripts on Apache under fcgi

问题

运行每个脚本的步骤如下:

go build script.go
mv script script.fcgi

我的Apache配置如下:

<VirtualHost [我的IP]:80>
    ServerAdmin webmaster@example.com
    ServerName website.com
    DocumentRoot /home/user/www
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /our_bin [QSA,L]
    <Directory /home/user/www>
        Allow from all
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d [OR]
        RewriteCond %{REQUEST_URI} ^/$
        RewriteRule ^(.*)$ script.fcgi/$1 [QSA,L]
    </Directory>
</VirtualHost>

问题:
1)如果我构建一个脚本,它会将我链接的所有包都构建进去,对吗?
2)我如何自定义fcgi和go,以便不需要每次都进行构建?

抱歉我的英语不好。

英文:

For run every script i do:

go build script.go
mv script script.fcgi

My apache config looks so:

&lt;VirtualHost [myip]:80&gt;
    ServerAdmin webmaster@example.com
    ServerName website.com
    DocumentRoot /home/user/www
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /our_bin [QSA,L]
    &lt;Directory /home/user/www&gt;
        Allow from all
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d [OR]
        RewriteCond %{REQUEST_URI} ^/$
        RewriteRule ^(.*)$ script.fcgi/$1 [QSA,L]
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;

Question:

  1. If i build 1 script, it will builded with all packages that i link, right?
  2. How i can customize fcgi and go so that it will not need build every time

sorry for bad english

答案1

得分: 2

你无法直接在Apache中运行Go应用程序,因为Go不是一种“脚本语言”,Apache也不知道如何处理它(与PHP FCGI和其他变体不同)。

你需要使用一个HTTP或FCGI服务器来构建(编译)你的Go应用程序,并运行它,然后使用Apache(或nginx)来反向代理到你的Go应用程序监听的HTTP端口/FCGI套接字。

可以参考一下net/http文档和Go文档中的simple web application教程。根据我的经验,我建议使用反向HTTP代理而不是FCGI,因为它更容易调试和配置。

例如:

<VirtualHost myhost:80>
                ServerName www.mydomain.com
                ProxyPass / http://localhost:8000/ # 你的Go应用程序监听的端口
                ProxyPassReverse / http://localhost:8000/
</VirtualHost>

请注意,这只是一个示例,没有经过测试,但希望能帮助你入门。

英文:

You can't. Go isn't a "scripting language" and Apache doesn't know how to process it (unlike PHP FCGI and variants).

You need to build (compile) your Go application with a HTTP or FCGI server, and run it, and then use Apache (or nginx) to reverse proxy to the HTTP port/FCGI socket your Go app is listening on.

Take a look at the net/http documentation and the simple web application tutorial in the Go documentation. From my experience I'd recommend using a reverse HTTP proxy over FCGI as it's easier to debug/configure.

i.e.

&lt;VirtualHost myhost:80&gt;
                ServerName www.mydomain.com
                ProxyPass / http://localhost:8000/ # Your Go app&#39;s listening port
                ProxyPassReverse / http://localhost:8000/
&lt;/VirtualHost&gt;

Note that this isn't tested nor a complete example, but should hopefully get you started.

huangapple
  • 本文由 发表于 2014年3月1日 11:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/22109629.html
匿名

发表评论

匿名网友

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

确定