Common Gateway Interface如雷贯耳,遗憾的是一直以来都没玩过CGI,今天尝试一把。Tomcat可以是玩CGI的,但得改下配置。为了方便,直接使用一款更轻量级的web服务器lighttpd来跑,。

  先把lighttpd安装一下:直接使用linux的包管理器,先安装一个epel软件仓库

  1. yum install epel-release

    从这个仓库里拿到lighttpd并安装

  1. yum install lighttpd

  安装好web服务器后就可以来写CGI代码了,可以用各种语言来写,这里选择C。C编译后本身就是可执行文件,所以我们得把CGI的配置改一改:

  1. [root@iZbp11ahvmlfioymoo7u3bZ ~]# vi /etc/lighttpd/conf.d/cgi.conf
  2.  
  3. #######################################################################
  4. ##
  5. ## CGI modules
  6. ## ---------------
  7. ##
  8. ## See https://redmine.lighttpd.net/projects/lighttpd/wiki/docs_modcgi
  9. ##
  10. server.modules += ( "mod_cgi" )
  11.  
  12. ##
  13. ## Plain old CGI handling
  14. ##
  15. ## For PHP don't forget to set cgi.fix_pathinfo = 1 in the php.ini.
  16. ##
  17. cgi.assign = ( ".pl" => "/usr/bin/perl",
  18. ".cgi" => "/usr/bin/perl",
  19. ".rb" => "/usr/bin/ruby",
  20. ".erb" => "/usr/bin/eruby",
  21. ".py" => "/usr/bin/python" )
  22.  
  23. ##
  24. ## to get the old cgi-bin behavior of apache
  25. ##
  26. ## Note: make sure that mod_alias is loaded if you uncomment the
  27. ## next line. (see modules.conf)
  28. ##
  29. #alias.url += ( "/cgi-bin" => server_root + "/cgi-bin" )
  30. #$HTTP["url"] =~ "^/cgi-bin" {
  31. # cgi.assign = ( "" => "" )
  32. #}
  33.  
  34. ##
  35. #######################################################################

  这里将.cgi改为

  1. ".cgi" => "",

  以上表示指定解析程序为空,这样对于带扩展名为.cgi的请求,不需要特定解析程序(比如用/bin/sh/perl)就能执行CGI。接着到/var/www/lighttpd目录,用C写一个简单例子

  

  1. // A "hello world" page
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define INCR_LEN 10
  6.  
  7. char *getValue(char *, char **);// get the param value
  8. char **getParameters(char *, char **);// get the array of params and values
  9. int main(void)
  10. {
  11. char **pQuery;
  12. char *pParam;
  13. char *name;
  14. char *city;
  15. pParam = getenv("QUERY_STRING");
  16. char *pStr = malloc(strlen(pParam)+);
  17. memcpy(pStr, pParam, strlen(pParam)+);
  18. pQuery = getParameters(pStr, pQuery);
  19. name = getValue("name", pQuery);
  20. city = getValue("city", pQuery);
  21.  
  22. printf("Content-Type:text/html\n\n");// print html
  23. puts("<html>");
  24. puts("<head><title>An HTML Page From a CGI</title></head>");
  25. puts("<body><br>");
  26. puts("<p><h2>Hello world!</h2></p>");
  27. printf("<p>Your name is :%s</p>\n", name);
  28. printf("<p>Your city is :%s</p>\n", city);
  29. puts("</body>");
  30. puts("</html>");
  31.  
  32. return ;
  33. }
  34.  
  35. char **getParameters(char *pTemp, char **pQuery)
  36. {
  37. char **pArrayTemp = NULL;
  38. pQuery = calloc(INCR_LEN, sizeof(char *));
  39. char *pParamIndex = NULL;
  40. int i = ;
  41. int count_max = INCR_LEN;
  42.  
  43. while((pParamIndex = strchr(pTemp,'&')) != NULL)
  44. {
  45. if(i == count_max) // array reach max(10) need more memory
  46. {
  47. count_max += INCR_LEN;
  48. pArrayTemp = realloc(pQuery, count_max*sizeof(char*));
  49. if(!pArrayTemp)
  50. {
  51. exit();
  52. }
  53. pQuery = pArrayTemp;
  54. }
  55.  
  56. *(pQuery+i) = malloc(pParamIndex - pTemp + );
  57. strncpy(*(pQuery+i), pTemp, pParamIndex - pTemp);
  58. strncpy(pTemp, pParamIndex+, strlen(pTemp) - (pParamIndex-pTemp) + );
  59. i++;
  60. }
  61. *(pQuery+i) = malloc(strlen(pTemp) + );
  62. strncpy(*(pQuery+i), pTemp, strlen(pTemp)+);
  63. return pQuery;
  64. }
  65.  
  66. char *getValue(char *pParameter, char **pParamValues)
  67. {
  68. char *pValue = NULL;
  69. for(; *pParamValues!=NULL; pParamValues++)
  70. {
  71. if(strstr(*pParamValues, pParameter))
  72. {
  73. pValue = strchr(*pParamValues, '=');
  74. if(pValue)
  75. return pValue+;
  76. }
  77. }
  78.  
  79. return NULL;
  80. }

  把上面的C编译一下:

  1. gcc hello.c -o hello.cgi

  最后把端口号由80改为其他端口号如8089,避免启动时与原有端口冲突。

  1. vi /etc/lighttpd/lighttpd.conf

  找到server.port后,将80改为8089。接着启动lighttpd服务器:

  1. systemctl start lighttpd

  再看下是否已经启起来了:

  1. systemctl status lighttpd
  2. lighttpd.service - Lightning Fast Webserver With Light System Requirements
  3. Loaded: loaded (/usr/lib/systemd/system/lighttpd.service; disabled; vendor preset: disabled)
  4. Active: active (running) since Thu -- :: CST; 2s ago
  5. Main PID: (lighttpd)
  6. CGroup: /system.slice/lighttpd.service
  7. └─ /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf
  8.  
  9. Apr :: iZbp11ahvmlfioywlf7u3bZ systemd[]: Started Lightning Fast Webserver With Light System Requirements.
  10. Apr :: iZbp11ahvmlfioywlf7u3bZ lighttpd[]: -- ::: (network.c.) warning: please use server.use-ipv6 only for hostnames, not wi...Y changes
  11. Apr :: iZbp11ahvmlfioywlf7u3bZ lighttpd[]: -- ::: (server.c.) 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了

