CAT724_nginxのインストール

CAT724へ nginx を組み込む手順について

nginx (エンジンエックスと発音する)は最近人気が高まっている小型高性能なWebサーバです。CAT724のROMへ組み込む手順について解説します。

準備

CAT724の内蔵ROMは debian squeeze (6.x) ベースです (2014年12月現在)。

microSD に debian squeeze 環境を用意し、microSDがrootfsになるようにしてブートします。

  • /etc/apt/sources.list を次のようにしてください。
deb http://download.si-linux.co.jp/debian-sh/squeeze-sh4 squeeze main
deb-src http://ftp.jp.debian.org/debian/ squeeze main contrib non-free
deb-src http://security.debian.org/ squeeze/updates main
deb-src http://ftp.jp.debian.org/debian/ squeeze-updates main

社内からインターネットに出る際にプロキシを経由する必要がある場合、環境変数を設定してください。

# export http_proxy=http://192.168.1.1:8080/
   プロキシが 192.168.1.1 ポート 8080 の例
# apt-cache clean
   (/var/cache/apt/archive をカラにします)
# apt-get update
root@supercat:~# apt-get install nginx-light fcgiwrap
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libfcgi0ldbl nginx-common spawn-fcgi
The following NEW packages will be installed:
  fcgiwrap libfcgi0ldbl nginx-common nginx-light spawn-fcgi
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 709 kB of archives.
After this operation, 1925 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
WARNING: The following packages cannot be authenticated!
  libfcgi0ldbl nginx-common spawn-fcgi fcgiwrap nginx-light
Install these packages without verification [y/N]? y
Get:1 http://download.si-linux.co.jp/debian-sh/squeeze-sh4/ squeeze/main libfcgi0ldbl sh4 2.4.0-8.1 [287 kB]
Get:2 http://download.si-linux.co.jp/debian-sh/squeeze-sh4/ squeeze/main nginx-common all 1.2.1-2.2+wheezy3 [72.8 kB]
Get:3 http://download.si-linux.co.jp/debian-sh/squeeze-sh4/ squeeze/main spawn-fcgi sh4 1.6.3-1 [13.6 kB]
Get:4 http://download.si-linux.co.jp/debian-sh/squeeze-sh4/ squeeze/main fcgiwrap sh4 1.0.3-3 [16.3 kB]
Get:5 http://download.si-linux.co.jp/debian-sh/squeeze-sh4/ squeeze/main nginx-light sh4 1.2.1-2.2+wheezy3 [319 kB]

ダウンロードした *.deb パッケージを /root/download に取っておきます。

# mkdir ~/download
# mv /var/cache/apt/archives/*.deb ~/download/
  • /etc/nginx/sites-enabled/default
