最終指望:經過配置apache的cgi可以使得apache能經過cgi方式鏈接go程序(由於咱們的後端程序是用go語言寫的)。html
指望:經過配置cgi使得應用程序可以跑起來。web
go代碼:apache
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後端
如今就準備好了應用程序。api
apache配置:app
<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/ 可以輸出些東西。ide
輸出:ui
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成功。this
在apache的bin-cgi目錄下已經有test-cgi應用程序,咱們指望可以經過實驗1的方式順利引入進來。spa
apache配置:
<VirtualHost *:80> ServerName api.query.soft.111.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 *:80> ServerName api.query.soft.111.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