Linux下嵌入式Web伺服器BOA和CGI編程開發

来源:https://www.cnblogs.com/ikaros-521/archive/2019/10/18/11698520.html
-Advertisement-
Play Games

**目錄**一、環境搭建二、相關配置(部分)三、調試運行四、測試源碼參考五、常見錯誤六、擴展(CCGI,SQLite) # 一、環境搭建操作系統:Ubuntu12.04 LTSboa下載地址(但是我找不到...): http://www.boa.org/我是其他網站找到的資源,但是忘了網址了,所以我 ...




**目錄**
一、環境搭建
二、相關配置(部分)
三、調試運行
四、測試源碼參考
五、常見錯誤
六、擴展(CCGI,SQLite)



# 一、環境搭建
操作系統:Ubuntu12.04 LTS
boa下載地址(但是我找不到...): http://www.boa.org/
我是其他網站找到的資源,但是忘了網址了,所以我直接上雲盤資源
鏈接: https://pan.baidu.com/s/1_SpR9MDcmSB8jpDm76fw6A 提取碼: hb1e
可以參考:Ubuntu下boa伺服器的配置與搭建
cgi:直接終端安裝 sudo apt-get install apache2
可以參考: ubuntu 下搭建cgi環境

# 二、相關配置(部分)
boa我的配置:/etc/boa$ sudo vi boa.conf

# 下麵幾個都是關鍵點,基本就錯這幾個點上
# cumentRoot /var/www
#將cgi保存的實際位置和網站地址做個對應
# ScriptAlias /cgi-bin/ /var/www/cgi-bin/
#cgi腳本運行時能看到的$PATH(可選)
# CGIPath /bin:/usr/bin:/usr/local/bin
#如果想在任何位置都能運行cgi,要添加這個(可選)
AddType application/x-httpd-cgi cgi
# Boa v0.94 configuration file
# File format has not changed from 0.93
# File format has changed little from 0.92
# version changes are noted in the comments
#
# The Boa configuration file is parsed with a lex/yacc or flex/bison
# generated parser. If it reports an error, the line number will be
# provided; it should be easy to spot. The syntax of each of these
# rules is very simple, and they can occur in any order. Where possible
# these directives mimic those of NCSA httpd 1.3; I saw no reason to 
# introduce gratuitous differences.

# $Id: boa.conf,v 1.25 2002/03/22 04:33:09 jnelson Exp $
# The "ServerRoot" is not in this configuration file. It can be compiled
# into the server (see defines.h) or specified on the command line with
# the -c option, for example:
#
# boa -c /usr/local/boa

# Port: The port Boa runs on. The default port for http servers is 80.

# If it is less than 1024, the server must be started as root.
# 埠老是被占用,所以我改掉了
Port 88

# Listen: the Internet address to bind(2) to. If you leave it out,
# it takes the behavior before 0.93.17.2, which is to bind to all
# addresses (INADDR_ANY). You only get one "Listen" directive,
# if you want service on multiple IP addresses, you have three choices:
# 1. Run boa without a "Listen" directive
# a. All addresses are treated the same; makes sense if the addresses
# are localhost, ppp, and eth0.
# b. Use the VirtualHost directive below to point requests to different
# files. Should be good for a very large number of addresses (web
# hosting clients).
# 2. Run one copy of boa per IP address, each has its own configuration
# with a "Listen" directive. No big deal up to a few tens of addresses.
# Nice separation between clients.
# The name you provide gets run through inet_aton(3), so you have to use dotted
# quad notation. This configuration is too important to trust some DNS.

#Listen 192.68.0.5
# User: The name or UID the server should run as.
# Group: The group name or GID the server should run as.

User 0 
Group 0

# ServerAdmin: The email address where server problems should be sent.
# Note: this is not currently used, except as an environment variable
# for CGIs.
#ServerAdmin root@localhost
# ErrorLog: The location of the error log file. If this does not start
# with /, it is considered relative to the server root.
# Set to /dev/null if you don't want errors logged.
# If unset, defaults to /dev/stderr

ErrorLog /var/log/boa/error_log

# Please NOTE: Sending the logs to a pipe ('|'), as shown below,
# is somewhat experimental and might fail under heavy load.
# "Usual libc implementations of printf will stall the whole
# process if the receiving end of a pipe stops reading."
#ErrorLog "|/usr/sbin/cronolog --symlink=/var/log/boa/error_log /var/log/boa/error-%Y%m%d.log"

