TinyHttp运行配置

作者

Tinyhttpd简介

Tinyhttpd 是J. David Blackstone在1999年写的一个不到 500 行的超轻量型 Http Server,用来学习非常不错,可以帮助我们真正理解服务器程序的本质

Tinyhttpd下载

可以直接下载github上的代码(
https://github.com/TronYY/Tinyhttpd),下载后解压在自己的目录下。

安装perl

测试时需要本机安装perl(mac和Ubuntu默认已装)可以在终端输入which perl判断perl是否已经安装,如果提示类似

1
/usr/bin/perl

则说明已安装上。如果没有则要自行安装。

安装perl-cgi

  • 执行perl -MCPAN -e shell
  • 执行install CGI.pm

可以在终端里执行perl -MCGI -e 'print "CGI.pm version $CGI::VERSION\n";' 这条命令来验证是否安装成功

如果出现 CGI.pm version 4.26 这种显示CGI版本的文本,说明安装成功。

注释和修改

  • 打开下载的Tinyhttpd中的httpd.c做如下修改:
  1. Comment out the #include <pthread.h> line.
  2. Comment out the line that defines the variable newthread.
  3. Comment out the two lines that run pthread_create().
  4. Uncomment the line that runs accept_request().
  • 打开同一个目录中的Makefile文件,去除掉"-lsocket"
  • 在终端输入which perl记录下perl 的路径如/usr/bin/perl
    输入打开Tinyhttpd的子文件夹htdocs,将其中的check.cgi和color.cgi的首行都改为
    1
    #!/usr/bin/perl -Tw

权限设置

在服务器运行以后,我遇到一个问题就是浏览器一直无输出页面,找了很久,发现是这个页面对应的文件index.html的读写权限问题,在Tinyhttpd的子文件夹htdocs下用以下命令修改文件权 限

1
sudo chmod 600 index.html

make和运行

在Tinyhttpd目录下打开终端输入make进行编译(如果提make: Nothing to be done for all’.则输入命令make clean后再输入make即可)

在终端在输入./httpd

1
httpd running on port 4000

随机端口号显示为4000,打开浏览器,输入http://127.0.0.1:4000即可出现如下网页,说明运行成功
image