apache:添加cgi模式

时间:2024-01-06 08:44:32

最终期望:通过配置apache的cgi能够使得apache能通过cgi方式连接go程序(因为我们的后端程序是用go语言写的)。

实验1:

期望:通过配置cgi使得应用程序能够跑起来。

go代码:

package main

import "fmt"

func main() {
fmt.Println("Content-type: text/plain;\n")
for i, j := , ; i <= j; i, j = i+, j- { //right
fmt.Println("i=", i, ";j=", j)
} for i, j := , ; i <= j; i++ { //right
fmt.Println("i=", i, ";j=", j)
} //for i,j := 0,5; i <= j; i++, j-- { //wrong
// fmt.Println("i=",i,";j=",j)
//}
}

Go编译:/usr/local/go/bin/go build -o /home/luwenwei/go/base/main /home/luwenwei/go/base/for.go

现在就准备好了应用程序。

apache配置:

<VirtualHost *:80>
ServerName api.query.soft.360.cn ScriptAlias / /home/luwenwei/go/base/main
#ScriptAlias /test-cgi /usr/local/apache2/cgi-bin/test-cgi <Directory "/usr/local/apache2/cgi-bin/">
Options -Indexes FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory> ErrorLog logs/error_lww
CustomLog "|/usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/lww_access_log.%Y%m%d 86400 480" combinedio
</VirtualHost>

期望访问 http://api.query.soft.111.cn/ 能够输出些东西。

输出:

i=  ;j=
i= ;j=
i= ;j=
i= ;j=
i= ;j=
i= ;j=
i= ;j=
i= ;j=
i= ;j=

结果符合预期,实验1成功。

实验2:

在apache的bin-cgi目录下已经有test-cgi应用程序,我们期望能够通过实验1的方式顺利引入进来。

apache配置:

<VirtualHost *:>
ServerName api.query.soft..cn #ScriptAlias / /home/luwenwei/go/base/main
ScriptAlias /test-cgi /usr/local/apache2/cgi-bin/test-cgi ErrorLog logs/error_lww
CustomLog "|/usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/lww_access_log.%Y%m%d 86400 480" combinedio
</VirtualHost>

访问http://api.query.soft.111.cn/test-cgi,期望能够输出些内容。

实际输出:

Forbidden

You don't have permission to access /test-cgi on this server.

实际不符合预期。

正确的配置为:

<VirtualHost *:>
ServerName api.query.soft..cn #ScriptAlias / /home/luwenwei/go/base/main
ScriptAlias /test-cgi /usr/local/apache2/cgi-bin/test-cgi <Directory "/usr/local/apache2/cgi-bin/">
Options -Indexes FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory> ErrorLog logs/error_lww
CustomLog "|/usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/lww_access_log.%Y%m%d 86400 480" combinedio
</VirtualHost>

配置好FollowSymLinks后可正常访问。页面输出为:

CGI/1.0 test script report:

argc is 0. argv is .

SERVER_SOFTWARE = Apache
SERVER_NAME = api.query.soft.111.cn
GATEWAY_INTERFACE = CGI/1.1
SERVER_PROTOCOL = HTTP/1.1
SERVER_PORT = 80
REQUEST_METHOD = GET
HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
PATH_INFO =
PATH_TRANSLATED =
SCRIPT_NAME = /test-cgi
QUERY_STRING =
REMOTE_HOST =
REMOTE_ADDR = 10.18.32.50
REMOTE_USER =
AUTH_TYPE =
CONTENT_TYPE =
CONTENT_LENGTH =

参考文献

http://blog.csdn.net/xiaojianpitt/article/details/5785925

http://blog.csdn.net/wangfeng2500/article/details/8119959

http://blog.sina.com.cn/s/blog_49c5f8100100ufd6.html

http://www.jb51.net/article/18230.htm

http://man.chinaunix.net/newsoft/ApacheManual/howto/cgi.html