首页 > 代码库 > apache:添加cgi模式

apache:添加cgi模式

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

实验1:

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

go代码:

package mainimport "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= 5i= 1 ;j= 4i= 2 ;j= 3i= 0 ;j= 5i= 1 ;j= 5i= 2 ;j= 5i= 3 ;j= 5i= 4 ;j= 5i= 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,期望能够输出些内容。

实际输出:

ForbiddenYou 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 = ApacheSERVER_NAME = api.query.soft.360.cnGATEWAY_INTERFACE = CGI/1.1SERVER_PROTOCOL = HTTP/1.1SERVER_PORT = 80REQUEST_METHOD = GETHTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8PATH_INFO = PATH_TRANSLATED = SCRIPT_NAME = /test-cgiQUERY_STRING = REMOTE_HOST =REMOTE_ADDR = 10.18.32.50REMOTE_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

apache:添加cgi模式