apache:添加cgi模式

最终期望:通过配置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 := 0, 5; i <= j; i, j = i+1, j-1 { //right
        fmt.Println("i=", i, ";j=", j)
    }   

    for i, j := 0, 5; 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.360.cn/ 能够输出些东西。

输出:

i= 0 ;j= 5
i= 1 ;j= 4
i= 2 ;j= 3
i= 0 ;j= 5
i= 1 ;j= 5
i= 2 ;j= 5
i= 3 ;j= 5
i= 4 ;j= 5
i= 5 ;j= 5

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

实验2:

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

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

    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.360.cn/test-cgi,期望能够输出些内容。

实际输出:

Forbidden

You dont have permission to access /test-cgi on this server.

实际不符合预期。

正确的配置为:

<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>

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

CGI/1.0 test script report:

argc is 0. argv is .

SERVER_SOFTWARE = Apache
SERVER_NAME = api.query.soft.360.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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。