LoginSignup
4
7

More than 5 years have passed since last update.

Debian/Ubuntu 用に nginx をビルドする

Posted at

nginx は Apache と違ってモジュールを static リンクする必要がある都合上、自分でビルドしないと行けない場合が多い。
以下は Debian/Ubuntu 用の nginx のビルド方法のメモ。

1. ダウンロード

nginx公式サイトのダンロードページから、最新の nginx のアーカイブ(ここでは 1.9.0 とする)を落としてくる。

次にアーカイブを展開して cd する。

tar xvpf nginx-1.9.0.tar.gz
cd nginx-1.9.0

2. ビルド

まず、ビルドに必要なパッケージをインストールする。

sudo apt-get update
sudo apt-get install build-essential libpcre3-dev libssl-dev zlib1g-dev

次に、configure する。
nginx は configure の段階で利用可能なモジュールが決まる。下の例ではデフォルトのモジュールに加えて http_ssl_module, http_stub_status_module, http_gzip_static_module, http_spdy_module を有効にしている。
また、各種パスは Debian の nginx パッケージのパスに準拠している。ただし、prefix/usr ではなく /usr/local を指定している。
どのようなオプションがあるのかは ./configure --help を実行すれば表示してくれる。

./configure \
    --with-cc-opt="-O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2" \
    --with-ld-opt="-Wl,-Bsymbolic-functions -Wl,-z,relro" \
    --prefix=/usr/local \
    --conf-path=/etc/nginx/nginx.conf \
    --http-log-path=/var/log/nginx/access.log \
    --error-log-path=/var/log/nginx/error.log \
    --lock-path=/var/lock/nginx.lock \
    --pid-path=/run/nginx.pid \
    --http-client-body-temp-path=/var/lib/nginx/body \
    --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
    --http-proxy-temp-path=/var/lib/nginx/proxy \
    --http-scgi-temp-path=/var/lib/nginx/scgi \
    --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --with-http_spdy_module \
    --build=$USER

make して install する。

make -j4
sudo make install

/var/lib/nginx を作る。(make install で作ってくれないので)

sudo mkdir /var/lib/nginx

3. SysV init スクリプトのインストール

nginx の自動起動などを行うために SysV init 用のスクリプトをインストールする。Debian パッケージから持ってきてもいいが、ここでは nginx wiki に書いてあったレポジトリを利用する。

git clone https://github.com/Fleshgrinder/nginx-sysvinit-script.git
cd nginx-sysvinit-script
sudo make

(ところで、この makefile の shebang が /bin/sh になっているのが地味に気になる…)

4. 起動する

sudo service nginx start

これで、curl -D - http://localhost/ を実行してちゃんと HTML が返ってくれば OK。

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7