Nginxをインストールしてみる

VirtualBoxにCentOSをとりあえずインストール
の続き

Nginxをインストールしてみる。


とりあえずrootになっておく。

$ su -

インストール

Nginxのリポジトリを登録して、yumでインストールする。

# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# yum -y install nginx

起動して、自動起動の設定をする。

# service nginx start
# chkconfig nginx on

http://サーバーのアドレス/
にアクセスすると、Nginxのページが表示される。

DocumentRootの変更

DocumentRootは、下記のファイルで設定されている。

# cat /etc/nginx/conf.d/default.conf
server {
    ・・・
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    ・・・
}

DocumentRootを「/var/www/html」にしてみる。

まず、ディレクトリを作成し、index.htmlを置いておく。

# mkdir -p /var/www/html
# vi /var/www/html/index.html
Hello, world!

設定を下記のように修正する。

# vi /etc/nginx/conf.d/default.conf
server {
    ・・・
    location / {
        root   /var/www/html;
        index  index.html index.htm;
    }
    ・・・
}

再起動して設定を反映。

# service nginx restart

http://サーバーのアドレス/
にアクセスすると、「Hello, world!」が表示される。

PHPの実行

NginxでPHPを実行してみる。

とりあえず、 PHPをインストールする。

# yum -y install php

NginxでPHPを動作させるには、php-fpmというものが必要とのことなのでインストールする。

# yum -y install php-fpm

起動して、自動起動の設定をする。

# service php-fpm start
# chkconfig php-fpm on

Nginxの設定を変更する。

# vi /etc/nginx/conf.d/default.conf
server {
    ・・・
    location / {
        root   /var/www/html;
        index  index.php index.html index.htm;	←index.phpを追加
    }
    ・・・
    location ~ \.php$ {				←以下、コメントを解除
        root           /var/www/html;		←/var/www/htmlを設定
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/html$fastcgi_script_name; ←/var/www/htmlを設定
        include        fastcgi_params;
    }
}

再起動して設定を反映。

# service nginx restart

下記のindex.phpを設置して、

# vi /var/www/html/index.php
<?php
phpinfo();

http://サーバーのアドレス/
にアクセスすると、phpinfoのページが表示される。

Apacheとの比較

abでApacheと比較してみる。

まずは、Nginxを計測してみる。
Macから下記のabコマンドを実行すると、約3.8秒かかった。

mac$ ab  -c 100 -n 1000 http://サーバーのアドレス/
・・・
Server Software:        nginx/1.6.0
Server Hostname:        サーバーのアドレス
Server Port:            80

Document Path:          /
Document Length:        40066 bytes

Concurrency Level:      100
Time taken for tests:   3.866 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      40212000 bytes
HTML transferred:       40066000 bytes
Requests per second:    258.65 [#/sec] (mean)
Time per request:       386.626 [ms] (mean)
Time per request:       3.866 [ms] (mean, across all concurrent requests)
Transfer rate:          10156.98 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.2      0      17
Processing:    26  366  69.6    391     476
Waiting:       23  363  69.6    388     473
Total:         30  367  69.1    391     488

Percentage of the requests served within a certain time (ms)
  50%    391
  66%    405
  75%    411
  80%    415
  90%    421
  95%    454
  98%    464
  99%    469
 100%    488 (longest request)

次にApacheを計測してみる。

VirtualBoxにCentOSをとりあえずインストール
の状態から、下記の手順でApachePHPをインストール。

$ su -
# yum -y install httpd php
# service httpd start
# vi /var/www/html/index.php
<?php
phpinfo();

Macから下記のabコマンドを実行すると、約4.8秒かかった。

mac$ ab  -c 100 -n 1000 http://サーバーのアドレス/
・・・
Server Software:        Apache/2.2.15
Server Hostname:        サーバーのアドレス
Server Port:            80

Document Path:          /
Document Length:        43411 bytes

Concurrency Level:      100
Time taken for tests:   4.847 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      43583000 bytes
HTML transferred:       43411000 bytes
Requests per second:    206.32 [#/sec] (mean)
Time per request:       484.683 [ms] (mean)
Time per request:       4.847 [ms] (mean, across all concurrent requests)
Transfer rate:          8781.31 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.1      0      10
Processing:    83  467 120.4    490     993
Waiting:       20  296  72.4    313     491
Total:         89  468 119.4    490     994

Percentage of the requests served within a certain time (ms)
  50%    490
  66%    522
  75%    541
  80%    556
  90%    594
  95%    631
  98%    698
  99%    753
 100%    994 (longest request)