LoginSignup
5
2

More than 5 years have passed since last update.

passenger + nginxでオプションを指定してビルドする

Last updated at Posted at 2017-05-05

Phusion Passengerを組み込んだnginxをビルドしてくれるpassenger-install-nginx-moduleですが、ここで自動ビルドされるnginxには欲しいモジュールが組み込まれていないかも知れません。
(例えばHTTP/2(ALPN)対応など)

passenger-install-nginx-moduleはそんな時も簡単に対応できました。

前提条件

以下の条件で確認しています

  • Phusion PassengerをGemインストール済
  • passenger 5.2.2

nginxのソースコードを準備

ビルドしたいバージョンのnginxソースコードを取得し、展開しておきます。

# cd /usr/local/src
# curl -L -O https://nginx.org/download/nginx-1.12.2.tar.gz
# tar xofz nginx-1.12.2.tar.gz

その他モジュール類のソースコードを準備

後程configure時に指定したいものがある場合もここで用意しておきます。
例えば、ALPNに対応するためにはOpenSSLのバージョンが1.0.2以上である必要がある 1 ため、ソースコードを取得し展開しておきます。

# cd /usr/local/src
# curl -L -O https://www.openssl.org/source/openssl-1.1.0h.tar.gz
# tar xofz openssl-1.1.0h.tar.gz

passenger-install-nginx-module実行

対話型なんてまどろっこしいことはしたくない、そんな方は補足へ。

第2の道へ

passenger-install-nginx-moduleを実行し、Automatically download and install Nginx?と聞かれるまで通常通り進めます。

# passenger-install-nginx-module

省略

Automatically download and install Nginx?

Nginx doesn't support loadable modules such as some other web servers do,
so in order to install Nginx with Passenger support, it must be recompiled.

Do you want this installer to download, compile and install Nginx for you?

 1. Yes: download, compile and install Nginx for me. (recommended)
    The easiest way to get started. A stock Nginx 1.12.2 with Passenger
    support, but with no other additional third party modules, will be
    installed for you to a directory of your choice.

 2. No: I want to customize my Nginx installation. (for advanced users)
    Choose this if you want to compile Nginx with more third party modules
    besides Passenger, or if you need to pass additional options to Nginx's
    'configure' script. This installer will  1) ask you for the location of
    the Nginx source code,  2) run the 'configure' script according to your
    instructions, and  3) run 'make install'.

Whichever you choose, if you already have an existing Nginx configuration file,
then it will be preserved.

Enter your choice (1 or 2) or press Ctrl-C to abort: 

ここで2を入力して進みます。

Enter your choice (1 or 2) or press Ctrl-C to abort: 2

ソースコードを指定

すると、次のようにnginxソースコードの場所を聞かれます。

Where is your Nginx source code located?

Please specify the directory: 

先程用意しておいた場所を入力して進みます。

Please specify the directory: /usr/local/src/nginx-1.12.2

インストール先を指定

続いて、nginxのインストール先を聞かれます。

Where do you want to install Nginx to?

Please specify a prefix directory [/opt/nginx]: 

/opt/nginxで構わなければそのままEnter、必要があれば場所を入力して進みます。

configureオプション追加

そしてお待ちかね、configure時に追加したいオプションを聞いてきます。

Extra Nginx configure options

If you want to pass extra arguments to the Nginx 'configure' script, then
please specify them. If not, then specify nothing and press Enter.

If you specify nothing then the 'configure' script will be run as follows:

  sh ./configure --prefix='/opt/nginx' --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-cc-opt=-Wno-error --with-ld-opt='' --add-module='/path/to/passenger-5.2.2/src/nginx_module'

Extra arguments to pass to configure script: 

ここで好きなようにオプションを指定します。
今回はHTTP/2(ALPN)対応のために--with-openssl=でOpenSSLのソースコードを指定しました。

Extra arguments to pass to configure script: --with-openssl='/usr/local/src/openssl-1.1.0h'

Enterで進むと、入力したオプションを含めた確認が表示されます。

Confirm configure flags

The Nginx configure script will be run as follows:

  sh ./configure --prefix='/opt/nginx' --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-cc-opt=-Wno-error --with-ld-opt='' --add-module='/path/to/passenger/src/nginx_module' --with-openssl='/usr/local/src/openssl-1.1.0h'

Is this what you want? (yes/no) [default=yes]: 

ここでnoを入力すると、Extra Nginx configure optionsまで戻り再度設定できるようになります。
問題なければ、yesあるいはそのままEnterで進みます。

ビルド

後は、ビルドが終わり以下のようなメッセージが表示されるまで待ちます。

Nginx with Passenger support was successfully installed.

The Nginx configuration file (/opt/nginx/conf/nginx.conf)
must contain the correct configuration options in order for Phusion Passenger
to function correctly.

This installer has already modified the configuration file for you! The
following configuration snippet was inserted:

  http {
      ...
      passenger_root /path/to/passenger;
      passenger_ruby /path/to/ruby;
      ...
  }

After you start Nginx, you are ready to deploy any number of Ruby on Rails
applications on Nginx.

Press ENTER to continue.

Enterを入力すると完了です。

Deploying a web application

To learn how to deploy a web app on Passenger, please follow the deployment
guide:

  https://www.phusionpassenger.com/library/deploy/nginx/deploy/

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
https://www.phusionpassenger.com

Passenger® is a registered trademark of Phusion Holding B.V.

確認

期待通りのnginxがビルドされたか確認しておきます。
インストール先のsbin/nginx -Vを実行します。

# /opt/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.1.0h  27 Mar 2018
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-cc-opt=-Wno-error --with-ld-opt= --add-module=/path/to/passenger/src/nginx_module --with-openssl=/usr/local/src/openssl-1.1.0h

今回の例では、built with OpenSSLのバージョンが指定したソースコードのものとなっていること、configure arguments:に指定したオプションが記述されていることが確認できます。

お疲れさまでした。

補足) 非対話型ビルド

上記の対話型ビルド作業は、passenger-install-nginx-moduleに引数を渡すことで非対話型で済ませることもできます。

# passenger-install-nginx-module --help
Usage: passenger-install-nginx-module [options]

Options:
        --auto                       Automatically confirm 'Press ENTER to
                                     continue' prompts.
        --prefix=DIR                 Use the given Nginx install prefix instead
                                     of asking for it interactively.
        --auto-download              Download and install Nginx automatically,
                                     instead of asking interactively whether to
                                     download+install or to use an existing
                                     Nginx source directory.
        --nginx-source-dir=DIR       Compile and install Nginx using the given
                                     Nginx source directory, instead of
                                     interactively asking to download+install
                                     or to use an existing Nginx source
                                     directory. Conflicts with --auto-download.
        --extra-configure-flags=STRING
                                     Pass these extra flags to Nginx's
                                     'configure' script, instead of asking for
                                     it interactively. Specify 'none' if you
                                     do not want to pass additional flags but do
                                     not want this installer to ask
                                     interactively either.
        --languages NAMES            Comma-separated list of interested
                                     languages (e.g.
                                     'ruby,python,nodejs,meteor')
        --force-colors               Display colors even if stdout is not a TTY

今回の記事の例では、以下のように設定すると対話型と同様の結果になります。

passenger-install-nginx-module --auto --prefix=/opt/nginx --nginx-source-dir='/usr/local/src/nginx-1.12.2' --extra-configure-flags="--with-openssl='/usr/local/src/openssl-1.1.0h'"
5
2
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
5
2