# AccessLog: The location of the access log file. If this does not
# start with /, it is considered relative to the server root.
# Comment out or set to /dev/null (less effective) to disable 
# Access logging.
# AccessLog /var/log/boa/access_log
# Please NOTE: Sending the logs to a pipe ('|'), as shown below,
# is somewhat experimental and might fail under heavy load.
# "Usual libc implementations of printf will stall the whole
# process if the receiving end of a pipe stops reading."
#AccessLog "|/usr/sbin/cronolog --symlink=/var/log/boa/access_log /var/log/boa/access-%Y%m%d.log"

# UseLocaltime: Logical switch. Uncomment to use localtime 
# instead of UTC time
#UseLocaltime

# VerboseCGILogs: this is just a logical switch.
# It simply notes the start and stop times of cgis in the error log
# Comment out to disable.

#VerboseCGILogs

# ServerName: the name of this server that should be sent back to 
# clients if different than that returned by gethostname + gethostbyname

ServerName www.your.org.here

# VirtualHost: a logical switch.
# Comment out to disable.
# Given DocumentRoot /var/www, requests on interface 'A' or IP 'IP-A'
# become /var/www/IP-A.
# Example: http://localhost/ becomes /var/www/127.0.0.1
#
# Not used until version 0.93.17.2. This "feature" also breaks commonlog
# output rules, it prepends the interface number to each access_log line.
# You are expected to fix that problem with a postprocessing script.

#VirtualHost

# DocumentRoot: The root directory of the HTML documents.
# Comment out to disable server non user files.

DocumentRoot /var/www

# UserDir: The name of the directory which is appended onto a user's home
# directory if a ~user request is recieved.

UserDir public_html

# DirectoryIndex: Name of the file to use as a pre-written HTML
# directory index. Please MAKE AND USE THESE FILES. On the
# fly creation of directory indexes can be _slow_.
# Comment out to always use DirectoryMaker

DirectoryIndex index.html

# DirectoryMaker: Name of program used to create a directory listing.
# Comment out to disable directory listings. If both this and
# DirectoryIndex are commented out, accessing a directory will give
# an error (though accessing files in the directory are still ok).

DirectoryMaker /usr/lib/boa/boa_indexer

# DirectoryCache: If DirectoryIndex doesn't exist, and DirectoryMaker
# has been commented out, the the on-the-fly indexing of Boa can be used
# to generate indexes of directories. Be warned that the output is 
# extremely minimal and can cause delays when slow disks are used.
# Note: The DirectoryCache must be writable by the same user/group that 
# Boa runs as.

# DirectoryCache /var/spool/boa/dircache

# KeepAliveMax: Number of KeepAlive requests to allow per connection
# Comment out, or set to 0 to disable keepalive processing

KeepAliveMax 1000

# KeepAliveTimeout: seconds to wait before keepalive connection times out

KeepAliveTimeout 10

# MimeTypes: This is the file that is used to generate mime type pairs
# and Content-Type fields for boa.
# Set to /dev/null if you do not want to load a mime types file.
# Do *not* comment out (better use AddType!)

MimeTypes /etc/mime.types

# DefaultType: MIME type used if the file extension is unknown, or there
# is no file extension.

DefaultType text/plain

# CGIPath: The value of the $PATH environment variable given to CGI progs.

CGIPath /bin:/usr/bin:/usr/local/bin

# SinglePostLimit: The maximum allowable number of bytes in 
# a single POST. Default is normally 1MB.

# AddType: adds types without editing mime.types
# Example: AddType type extension [extension ...]

# Uncomment the next line if you want .cgi files to execute from anywhere
#AddType application/x-httpd-cgi cgi

# Redirect, Alias, and ScriptAlias all have the same semantics -- they
# match the beginning of a request and take appropriate action. Use
# Redirect for other servers, Alias for the same server, and ScriptAlias
# to enable directories for script execution.

# Redirect allows you to tell clients about documents which used to exist in
# your server's namespace, but do not anymore. This allows you to tell the
# clients where to look for the relocated document.
# Example: Redirect /bar http://elsewhere/feh/bar

# Aliases: Aliases one path to another.
# Example: Alias /path1/bar /path2/foo

Alias /doc /usr/doc

# ScriptAlias: Maps a virtual path to a directory for serving scripts
# Example: ScriptAlias /htbin/ /www/htbin/

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
View Code

 

cgi我的配置:sudo vi /etc/apache2/sites-enabled/000-default

