HEARTBEATS

nginx連載2回目: nginxのインストール

   

前回はnginxの概要を紹介しましたが、今回はnginxのインストールについて紹介します。なお、設定については今回紹介する予定でしたが、文章が長くなってしまったので次回にします。

「百聞は一見にしかず」ということで、実際にインストールして体験してみましょう。

インストール方法

nginxのインストール方法は主に次の3通りです。

  • ソースコードからビルドしてインストールする
  • OSのパッケージシステムからパッケージをインストールする
  • nginx.org提供のバイナリパッケージをインストールする

お使いのOSがメジャーなLinuxディストリビューションや*BSD系のUNIXであれば、パッケージシステムからインストールできます。なお、本記事ではCentOS 6にnginx.org提供のパッケージを利用する前提で話を書きます。

また、次のサイトにも情報がありますので一度ご覧ください。

なお、2012年2月9日時点での最新の安定版のバージョンは1.0.12で、開発版のバージョンは1.1.14です。

モジュールの組み込みについて

前回はnginxはモジュールにより機能を拡張できる仕組みであると説明しましたが、そのモジュールの組み込みについてここで説明します。

公式サイトからtar ballをダウンロードして、展開すると次のようになります。

$ tar xvzf nginx-1.0.12.tar.gz 
$ cd nginx-1.0.12
$ ls -F
CHANGES     LICENSE  auto/  configure*  html/  src/
CHANGES.ru  README   conf/  contrib/    man/

ここで'./configure --help'と入力して実行すると、通常のオプションに加え、次のようにモジュールの組み込みに関するオプションが表示されます。

$ ./configure --help
略
  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
略
  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
略
  --add-module=PATH                  enable an external module

ここで、--with-FOO_moduleというオプションがあったら、FOOモジュールはデフォルトでは組み込まれません。組み込みたければそのオプションを指定する必要があります。逆に、--without-FOO_moduleというオプションがあったら、FOOモジュールはデフォルトで組み込まれるため、組み込みたくなければ--without-FOO_moduleを指定する必要があります。さらに、--add-moduleによりサードパーティのモジュールのパスを指定して組み込むことができます。

どのモジュールがデフォルトで組み込まれるかはnginx wiki - Modulesにモジュールの一覧があるので確認できます。

nginxの機能はモジュールをビルド時に組み込むことにより有効になるため、バイナリパッケージを利用する方でもこのようにしてモジュールが組み込まれることを知っておいてください。

ビルド済みのバイナリからもビルド時のオプションを次のようにして確認することができます。パッケージをインストールした場合には一度確認してみるとよいでしょう。

$ nginx -V
nginx version: nginx/0.8.54
built by gcc 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC)
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-file-aio --with-mail_ssl_module --with-ipv6 以下略

nginx.org提供パッケージのモジュール

公式ダウンロードサイトではRHEL 5, RHEL 6, CentOS 5, CentOS 6, Debian GNU/Linux 6(squeeze), Ubuntu 10.04 LTS用に安定版の最新バージョンのパッケージおよびその配布リポジトリが提供されています。このパッケージでは次のモジュールが組み込まれいませんが、利用する機会が少ない機能のモジュールであるため、問題は無いでしょう。

  • ngx_http_xslt_module
  • ngx_http_image_filter_module
  • ngx_http_geoip_module
  • ngx_http_degradation_module
  • ngx_http_perl_module

パッケージのバージョン

Linuxディストリビューションや*BSDでは以下のようなバージョンのnginxのパッケージが提供されています。(2012年2月9日現在)

Linuxディストリビューション

Linuxディストリビューションバージョン
RHEL 6/CentOS 6/Scientific Linux 6なし, (Fedora EPEL 6: 0.8.54), (nginx.org: 1.0.12)
RHEL 5/CentOS 5/Scientific Linux 5なし, (Fedora EPEL 5: 0.8.55), (nginx.org: 1.0.12)
Amazon Linux AMI 2011.090.8.54
Fedora 161.0.5, (updates: 1.0.10)
Fedora 150.8.54, (updates: 1.0.10)
OpenSUSE 12.11.0.5
Debian GNU/Linux squeeze0.7.67 (squeeze-backports: 1.1.8), (nginx.org: 1.0.12)
Debian GNU/Linux wheezy1.1.12
Ubuntu 11.101.0.5
Ubuntu 10.04 LTS0.7.65, (nginx.org: 1.0.12)
Gentoo Linux1.0.10

*BSD系

OSバージョン
FreeBSD1.0.12
NetBSD1.0.10
OpenBSD1.0.11
Mac OS Xなし, (MacPorts: 1.0.11)

インストール例

ここでは、CentOS 6.2にnginx.org提供のリポジトリからnginxのパッケージをインストールする例を示してみます。

まず、公式ダウンロードサイトの"Pre-Build Linux Packages"からリポジトリ用のRPMファイルをダウンロードしてインストールします。"CentOS 6"と書いてあるリンクをクリックするとダウンロードできます。wgetで取得してインストールする例は次の通りです。

$ wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

これにより次の内容の/etc/yum.repos.d/nginx.repoというファイルがインストールされます。

# nginx.repo

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

nginxのリポジトリが登録できたので、yumコマンドでnginxをインストールします。

# yum install nginx

以上でインストールは完了です。次にnginxのサービスを起動してみましょう。なお、別のWebサーバが動いていたら先に停止しておいてください。

# service nginx start

Webブラウザでインストールしたサーバにアクセスしてください。nginx.orgのパッケージの場合には"Welcome to nginx!"という文字が表示されます。エラー以外のページが表示されたら成功です。

ドキュメントのルートディレクトリ(nginx.orgパッケージの場合は/usr/share/nginx/html。パッケージ毎に異なる)にHTMLファイルを置くことによりWebサーバとして利用できます。

次回は設定について紹介します。来週を予定しています。