nginx をインストールする

nginx インストールまでの流れ

% whereis nginx
nginx: /usr/ports/www/nginx
% cd /usr/ports/www/nginx
% sudo make fetch-recursive
% sudo make install clean

インストールされたディレクトリ。

% ls -l /usr/local/sbin/nginx
-r-xr-xr-x  1 root  wheel  531356  523 15:19 /usr/local/sbin/nginx

nginx を起動する。

デーモンとして起動をしたいので、rc.conf に「nginx_enable="YES"」を追加する。

% sudo vi /etc/rc.conf
nginx_enable="YES"

そして、起動させる。

% sudo /usr/local/etc/rc.d/nginx start
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.

単純に一回だけ起動させたい場合は、

% sudo /usr/local/etc/rc.d/nginx onestart

nginx で使うための SSL/TSL 証明書を準備する

root ではないと作業ができないので、su で root になって作業をする。
暗号用の乱数を生成し、秘密鍵を生成する。

% su -
Password:
# openssl sha1 /var/log/messages > rand.dat
# ls -l
total 12
-rw-r--r--  1 root  wheel  9472 May 19 14:37 openssl.cnf
-rw-r--r--  1 root  wheel    66 May 23 16:12 rand.dat
# openssl genrsa -rand rand.dat -des 2048 > 20110523_key_with_pass.pem
66 semi-random bytes loaded
Generating RSA private key, 2048 bit long modulus
................................................................................................+++
.....+++
e is 65537 (0x10001)
Enter pass phrase:
Verifying - Enter pass phrase:

最後に入力したパスフレーズは、サーバーを起動するごとにパスワードの入力を求められるので、パスワード抜きで動作するようにする。

# openssl rsa -in 20110523_key_with_pass.pem -out 20110523_key_without_pass.pem
Enter pass phrase for 20110523_key_with_pass.pem:
writing RSA key

CSR と X.509 証明書の生成をする。

# openssl req -new -key 20110523_key_without_pass.pem -out 20110523_csr.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
# openssl x509 -in 20110523_csr.pem -req -signkey 20110523_key_without_pass.pem -out 20110523_x509.pem
Signature ok

nginx で秘密鍵と証明書を利用するために、任意のディレクトリに移動させる。今回はファイル本体を移動させずに、ln コマンドでシンボリックリンクを使う。

# mkdir /usr/local/etc/nginx/ssl
# ln -s /etc/ssl/20110523_key_without_pass.pem /usr/local/etc/nginx/ssl/server.key
# ln -s /etc/ssl/20110523_x509.pem /usr/local/etc/nginx/ssl/server.pem

PHP を使えるようにする

まずは PHP をインストールする。

% whereis php5
/usr/ports/lang/php5
% cd /usr/ports/lang/php5
% sudo make fetch-recursive
% sudo make install clean
% rehash
% php -v

spawn-fcgi をインストールする

まずはインストールをする。

% whereis spawan-fcgi
/usr/ports/www/spawn-fcgi
% cd /usr/ports/www/spawn-fcgi
% sudo make fetch-recursive
% sudo make insall clean

rc.conf に設定を追加する。

% sudo vi /etc/rc.conf
spawn_fcgi_enable="YES"

spawn-fcgi を起動する。

% sudo /usr/local/etc/rc.d/spawn-fcgi start
Starting spawn_fcgi.
spawn-fcgi: child spawned successfully: PID: 76317

起動の状態を確認する。

