树莓派安装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

- 查看错误日志
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.youkuaiyun.com/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

参考文档
- 嵌入式设备web开发笔记:boa和cgic
嵌入式设备web开发笔记:boa和cgic - 简书 - 芯灵思SINLINX A33开发板BOA与CGI移植https://www.cnblogs.com/Sinlinx/p/10438726.html

本文详细介绍了在树莓派上安装和配置Web服务器Boa的过程,包括更换软件源以加速下载,以及安装和配置Boa服务器的具体步骤。此外,还深入探讨了如何安装和测试CGIC,一个用于C语言的CGI库,提供了从克隆项目到编译、安装及测试的完整流程。
4487

被折叠的 条评论
为什么被折叠?



