C++と色々

主にC++やプログラムに関する記事を投稿します。

CentOS 6にopenssl1.0.2以上を使用したnginx1.10以上を入れる

モチベーション

自分が使ってるサーバーはcentos 6で、既にnginxが導入済みであり、http2に対応させたかったのが発端です。 ChromeはALPNのみ対応していますが、ALPN対応のopensslは1.0.2以上であり、CentOS 6のopensslは1.0.1系のため 自分でビルドしてopenssl 1.0.2以上を使用したnginxを用意しました。

ビルドツールを入れる

qiita.com

を参考に、ビルドツールを入れました:

# yum groupinstall  "Development Tools"
# yum install  pcre pcre-devel zlib zlib-devel

openssl, nginx, njsを入れる

qiita.com

を参考に、openssl, nginx, njsのソースコードを入れます。 ここの段落は $ cd /usr/local/src で作業しています。

oepnssl

$ sudo wget https://www.openssl.org/source/openssl-1.0.2-latest.tar.gz
$ sudo tar -zxf openssl-1.0.2-latest.tar.gz

1.0.2以上であれば良かったのでそのままいれましたが、普通は最新バージョンで良いと思います。

nginx

http://nginx.org/en/download.html から2018/05時点で最新の1.14.0を入れました:

$ sudo wget http://nginx.org/download/nginx-1.14.0.tar.gz
$ sudo tar -zxf nginx-1.14.0.tar.gz

njs

よくわからないので、 http://hg.nginx.org/njs/tags からtipをクリックして、 http://hg.nginx.org/njs/rev/tipハッシュ値のファイルをダウンロードしました

$ sudo wget http://hg.nginx.org/njs/archive/d0e244bc8760.tar.gz
$ sudo tar -zxf d0e244bc8760.tar.gz

njsのconfigureします:

$ sudo mv ./njs-d0e244bc8760 ./nginx-1.14.0
$ cd ./nginx-1.14.0
$ sudo mv njs-d0e244bc8760 njs-tip
$ cd /njs-tip
$ sudo ./configure

ビルドする

nginx -V で表示されたconfigure argumentsをコピーし、configureの引数に渡します。更に末尾に --with-openssl=/usr/local/src/openssl-1.0.2o --with-openssl-opt=-fPIC を付け足します。 --with-openssl-opt=-fPIC が無いと

nginx relocation R_X86_64_32 against `.rodata' can not be used when making a shared object

というエラーでビルド失敗します。

nginx - OpenSSL 1.0.2k + nginx ソースからのインストール時エラーが生じる(65991)|teratail

$ pwd
/usr/local/src/nginx-1.14.0
$ sudo ./configure --prefix=/etc/nginx ...(中略)... -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl=/usr/local/src/openssl-1.0.2o --with-openssl-opt=-fPIC
$ sudo make
$ sudo make install

makeは数分かかりました。

確認

$ nginx -V
nginx version: nginx/1.14.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.2o  27 Mar 2018
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 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl=/usr/local/src/openssl-1.0.2o --with-openssl-opt=-fPIC

http2にする

nginx.configのlistenにhttp2を追記します:

listen 443 ssl http2;

nginxを再起動します:

$ sudo service nginx restart

ChromeのNetworkタブでprotocolがh2になっていることを確認して満足しました