树莓派安装Web服务器Boa和CGIC

news/2024/7/6 0:33:03 标签: 嵌入式, linux, cgi

树莓派安装Web服务器Boa和CGIC

陈拓 2020/08/01-2020/08/09

 

1. 树莓派换源

为了加快所需软件的下载,我们需要先换源。

  • 首先查看系统版本:lsb_release -a

  • 修改软件更新源 /etc/apt/sources.list

sudo nano /etc/apt/sources.list

在下面的语句前面加#注释掉这行:

#deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi

可以用阿里源:

deb http://mirrors.aliyun.com/raspbian/raspbian/ buster main contrib non-free rpi

也可以用科大源:

deb http://mirrors.ustc.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi

然后按Ctrl + O存盘,Ctrl + X退出。

  • 修改系统更新源/etc/apt/sources.list.d/raspi.list

sudo nano /etc/apt/sources.list.d/raspi.list

在下面的语句前面加#注释掉这行:

#deb http://archive.raspberrypi.org/debian/ buster main

在这里可以添加阿里的源:

deb http://mirrors.aliyun.com/archive.raspberrypi.org/debian/ buster main ui

也可以里用科大源:

deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ buster main ui

  • 同步更新源

sudo apt-get update

换源完成。

2. 安装Boa

  • 更新软件源

sudo apt-get update

  • 安装Boa

sudo apt-get install boa

删除不需要的包:

sudo apt autoremove

  • 查看安装的boa软件包

sudo dpkg -l |grep boa

3. Boa服务器的配置

  • boa配置文件boa.conf的位置

sudo find / -name "boa.conf"

  • 修改配置文件

sudo nano /etc/boa/boa.conf

  • 查看我的所有配置

cat /etc/boa/boa.conf | grep -v "^#"

Port 80

User root

Group root

ErrorLog /var/log/boa/error_log

AccessLog /var/log/boa/access_log

DocumentRoot /var/www

UserDir public_html

DirectoryIndex index.html

DirectoryMaker /usr/lib/boa/boa_indexer

KeepAliveMax 1000

KeepAliveTimeout 10

MimeTypes /etc/mime.types

DefaultType text/plain

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

Alias /doc /usr/share/doc

ScriptAlias /cgi-bin/ /var/www/cgi-bin/

  • 对照检查各目录和文件是否存在,如果不存在创建之

/var/www目录存在                                    

/var/log/boa/error_log文件存在

/var/log/boa/access_log文件存在

/usr/lib/boa/boa_indexer文件存在

/etc/mime.types文件存在

/bin:/usr/bin:/usr/local/bin目录存在

/usr/share/doc目录存在

在/var/www下创建子目录cgi-bin

sudo mkdir /var/www/cgi-bin

  • 日志文件

error_log和access_log是今天的日志,以前的日志会每天形成一个压缩文件,旧日志如果不需要可以删除。

  • 查看boa进程是否已经启动

ps -e | grep boa

  • 写默认首页: index.html

在/var/www/目录下创建简单的index.html文件。

<html>
<body>
<h1>Hello Pi BOA.</h1>
</body>
</html>

sudo nano /var/www/index.html

  • 测试

在PC浏览器中输入http://raspberrypi.local/

用IP地址访问boa服务器响应会更快。

查IP地址:

ifconfig

http://192.168.137.212

  • 查看错误日志

sudo cat /var/log/boa/error_log

request "GET /favicon.ico HTTP/1.1" ("/var/www/favicon.ico"): document open: No such file or directory

复制一个favicon.ico到/var/www

