理想未来ってなんやねん

娘可愛い。お父さん頑張る。

CentOS 5.xにnginxをインストールする

本日から数回に分けてnginxのインストール方法について記載したいと思います。


nginxのうんちくについては今日は面倒なので省略します。気が向いたら後で記載します。

nginxにインストールする

CentOSの場合、リポジトリを追加することでyumを使用してインストールできます。


まず、/etc/yum.repos.d/nginx.repo に以下の内容を記載。

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/5/$basearch/
gpgcheck=0
enabled=1


yumでインストールする。

$ sudo yum install nginx


OS起動時に自動起動できるようにしておきます。
その前にApacheが起動している場合は先に停止しておきます。

$ sudo /sbin/chkconfig httpd off
$ sudo /sbin/service httpd stop
$ sudo /sbin/chkconfig nginx on
$ sudo /sbin/service nginx start

設定

設定は/etc/nginx/nginx.confで行います。
nginx.confの設定はデフォルトで下記のようになっています。

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


httpの設定を見ると最後の行にincludeで始まる行があり、/etc/nginx/conf.d/*.confのファイルを読み込んでいることが分かります。


/etc/nginx/conf.d/の中のファイルをみると、以下のようになっています。

$ ls -la /etc/nginx/conf.d/
drwxr-xr-x 2 root root 4096  2月  5 00:12 .
drwxr-xr-x 3 root root 4096  2月  5 00:07 ..
-rw-r--r-- 1 root root 1097 12月 16 00:14 default.conf
-rw-r--r-- 1 root root  454 12月 16 00:14 example_ssl.conf

Apacheで言うところのDocumentRootの設定は、/etc/nginx/conf.d/default.confに記載されている。
default.confのデフォルトの設定は以下の通り。

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #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/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

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

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


/usr/share/nginx/htmlがデフォルトのDocumentRootになっていることがわかります。


phpは動かす為の設定がコメントアウトしてあり、そのまでは実行できません。
コメント内容をみるとリバースプロキシとしてApacheなど他のhttpdにリクエストするか、FastCGIにリクエストするようになっている。


phpを実行させる方法は様々ありますが、個人的な要件*1を満たすものとしては、php-fpmを使うのが一番近いような気がします。


php-fpmを使用するにはソースからビルドするかremiなどのリポジトリから追加する必要があります。
個人的には野良リポジトリは使わない方針の為、ソースからビルドする方針で進めていきます。


以上、今日のところはここまで。


次回に続く。


ハイパフォーマンスHTTPサーバ Nginx入門

ハイパフォーマンスHTTPサーバ Nginx入門

*1:速度、バーチャルホストでのセキュリティ確保、バーチャルホストを増やす設定の簡単さ