CentOS6.XでHTTP/2のためにnginxを設定

centOS6.xはALPN対応パッケージがないので、自力でコンパイル&インストールする。

事前にやること

現在のパッケージインストールしたnginxのオプションを控えておく。

$sudo nginx -V

nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments:
 --prefix=/etc/nginx
 --sbin-path=/usr/sbin/nginx
 --modules-path=/usr/lib64/nginx/modules
 --conf-path=/etc/nginx/nginx.conf
 --error-log-path=/var/log/nginx/error.log
 --http-log-path=/var/log/nginx/access.log
 --pid-path=/var/run/nginx.pid
 --lock-path=/var/run/nginx.lock
 --http-client-body-temp-path=/var/cache/nginx/client_temp 
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp 
 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 
 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 
 --http-scgi-temp-path=/var/cache/nginx/scgi_temp 
 --user=nginx
 --group=nginx
 --with-compat
 --with-file-aio
 --with-threads 
 --with-http_addition_module
 --with-http_auth_request_module 
 --with-http_dav_module
 --with-http_flv_module
 --with-http_gunzip_module 
 --with-http_gzip_static_module
 --with-http_mp4_module
 --with-http_random_index_module 
 --with-http_realip_module
 --with-http_secure_link_module
 --with-http_slice_module 
 --with-http_ssl_module
 --with-http_stub_status_module 
 --with-http_sub_module
 --with-http_v2_module
 --with-mail
 --with-mail_ssl_module 
 --with-stream
 --with-stream_realip_module
 --with-stream_ssl_module 
 --with-stream_ssl_preread_module

OpenSSL1.0.1系のためALPNに対応した1.0.2系の最新版に変える。

パッケージアンインストールすると、現在の設定ファイル等も削除されてしまう場合があるので、/etc/nginx配下、/etc/inid.dの起動スクリプトをバックアップする。

$ mkdir ~/backup $ cd ~/backup $ sudo cp -r /etc/nginx . $ sudo cp /etc/init.d init_nginx

コンパイル

作業ディレクトリでopensslの最新版とnginxの最新版をダウンロードし、展開する。

$ wget https://www.openssl.org/source/openssl-1.0.2m.tar.gz 
$ tar zxvf openssl-1.0.2m.tar.gz 
$ wget https://nginx.org/download/nginx-1.13.7.tar.gz
$ wget zxvf nginx-1.13.7.tar.gz

コンパイルオプションは、各種技術系サイトの情報をベースに、設定パスやユーザ名等は極力、パッケージ版のものを踏襲するようにして、設定ファイル等の変更が発生しないようにしている。

$ cd nginx-1.13.7
$ ./configure  \
   --prefix=/etc/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 \
   --pid-path=/var/run/nginx.pid \
   --lock-path=/var/run/nginx.lock \
   --http-client-body-temp-path=/var/cache/nginx/client_temp \
   --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
   --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
   --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
   --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
   --user=nginx \
   --group=nginx \
   --with-http_ssl_module \
   --with-http_realip_module \
   --with-http_addition_module \
   --with-http_sub_module \
   --with-http_dav_module \
   --with-http_flv_module \
   --with-http_mp4_module \
   --with-http_gunzip_module \
   --with-http_gzip_static_module \
   --with-http_random_index_module \
   --with-http_secure_link_module \
   --with-http_stub_status_module \
   --with-http_auth_request_module \
   --with-threads \
   --with-stream \
   --with-stream_ssl_module \
   --with-http_slice_module \
   --with-mail \
   --with-mail_ssl_module \
   --with-file-aio \
   --with-http_v2_module \
   --with-ipv6 \
   --with-openssl=../openssl-1.0.2m/

--with-http_v2_module、--with-openssl=../openssl-1.0.2m/の指定必須。opensslは事前にコンパイル不要なので-with-opensslで指定するフォルダに展開しておけばよい。

足りないヘッダ等があれば、makeファイル作成時にエラーがでるのでxxx-devel等の該当パッケージをいれる。

$yum install pcre-devel
インストール、設定の戻し
$ make

ここまでエラーなく完了したら、nginxの既存パッケージをアンインストールして、コンパイルしたものをインストールする。

$ sudo yum remove nginx*
$ sudo make install

設定ファイルの書き戻し、ownerをnginx:nginxに戻しておく等。

$ cd ~/backup
$ sudo cp init_nginx /etc/inin.d/nginx
$ sudo cp -R nginx /etc/
$ sudo chown -R nginx:nginx /etc/nginx

ちゃんと反映されているか確認。

$ sudo nginx -V

nginx version: nginx/1.13.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
built with OpenSSL 1.0.2m  2 Nov 2017
TLS SNI support enabled
configure arguments:
 --prefix=/etc/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
 --pid-path=/var/run/nginx.pid
 --lock-path=/var/run/nginx.lock
 --http-client-body-temp-path=/var/cache/nginx/client_temp
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp
 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
 --http-scgi-temp-path=/var/cache/nginx/scgi_temp
 --user=nginx
 --group=nginx
 --with-http_ssl_module
 --with-http_realip_module
 --with-http_addition_module
 --with-http_sub_module
 --with-http_dav_module
 --with-http_flv_module
 --with-http_mp4_module 
 --with-http_gunzip_module
 --with-http_gzip_static_module
 --with-http_random_index_module
 --with-http_secure_link_module
 --with-http_stub_status_module
 --with-http_auth_request_module
 --with-threads
 --with-stream
 --with-stream_ssl_module
 --with-http_slice_module
 --with-mail
 --with-mail_ssl_module
 --with-file-aio
 --with-http_v2_module
 --with-openssl=../openssl-1.0.2m/

あとは、起動して今までのコンテンツが動作している事を確認して完了。

$ service nginx start