nginxとfcgiwrapでcgiを動かす(Ubuntu 10.04、10.10対応)
メモリの少ないマシンでHTTPサーバを立てようとして、nginxについて調べてみたのだけど、日本語の情報のクオリティが低い(コンパイルからやってる。古い)ので、odawaraが書いておくよ。
1.nginxとfcgiwrapを入れる。
10.10(Maverick)には両方パッケージがあるので、
# apt-get install nginx fcgiwrap
でOK。
10.04(lucid)にはfcgiwrapがないのだけど、Maverickのパッケージが使えるので(ただのスクリプトだからね)借りてくる。
(64bitの時) # wget http://ftp.jaist.ac.jp/pub/Linux/ubuntu//pool/universe/f/fcgiwrap/fcgiwrap_1.0-1_amd64.deb (32bitの時) # wget http://ftp.jaist.ac.jp/pub/Linux/ubuntu//pool/universe/f/fcgiwrap/fcgiwrap_1.0-1_i386.deb 依存関係の解決 # apt-get install libfcgi0ldbl spawn-fcgi 入れる。 # dpkg -i dpkg -i fcgiwrap_1.0-1_amd64.deb # dpkg -i dpkg -i fcgiwrap_1.0-1_i386.deb 忘れずにnginxも入れる。 # apt-get install nginx
2.apacheなんかが動いてる時は止めておく。
# /etc/init.d/apache2 stop
3.nginxの設定ファイルを編集する。
fcgiwrapのnginxの設定例が/usr/share/doc/fcgiwrap/examples/nginx.confにあるので、ここからコピペする。
# vim /etc/nginx/sites-available/default
こうなる。
server { listen 80; ## listen for ipv4 listen [::]:80 default ipv6only=on; ## listen for ipv6 server_name localhost; access_log /var/log/nginx/localhost.access.log; location / { root /var/www; index index.html index.htm; } location /doc { root /usr/share; autoindex on; allow 127.0.0.1; deny all; } location /images { root /usr/share; autoindex on; } # ここから追加 # fcgiwrap location /cgi-bin/ { # Disable gzip (it makes scripts feel slower since they have to complete # before getting gzipped) gzip off; # Set the root to /usr/lib (inside this location this means that we are # giving access to the files under /usr/lib/cgi-bin) root /usr/lib; # Fastcgi socket fastcgi_pass unix:/var/run/fcgiwrap.socket; # Fastcgi parameters, include the standard ones include /etc/nginx/fastcgi_params; # Adjust non standard parameters (SCRIPT_FILENAME) fastcgi_param SCRIPT_FILENAME /usr/lib$fastcgi_script_name; } # ここまで }
4.fcgiwrapを起動する。
# /etc/init.d/fcgiwrap start
5.nginxを起動する(起動してるかもしれないので、restart)
# /etc/init.d/nginx restart
6.CGIの動作確認。
/usr/lib/cgi-bin/にhello.cgiを置く。
hello.cgi
(置いてからchmod +xするのを忘れずに)
#!/usr/bin/python print "Content-type: text/html\n" print "<html>" print "<head>" print "<title>Hello world !</title>" print "</head>" print "<body>" print "Hello world !<br>" print "</body>" print "</html>"
localhost/cgi-bin/hello.cgiをブラウザで開いて動作確認。
おわり。
追記…
初期設定だと同時接続時に異常に遅いので調整する。
/etc/nginx/nginx.conf
nginxの待ち受けプロセスをCPU数に変更。
#worker_processes 1; worker_processes 2;
FastCGIの待ち受けプロセス数を増やす。
/etc/init.d/fcgiwrap
#FCGI_CHILDREN="1" FCGI_CHILDREN="8"