linux上通过lighttpd上跑一个C语言的CGI小页面以及所遇到的坑的更多相关文章

  1. 在K8S上跑一个helloworld

    建立docker镜像 为了方便起见,这里直接使用一个js网页作为应用,以此创建镜像 hello world网页 创建server.js,输入以下代码创建helloworld网页: var http = ...

  2. 安装了linux系统的设备上不了网怎么办

    玩了一阵子的树莓派,曾经计划将其作成一台小小无线路由,但是时间和精力关系始终未成功做成. 同时也有在进行一些arm开发板的学习,突然一天发现arm板直接插上网线不能是不能上网的,又想起之前玩树莓派的时 ...

  3. 把Linux安装到移动硬盘上

    把Linux安装到移动硬盘上 转载于:http://mrkh.me/install-linux-on-a-portable-hard-drive.html 这一篇文章讲一下,怎么把linux安装到移动 ...

  4. Linux 在一个命令行上执行多个命令

    Linux 在一个命令行上执行多个命令 1. [ ; ] 如果被分号(;)所分隔的命令会连续的执行下去,就算是错误的命令也会继续执行后面的命令. 2. [ && ] 如果命令被 &am ...

  5. 在 Linux 的 KVM虚拟机 上安装 Mac OS 系统的研究总结

    在 Linux 的 KVM虚拟机 上安装 Mac OS 系统的研究总结 一.资料来源:    网上一共找到两个方法,一个是视频上的教程,一个是网页资料. 二.视频资料方法内容:1.install qe ...

  6. 将Windows上的文件上传到Linux上

    下载一个SSH Secure Shell Client即可. SSHSecureShellClient-3.2.9下载地址: 免费下载地址在 http://linux.linuxidc.com/ 用户 ...

  7. 在openwrt上编译最简单的一个ipk包文件

    1 什么是opkg Opkg 是一个轻量快速的套件管理系统,目前已成为 Opensource 界嵌入式系统标准.常用于路由.交换机等嵌入式设备中,用来管理软件包的安装升级与下载. opkg updat ...

  8. linux的tomcat服务器上部署项目的方法

    在tomcat服务器上部署项目的前提,是我们已经准备好了tomcat服务器.在CentOs环境下部署JavaWeb环境,部署tomcat服务器在前面的文章中已经总结过了,可以参考以前文章. 一  to ...

  9. java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息

    1.java使用Jsch实现远程操作linux服务器进行文件上传.下载,删除和显示目录信息. 参考链接:https://www.cnblogs.com/longyg/archive/2012/06/2 ...

随机推荐

  1. Jquery中bind(), live(), on(), delegate()四种注册事件的优缺点,建议使用on()

    jquery中注册的事件,注册事件很容易理解偏差,叫法不一样.我第一反应就是如何添加事件,使用onclick之类的,暂时不讨论js注册事件的方法. 也看到园内前辈写过相关的帖子,但不是很详细,我找到了 ...

  2. Windows 7 英文版操作系统中文软件乱码解决方法

    http://blog.csdn.net/lqhbupt/article/details/18863243

  3. L208

    A hundred years ago it was assumed and scientifically “proved” by economists that the laws of societ ...

  4. java算法大全

    题一: /** * 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,  * 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少对? * 分析:  * 第 ...

  5. 背景 半透明问题 rgba + filter

    <html style=" background: violet;"><head><meta charset="utf-8"> ...

  6. MySQL主从数据一致性检验

    MySQL主从数据一致性检验 检查主从数据一致性,我们使用pt-table-checksum ,pt-table-checksum是percona-tools一个工具,用来校验主从库数据是不是一致. ...

  7. 【转载】Java Web的web.xml文件作用及基本配置

    其实web.xml就是asp.net的web.config一个道理. 说明: 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. web.xml文件是用来 ...

  8. TypeError: HashUpdate fail

    关于crypto的md5加密报错: 代码: var crypto = require('crypto'); var md5 = crypto.createHash('md5'); //crypto模块 ...

  9. ZOJ 3211dream city dp(效率优化)

    Dream City Time Limit: 1 Second      Memory Limit:32768 KB JAVAMAN is visiting Dream City and he see ...

  10. Codeforces gym101955 A【树形dp】

    LINK 有n个大号和m个小号 然后需要对这些号进行匹配,一个大号最多匹配2个小号 匹配条件是大号和小号构成了前缀关系 字符串长度不超过10 问方案数 思路 因为要构成前缀关系 所以就考虑在trie树 ...