簡易的なweb serve実装のtinytinyhttpdの環境をセットアップする

mattnさんが「mattn/tinytinyhttpd: tiny tiny httpd」 というツールを作ってくれているので、今回はこのtthttpdをWindowsやLinuxでビルドする方法をまとめる。

tthttpdは、名前の通りC++実装(ほぼC)のミニweb server。 自作のちょっとしたCGIアプリの実験で使えるようになっている。

Install方法

windowsでのやり方

git clone git@github.com:mattn/tinytinyhttpd.git
cd tinytinyhttpd

プロジェクトにMakefile.w32があるのでこれを使う。 ただし、このまま使うと以下のエラーが出るので修正する必要がある。

cc1plus.exe: error: CPU you selected does not support x86-64 instruction set

なので、Makefile.w32を以下のように修正する

.SUFFIXES: .cxx .o

all : tthttpd.exe

tthttpd.exe : main.o httpd.o utils.o
    g++ -O2 -mthreads -o $@ main.o httpd.o utils.o -lws2_32

.cxx.o :
    g++ -O2 -mthreads -Wall -c $

clean :
    rm *.o tthttpd.exe

-mtune=i686 の箇所を削除しただけ。

そして、以下のようにビルド。 (makeは各自の環境に合わせること。僕はmingwのmakeを使用)

mingw32-make.exe -f Makefile.w32

warningが出るがビルドはできるはず。 あとは適当なディレクトリに入れてPATHを通せば使える。

ttphhtd --version

Linuxでのビルド

Linuxはより簡単にできる。 以下の通りやればOK

git clone git@github.com:mattn/tinytinyhttpd.git
cd tinytinyhttpd
./autogen.sh
./configure
make
sudo make install

warningが出るものの、特に問題なくビルドできる。 以下のコマンドで確認しよう。

ttphttd --version

使い方

使い方はシンプル。以下のようにすると、port8080で ./publicディレクトリをrootにして起動できる。

tthttpd -p 8080 -d ./public

もう少し細かい設定をしたければ、ini形式のconfファイルを作成すればよい。

例えば、tthttpd.confを作成してみる。

# cat tthttpd.conf
[global]
port=8080
root=./public
indexpages=index.html,index.php
default_cgi=index.cgi
charset=utf-8
spawnexec=on

[mime/types]
cgi=@/usr/bin/perl
php=@/usr/bin/php

個人的によく設定するのは以下の項目

  • port ポート番号
  • root rootディレクトリ
  • default_cgi どのurlのpathでも実行されるcgiを指定

正直、mime/typesはデフォルトのままでも十分機能する。

細かいオプション等については「tinytinyhttpd/main.cxx at master · mattn/tinytinyhttpd」 のコードを見るとわかる。

そして、confを作成した後は以下のように実行する。

tthttpd -c tthttpd.conf