zoukankan      html  css  js  c++  java
  • 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 10
    
    char *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
    
    ");// print html
            puts("<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>
    ", name);
            printf("<p>Your city is :%s</p>
    ", 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 Requirements
       Loaded: loaded (/usr/lib/systemd/system/lighttpd.service; disabled; vendor preset: disabled)
       Active: active (running) since Thu 2019-04-18 14:18:04 CST; 2s ago
     Main PID: 30237 (lighttpd)
       CGroup: /system.slice/lighttpd.service
               └─30237 /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf
    
    Apr 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了

  • 相关阅读:
    纯CSS绘制三角形(各种角度)
    富文本编辑器Quill(二)上传图片与视频
    富文本编辑器Quill(一)简单介绍
    Vue中使用Cropper.js裁剪图片
    Python使用LDAP做用户认证
    concurrent.futures进行并发编程
    Selenium及Headless Chrome抓取动态HTML页面
    七夕节快到了,做个图钉画以及学习下Pillow吧
    Python socket
    Python爬取视频(其实是一篇福利)
  • 原文地址:https://www.cnblogs.com/wuxun1997/p/10730811.html
Copyright © 2011-2022 走看看