server {
        listen   80; ## listen for ipv4
        # listen   [::]:80 default ipv6only=on; ## listen for ipv6  <= コメントアウト
        #root /usr/share/nginx/www;
        root /home/kaihatsu/www;

nginxをリスタートする

# /etc/init.d/nginx restart

動作テストのwebページを作る

# cp -a /usr/share/nginx/www /home/kaihatsu/

PCからブラウザで CAT724のipアドレスにアクセスする

pic2.png

CGIを動かす

# apt-get install fcgiwrap
  • /etc/nginx/sites-enabled/default を編集
        #location ~ \.php$ {
                #fastcgi_pass   127.0.0.1:9000;
                #fastcgi_index  index.php;
                #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                #includefastcgi_params;
        #}

+       location ~ \.cgi$ {
+               fastcgi_pass unix:/var/run/fcgiwrap.socket;
+               fastcgi_index index.cgi;
+               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+               include fastcgi_params;
+       }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one

CAT724内蔵ROMへのインストール

必要ファイルの抽出

次のようなスクリプトを実行します。先ほど apt-get でダウンロードしたパッケージは /root/download に保存されているので、これを /tmp/rootdir に展開する物です。

#!/bin/bash

ROOTDIR=/tmp/rootdir
DOWNLOAD=/root/download

# apt-get install -d --reinstall $PACKAGES
for FILE in $DOWNLOAD/*.deb
do
        # echo $FILE
        dpkg-deb --extract $FILE $ROOTDIR
done

実行後に tree コマンドで /tmp/rootfs/ を見ると展開されたファイル一覧がわかります。/usr/share/man/ と /usr/share/doc はROM環境へインストールする必要はないので手作業で消しておきます。

# rm /tmp/rootdir/usr/share/man/ -rf
# rm /tmp/rootdir/usr/share/doc/ -rf

展開されたファイルサイズは次の通りです。

# du -hs /tmp/rootdir/
1.7M    /tmp/rootdir/

内蔵ROMをマウントしてコピー

内蔵ROMのrootfs領域 (mtdblockの第2パーティション)をマウントし、ファイルをコピーします。

# mount /dev/mtdblock2 /media/ -t jffs2
# cp -av /tmp/rootdir/* /media/

/etcの設定ファイルをコピーします。

# cp -av /etc/nginx /media/etc/

/home/www のコンテンツをコピーします。

# cp -av /home/www/ /media/home/

nginxの自動起動

詳しくは「組み込みLinuxガイド」の自動起動の章を参考にしてください。

microSDにインストールされている debian_squeeze 環境にて、nginxを自動起動させている仕掛けは次の通りです。

# ls -l /etc/rc2.d/*
-rw-r--r-- 1 root root 677 Jul 28  2011 /etc/rc2.d/README
lrwxrwxrwx 1 root root  18 Dec  5 09:29 /etc/rc2.d/S01bootlogs -> ../init.d/bootlogs
lrwxrwxrwx 1 root root  17 Dec  5 09:29 /etc/rc2.d/S01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root  14 Dec  5 09:29 /etc/rc2.d/S02cron -> ../init.d/cron
lrwxrwxrwx 1 root root  15 Dec  8 08:39 /etc/rc2.d/S02nginx -> ../init.d/nginx  <== このシンボリックリンク
lrwxrwxrwx 1 root root  18 Dec  5 09:29 /etc/rc2.d/S03rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root  19 Dec  5 09:29 /etc/rc2.d/S03rmnologin -> ../init.d/rmnologin
lrwxrwxrwx 1 root root  23 Dec  5 09:29 /etc/rc2.d/S03stop-bootlogd -> ../init.d/stop-bootlogd
# cat /etc/init.d/.depend.start
TARGETS = rsyslog killprocs bootlogs single cron nginx stop-bootlogd rmnologin rc.local         <== TARGETSにnginxを追記
INTERACTIVE =
single: killprocs bootlogs
cron: rsyslog
nginx: rsyslog               <=== nginxはrsyslogの後に起動する
stop-bootlogd: rsyslog bootlogs cron nginx
rmnologin: rsyslog bootlogs cron nginx
rc.local: rsyslog bootlogs cron nginx
# cat /etc/init.d/.depend.stop
TARGETS = nginx urandom sendsigs rsyslog umountnfs.sh hwclock.sh networking  ifupdown umountfs umountroot halt reboot                 <=== TARGETSにnginxを追記
sendsigs: nginx                       <=== シグナルを投げる前にnginxが止まる
rsyslog: sendsigs nginx
umountnfs.sh: sendsigs rsyslog nginx
hwclock.sh: rsyslog
networking: umountnfs.sh nginx
ifupdown: umountnfs.sh networking nginx
umountfs: umountnfs.sh ifupdown urandom hwclock.sh networking nginx     <=== rootfsのアンマウント前にnginxが止まる
umountroot: umountfs
halt: umountroot
reboot: umountroot

従ってROM内のrootfsにも同様な設定を記述します

# cd /media/etc/rc2.d/
# ln -s ../init.d/nginx S02nginx
# vi /media/etc/init.d/.depend.start       (抜粋)
TARGETS = bootlogs killprocs single portmap nfs-common rsyslog openbsd-inetd cron ssh rc.local rmnologin stop-bootlogd nginx    <==  nginxを追加する
rc.local: rsyslog portmap nfs-common openbsd-inetd bootlogs cron ssh nginx  <=== rc.local(最後に動くので)この前に nginxが起動するような依存関係を記述
# vi /media/etc/init.d/.depend.stop  (抜粋)
TARGETS = urandom openbsd-inetd sendsigs rsyslog umountnfs.sh portmap nfs-common hwclock.sh networking ifupdown umountfs umountroot halt reboot nginx
sendsigs: openbsd-inetd nginx
umountfs: portmap networking hwclock.sh ifupdown urandom openbsd-inetd umountnfs.sh nginx
# sync
# umount  /media/
# reboot

内蔵romをrootfsとして起動してください。

bootloaderにて

#> setparam -d
#> boot

関連