% sockstat -4
USER          COMMAND    PID   FD PROTO  LOCAL ADDRESS         FOREIGN ADDRESS
www           php-cgi    76322 0  tcp4   127.0.0.1:9000        *:*
www           php-cgi    76321 0  tcp4   127.0.0.1:9000        *:*
www           php-cgi    76320 0  tcp4   127.0.0.1:9000        *:*
www           php-cgi    76319 0  tcp4   127.0.0.1:9000        *:*
www           php-cgi    76318 0  tcp4   127.0.0.1:9000        *:*
www           php-cgi    76317 0  tcp4   127.0.0.1:9000        *:*
littlebuddha  sshd       76277 3  tcp4   182.xxx.xx.xxx:20048  xxx.xxx.xxx.178:14928
root          sshd       76275 3  tcp4   182.xxx.xx.xxx:20048  xxx.xxx.xxx.178:14928
www           nginx      30499 6  tcp4   *:80                  *:*
root          nginx      30498 6  tcp4   *:80                  *:*
root          ntpd       27023 20 udp4   *:123                 *:*
root          ntpd       27023 22 udp4   182.xxx.xxx.xxx:123   *:*
root          ntpd       27023 25 udp4   127.0.0.1:123         *:*
root          sendmail   794   4  tcp4   127.0.0.1:25          *:*
root          sshd       786   4  tcp4   *:20048               *:*
root          syslogd    499   7  udp4   *:514                 *:*

nginx と spawn-fcgi が起動しているのを確認できる。

SSL/TSL と spawn-fcgi が使えるように nginx.conf の設定を変更する

% sudo cp /usr/loca/etc/nginx/nginx.conf /usr/loca/etc/nginx/nginx.conf_20110523

基本的な設定は下記のようにしている。
細かい設定は、nginx の公式サイトを参照。

nginx の基本的な設定内容

nginx を起動するユーザーとグループの「www」が存在するかどうかを念のため確認をしておく。

% id www
uid=80(www) gid=80(www) groups=80(www)

OS によっては、「www」ではなく「nobody」が httpd を起動するユーザーとして登録されている場合がある。

user www www;
worker_processes 4;
error_log   /var/log/nginx/error.log;
pid       /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout   15;

    gzip       on;
    gzip_types  text/plain text/css application/xml application/xhtml+xml application/x-javascript;

    server {
        listen       80;
        server_name  example.com www.example.com;

        access_log   /var/log/nginx/www.example.com/access.log;

        location / {
            # DocumentRoot を指定。
            root   /var/www/WebDocumentRoot/www/www.example.com;
            index  index.php index.cgi index.html index.htm;
        }

        # 静的ファイルの有効期限
        location ~ ^/\.(jpg|png|gif|js|css)$/  {
            root    /var/www/virtual/big.server.com/htdocs;
            expires 30d;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /var/www/WebDocumentRoot/www/www.example.com;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    # HTTPS server
    #
    server {
        listen       443;
        server_name  example.com www.example.com;

        ssl                  on;
        ssl_certificate      /usr/local/etc/nginx/ssl/server.pem;
        ssl_certificate_key  /usr/local/etc/nginx/ssl/server.key;

        ssl_session_timeout  5m;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;

        location / {
            root   html;
            index  index.php index.cgi index.html index.htm;
        }

        location ~ \.php$ {
            root           /var/www/WebDocumentRoot/www/www.example.com;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param  HTTPS on;
            include        fastcgi_params;
        }
    }
}

そして、設定ファイルの記述方法に誤りがないかを確認する。

# nginx -c nginx.conf -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

誤りがなければ、nginx を再起動させる。

# /usr/local/etc/rc.d/nginx restart
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Stopping nginx.
Waiting for PIDS: 76974.
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.

spawn-fcgiPHP について

spawn-fcgi をインストールして、何の設定もせずに PHP のプロセスが生成されるのは、「/usr/local/etc/rc.d/spawn-fcgi」にそういう設定が記述されているからだ。
その設定には、PHP へのパスや、どのポート番号を使うのか、どれぐらいのプロセスを生成するのかなども記載されているので、必ず参照すること。

newsyslog に nginx のログを設定する

% sudo vi /etc/newsyslog.conf
/var/log/nginx/www11295u.sakura.ne.jp/access.log  644 30 * @T00 JC /var/run/nginx.pid
/var/log/nginx/error.log                          644 30 * @T00 JC /var/run/nginx.pid

毎日 0時に nginx のログを新しくし、当日以前のログは30日分を圧縮して保存するという設定にしている。