ServerName 127.0.0.1 
<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny 
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost> 
View Code

 

# 三、調試運行
    都配好後運行 sudo ./boa
    訪問 http://127.0.0.1:埠號
    我的就是 http://127.0.0.1:88
    gcc -o test.cgi test.c 編譯生成 test.cgi
    把cgi文件拷貝到 cgi-bin 下
    網址就是 http://localhost:88/cgi-bin/test.cgi

 

 

 

 

# 四、測試源碼參考



下麵提供幾個測試代碼,轉自其他大佬,不過我找不到網址了。
1、

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char *data;
    long m,n;
    printf("Content-type:text/html\n\n");
    printf("<HTML>");
    printf("<HEAD>");
    printf("<TITLE>multi</TITLE>");
    printf("</HEAD>");
    printf("<BODY>");
    printf("<H2 ALIGN=\"center\">multi control</H2>");
    printf("<FORM METHOD=\"GET\" ACTION=\"test1.cgi\">");
    printf("<P>Direction:<INPUT TYPE=\"text\" NAME=\"m\" VALUE=\"\" size=\"18\">");
    printf("<P>Step Number:<INPUT TYPE=\"text\" NAME=\"n\" VALUE=\"\" size=\"17\">");
    printf("<P ALIGN=\"left\">");
    printf("<INPUT TYPE=\"SUBMIT\"  VALUE=\"Submit\">");
    printf("<INPUT TYPE=\"RESET\"    VALUE=\"Reset\">");
    printf("</P>");
    printf("</FORM>");
    printf("</BODY>");
    printf("</HTML>");

    data=getenv("QUERY_STRING");
    if(!data)
        printf("<P>get no datas and it's wrong.");
    else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
        printf("<P>the input must be numbers");
    else
        printf("<P>%ld and %ld multi= %ld",m,n,m*n);
    return 0;
}

 

 

 


2、
/var/www 下的pass.html

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用戶登陸驗證</title>
</head>
<body>
<form name="form1" action="/cgi-bin/pass.cgi" method="POST">
<table align="center">
<tr><td align="center" colspan="2"></td></tr>
<tr>
<td align="right">用戶名</td>
<td><input type="text" name="Username"></td>
</tr>
<tr>
<td align="right">密 碼</td>
<td><input type="password" name="Password"></td>
</tr>
<tr>
<td><input type="submit" value="登 錄"></td>
<td><input type="reset" value="取 消"></td>
</tr>
</table>
</form>
</body>
</html>

 

/var/www/cgi-bin 下的pass.c

/*=====================================================================
cgi例子
=====================================================================*/
//pass.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getcgidata(FILE* fp, char* requestmethod);
int main()
{
       char *input;
       char *req_method;
       char name[64];
       char pass[64];
       int i = 0;
       int j = 0;
      
//     printf("Content-type: text/plain; charset=iso-8859-1\n\n");
       printf("Content-type: text/html\n\n");
       printf("The following is query reuslt:<br><br>");
       req_method = getenv("REQUEST_METHOD");
       input = getcgidata(stdin, req_method);
       // 我們獲取的input字元串可能像如下的形式
       // Username="admin"&Password="aaaaa"
       // 其中"Username="和"&Password="都是固定的
       // 而"admin"和"aaaaa"都是變化的,也是我們要獲取的
      
       // 前面9個字元是UserName=
       // 在"UserName="和"&"之間的是我們要取出來的用戶名
       for ( i = 9; i < (int)strlen(input); i++ )
       {
              if ( input[i] == '&' )
              {
                     name[j] = '\0';
                     break;
              }                  
              name[j++] = input[i];
       }
       // 前面9個字元 + "&Password="10個字元 + Username的字元數
       // 是我們不要的,故省略掉,不拷貝
       for ( i = 19 + strlen(name), j = 0; i < (int)strlen(input); i++ )
       {
              pass[j++] = input[i];
       }
       pass[j] = '\0';
       printf("Your Username is %s<br>Your Password is %s<br> \n", name, pass);
      
       return 0;
}
char* getcgidata(FILE* fp, char* requestmethod)
{
       char* input;
       int len;
       int size = 1024;
       int i = 0;
      
       if (!strcmp(requestmethod, "GET"))
       {
              input = getenv("QUERY_STRING");
              return input;
       }
       else if (!strcmp(requestmethod, "POST"))
       {
              len = atoi(getenv("CONTENT_LENGTH"));
              input = (char*)malloc(sizeof(char)*(size + 1));
             
              if (len == 0)
              {
                     input[0] = '\0';
                     return input;
              }
             
              while(1)
              {
                     input[i] = (char)fgetc(fp);
                     if (i == size)
                     {
                            input[i+1] = '\0';
                            return input;
                     }
                    
                     --len;
                     if (feof(fp) || (!(len)))
                     {
                            i++;
                            input[i] = '\0';
                            return input;
                     }
                     i++;
                    
              }
       }
       return NULL;
}

 

