apache:添加cgi模式

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

最終期望:通過配置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 don‘t 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模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.