linux上通过lighttpd上跑一个C语言的CGI小页面以及所遇到的坑
Common Gateway Interface如雷貫耳,遺憾的是一直以來都沒玩過CGI,今天嘗試一把。Tomcat可以是玩CGI的,但得改下配置。為了方便,直接使用一款更輕量級的web服務器lighttpd來跑,。
先把lighttpd安裝一下:直接使用linux的包管理器,先安裝一個epel軟件倉庫
yum install epel-release? 從這個倉庫里拿到lighttpd并安裝
yum install lighttpd安裝好web服務器后就可以來寫CGI代碼了,可以用各種語言來寫,這里選擇C。C編譯后本身就是可執行文件,所以我們得把CGI的配置改一改:
[root@iZbp11ahvmlfioymoo7u3bZ ~]# vi /etc/lighttpd/conf.d/cgi.conf ####################################################################### ## ## CGI modules ## --------------- ## ## See https://redmine.lighttpd.net/projects/lighttpd/wiki/docs_modcgi ## server.modules += ( "mod_cgi" )## ## Plain old CGI handling ## ## For PHP don't forget to set cgi.fix_pathinfo = 1 in the php.ini. ## cgi.assign = ( ".pl" => "/usr/bin/perl",".cgi" => "/usr/bin/perl",".rb" => "/usr/bin/ruby",".erb" => "/usr/bin/eruby",".py" => "/usr/bin/python" )## ## to get the old cgi-bin behavior of apache ## ## Note: make sure that mod_alias is loaded if you uncomment the ## next line. (see modules.conf) ## #alias.url += ( "/cgi-bin" => server_root + "/cgi-bin" ) #$HTTP["url"] =~ "^/cgi-bin" { # cgi.assign = ( "" => "" ) #}## #######################################################################這里將.cgi改為
".cgi" => "",以上表示指定解析程序為空,這樣對于帶擴展名為.cgi的請求,不需要特定解析程序(比如用/bin/sh/perl)就能執行CGI。接著到/var/www/lighttpd目錄,用C寫一個簡單例子
// A "hello world" page #include <stdio.h> #include <stdlib.h> #include <string.h> #define INCR_LEN 10char *getValue(char *, char **);// get the param value char **getParameters(char *, char **);// get the array of params and values int main(void) {char **pQuery;char *pParam;char *name;char *city;pParam = getenv("QUERY_STRING");char *pStr = malloc(strlen(pParam)+1);memcpy(pStr, pParam, strlen(pParam)+1);pQuery = getParameters(pStr, pQuery);name = getValue("name", pQuery);city = getValue("city", pQuery);printf("Content-Type:text/html\n\n");// print htmlputs("<html>");puts("<head><title>An HTML Page From a CGI</title></head>");puts("<body><br>");puts("<p><h2>Hello world!</h2></p>");printf("<p>Your name is :%s</p>\n", name);printf("<p>Your city is :%s</p>\n", city);puts("</body>");puts("</html>");return 0; }char **getParameters(char *pTemp, char **pQuery) {char **pArrayTemp = NULL;pQuery = calloc(INCR_LEN, sizeof(char *));char *pParamIndex = NULL;int i = 0;int count_max = INCR_LEN;while((pParamIndex = strchr(pTemp,'&')) != NULL){if(i == count_max) // array reach max(10) need more memory {count_max += INCR_LEN;pArrayTemp = realloc(pQuery, count_max*sizeof(char*));if(!pArrayTemp){exit(1);}pQuery = pArrayTemp;}*(pQuery+i) = malloc(pParamIndex - pTemp + 1);strncpy(*(pQuery+i), pTemp, pParamIndex - pTemp);strncpy(pTemp, pParamIndex+1, strlen(pTemp) - (pParamIndex-pTemp) + 1);i++;}*(pQuery+i) = malloc(strlen(pTemp) + 1);strncpy(*(pQuery+i), pTemp, strlen(pTemp)+1);return pQuery; }char *getValue(char *pParameter, char **pParamValues) {char *pValue = NULL;for(; *pParamValues!=NULL; pParamValues++){if(strstr(*pParamValues, pParameter)){pValue = strchr(*pParamValues, '=');if(pValue)return pValue+1;}}return NULL; }
把上面的C編譯一下:
gcc hello.c -o hello.cgi最后把端口號由80改為其他端口號如8089,避免啟動時與原有端口沖突。
vi /etc/lighttpd/lighttpd.conf找到server.port后,將80改為8089。接著啟動lighttpd服務器:
systemctl start lighttpd再看下是否已經啟起來了:
systemctl status lighttpd ● lighttpd.service - Lightning Fast Webserver With Light System RequirementsLoaded: loaded (/usr/lib/systemd/system/lighttpd.service; disabled; vendor preset: disabled)Active: active (running) since Thu 2019-04-18 14:18:04 CST; 2s agoMain PID: 30237 (lighttpd)CGroup: /system.slice/lighttpd.service└─30237 /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.confApr 18 14:18:04 iZbp11ahvmlfioywlf7u3bZ systemd[1]: Started Lightning Fast Webserver With Light System Requirements. Apr 18 14:18:04 iZbp11ahvmlfioywlf7u3bZ lighttpd[30237]: 2019-04-18 14:18:04: (network.c.166) warning: please use server.use-ipv6 only for hostnames, not wi...Y changes Apr 18 14:18:04 iZbp11ahvmlfioywlf7u3bZ lighttpd[30237]: 2019-04-18 14:18:04: (server.c.1380) can't have more connections than fds/2: 1024 1024我們看到已經啟動成功了,但提示要用ipv6的地址來訪問,但我想用ipv4,所以把它改掉,再次/etc/lighttpd/lighttpd.conf -> 找到server.use-ipv6 = "enable" -> 將enable改為disable ->?systemctl restart lighttpd
然后我們通過ip:8089/index.html訪問lighttpd自帶的歡迎頁:
再去訪問我們編譯好的hello.cgi,發現頁面可以訪問,卻啥都沒有,瀏覽器把頁面直接下載了而不是渲染出來。咋回事呢?原來這時候的cgi文件瀏覽器是無法解析的,必須要去modules.conf里打開cgi:vi /etc/lighttpd/modules.conf -> 找到 #include "conf.d/cgi.conf" -> 將前面的#號刪掉 -> 重啟lighttpd?systemctl restart lighttpd
再次訪問我們的hello.cgi,這次ok了
?
轉載于:https://www.cnblogs.com/wuxun1997/p/10730811.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的linux上通过lighttpd上跑一个C语言的CGI小页面以及所遇到的坑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Metro Win8风格的按钮(Filp
- 下一篇: ROS多根adsl叠加负载均衡PCC的做