效果圖:

 

 

輸入數據點擊“登錄”

 

自動跳轉到 /cgi-bin/pass.cgi ,獲取到數據並列印

 

# 五、常見錯誤
你的配置會影響網址,502什麼錯誤也是配置或許可權有問題導致的。
如:
502 Bad Gateway
The CGI was not CGI/1.1 compliant.
cgi_header: unable to find LFLF
1.可能是網址打錯了(路徑是否和配置文件對應)
2.配置有問題
3.許可權沒給足 chmod 777 test.cgi

# 六、擴展(CCGI,SQLite)
CGIC的主站點: http://www.boutell.com/cgic/
SQLite官網:http://www.sqlite.org/
配置參考大佬博客:項目實戰
我在調試中遇到的問題也在大佬的博客下麵做了 評論 ,如果大家碰到問題可以參考一下。
補充:編譯時會出錯,使用

gcc -o config.cgi config.c sqlite3.c cgic.c -lsqlite3 -lpthread -ldl

 

![涉及文件]

 

 


![html效果]

 

 


![cgi頁面]

 

 


數據已寫入資料庫,可使用以下命令

sqlite3 person.db
.table
select * from person;

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 場景 C#中委托與事件的使用-以Winform中跨窗體傳值為例: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100150700 參考上面的博客。 現在是在主頁面點擊按鈕時彈出窗體,在窗體的load事件中進行事件的訂閱,然後關閉 ...
  • 最近項目中有一個需求,將樹形結構的數據,以表格的形式展示在頁面中,下圖是最終呈現效果: 源碼: 後臺代碼: 全部源碼:Asp.Net Mvc自定義控制項之樹形結構數據生成表格-WPF特工隊內部資料.rar ...
  • 首先下載 ffmpeg http://ffmpeg.org/ 註意一定要從官網下載,其他地方可以會有問題 解壓後在 bin 目錄下找到 ffmpeg.exe 用到的使命是 -i 視頻地址 -ss 第幾幀 -f image2 圖片存放地址 用cmd試一下,首先切換到ffmpeg.exe所在目錄,輸入命 ...
  • 實現效果: 上一篇文章有附全文搜索結果的設計圖,下麵截一張開發完成上線後的實圖: 基本風格是模仿的百度搜索結果,綠色的分頁略顯小清新。 目前已採集並創建索引的文章約3W多篇,索引文件不算太大,查詢速度非常棒。 刀不磨要生鏽,人不學要落後。每天都要學一些新東西。 基本技術介紹: 還記得上一次做全文搜索 ...
  • 在我們開發某個系統的時候,客戶總會提出一些特定的報表需求,固定的報表格式符合他們的業務處理需要,也貼合他們的工作場景,因此我們儘可能做出符合他們實際需要的報表,這樣我們的系統會得到更好的認同感。本篇隨筆介紹如何基於FastReport報表工具,生成報表PDF文檔展示醫院處方箋的內容。之前在隨筆《在W... ...
  • 最近使用txt文件進行數據處理的時候,突然發現txt文件是怎樣編碼數據的了,它是以二進位來進行存儲的嗎?為了知道這個情況,我使用hexdump工具進行查看txt文件的二進位形式,並順道進行學習了hexdump文件的使用: hexdump 一般用來查看“二進位”文件的十六進位編碼,但實際上它能查看任何 ...
  • 當我按照這種情景使用時,出現了這種情況: 考慮著 gnome 桌面正在運行,可能是gnome-terminal 使用了工廠模式進行創建;查找gnome-terminal 文檔,有如下解決方案: 使用上述命令,即可使用轉發訪問 gnome-terminal 其實為了方便 還是建議使用 terminat ...
  • 友善 friendlycore 掛載 overlayfs 過程:起點在 /boot 目錄 的 ramdisk.img// 掛載真正的boot到目錄下mount /dev/mmcblk0p1 /boot// 可以看到文件 /boot/ramdisk.img// copy出來單獨分析:cp ramdis ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...