(可以用psftp,用法请看《用psftp在电脑和树莓派之间互传文件》https://blog.csdn.net/chentuo2000/article/details/106780169)

再次测试前要清除浏览器缓存。对于Chrome,按下Ctrl + Shift + Del 快捷键:

点击“清除数据”按钮。

再测试:

  • 访问日志

访问日志记录了浏览器的IP地址,主机和浏览器等信息。

sudo cat /var/log/boa/access_log

  • CGI程序的位置

CGI程序必须放在cgi-bin目录下。

4. 安装和测试CGIC

4.1 安装CGIC

CGIC是一个C语言cgi库,最新版本2.08,可以从github下载。

Github网址:https://github.com/boutell/cgic

下载之前认真阅读README.md。

  • 在树莓派上克隆cgic

git clone https://github.com/boutell/cgic.git

  • 查看cgic目录

  • 编译CGIC

cd cgic

make

查看编译结果:

libcgic.a:CGIC库

capture:调试辅助程序

cgictest.cgi:测试程序

  • 安装CGIC

sudo make install

CGIC安装路径为:

libcgic.a 安装在/usr/local/lib

cgic.h 安装在/usr/local/include

CGIC库安装后就可以使用CGIC编程了。

将capture和cgictest.cgi拷贝/var/www/cgi-bin目录:

sudo cp capture /var/www/cgi-bin

sudo cp cgictest.cgi /var/www/cgi-bin

  • 说明

也可以不编译安装CGIC,每次编译的时候,只要把cgic.c和cgic.h放到当前文件夹就好了。

4.2 测试CGIC

  • 写一个简单的cgi测试程序

建一个工作目录:

nano test.c

#include <stdio.h>

int main(void)
{
    printf("Content-Type:text/plain;charset=us-ascii\n\n");
    printf("Hello World\n\n");
    return 0;
}
  • 编译test.c

gcc -o test.cgi test.c

  • 使用Makefile编译

test.cgi:test.c

   gcc test.c -o test.cgi

注意:第二行开头一定是一个tab键(且仅有一个),不能使用空格。

保存好Makefile的内容之后,执行make命令就会编译生成test.cgi

如果没有编译安装CGIC,要先将cgic.h和cgic.c复制到工作目录,再这样

写Makefile文件:

test.cgi:test.c cgic.h cgic.c

    gcc test.c cgic.c -o test.cgi

  • 在本地运行测试

./test.cgi

将test.cgi复制到/var/www/cgi-bin

sudo cp test.cgi /var/www/cgi-bin

  • 在PC端浏览器运行测试

http://192.168.137.212/cgi-bin/test.cgi

重新驱动树莓派,再测试:

  • 运行CGIC自带的测试程序

在PC端浏览器运行:

http://192.168.137.212/cgi-bin/cgictest.cgi

 

参考文档

  1. 嵌入式设备web开发笔记:boa和cgic
    https://www.jianshu.com/p/dafcf652baed?utm_campaign=haruki
  2. 芯灵思SINLINX A33开发板BOA与CGI移植https://www.cnblogs.com/Sinlinx/p/10438726.html

 


http://www.niftyadmin.cn/n/1733229.html

相关文章

树莓派I2C通过Shell操作FDC2214

陈拓 chentuoms.xab.ac.cn 2020/07/21-2020/07/29 FDC2214是Ti公司的一款低功耗高精度的电容传感器芯片。本文讲述用树莓派Linux Shell配置和操作FDC2214&#xff0c;可以快速熟悉并进行原型开发。 1. 树莓派换源 为了加快所需软件的下载&#xff0c;我们需要先换源。 首先查…

界面渐变特效 -- CSS实现 -- 兼容IE8

特别注意&#xff1a;里面的RGB颜色值必须要全写&#xff0c;不能使用缩写。左右&#xff1a;background: -webkit-gradient(linear, 0 0, 0 100%, from(#80c1e7), to(#213c7c)); background: -webkit-linear-gradient(left, #80c1e7, #213c7c); background: -moz-linear-g…

javascript面向对象技术基础(二)

数组 我们已经提到过,对象是无序数据的集合,而数组则是有序数据的集合,数组中的数据(元素)通过索引(从0开始)来访问,数组中的数据可以是任何的数据类 型.数组本身仍旧是对象,但是由于数组的很多特性,通常情况下把数组和对象区别开来分别对待(Throughout this book, objects and…

树莓安装Nginx并支持CGI

陈拓 2020.09.17/2020.09.17 1. 概述 百度百科对Nginx 的介绍&#xff1a; Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器&#xff0c;同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔赛索耶夫为俄罗斯访问量第二的Rambler.ru站点&#xff08;俄文&#xff1a;…

Position属性四个值:static、fixed、absolute和relative的区别

1、static&#xff08;静态定位&#xff09;&#xff1a;默认值。没有定位&#xff0c;元素出现在正常的流中&#xff08;忽略 top, bottom, left, right 或者 z-index 声明&#xff09;。 2、relative&#xff08;相对定位&#xff09;&#xff1a;生成相对定位的元素&#x…

树莓派安装使用数据库SQLite

陈拓 2020/07/29-2020/07/29 1. 概述 SQLite官网&#xff1a;https://www.sqlite.org/index.html 最新版本&#xff1a; 官方对SQLite的介绍&#xff1a; SQLite是一个C语言库&#xff0c;它实现了一个小型、快速、自包含、高可靠性、全功能的SQL数据库引擎。SQLite是世界上…

(网页)JS和CSS不缓存方法,时间戳

<link ..... href".....css?time" new Date()><script type"text/javascript" src"js/retaccountDetailhtml.js?var1"></script>每次代码变动之后自动加上一个时间戳&#xff0c;使之前旧版本失效把。很多构建工具都可以做到…

如何清空type=file控件内容

<script>function go() {document.getElementById("ttt").outerHTML"";}</script><input typefile namettt id"ttt"> <input typebutton οnclick"go()" value清除file框的内容>