LoginSignup
0
0

More than 1 year has passed since last update.

PHP5.6でOpenssl1.1.0に対応する

Posted at

はじめに

サーバーAのSSL証明書をLet's Encryptに変更後、サーバーBからサーバーAのapiを叩くと以下のエラーが返ってきた。

error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /var/www/hoge/class/Net/Socket.php on line 108
PHP Warning:  fsockopen(): Failed to enable crypto in /var/www/hoge/class/Net/Socket.php on line 108
PHP Warning:  fsockopen(): unable to connect to ssl://fuga.jp:443 (Unknown error) in /var/www/hoge/class/Net/Socket.php on line 108

参考記事にあるように、今回はサーバーBのルート証明書が古かった。
参考:https://qiita.com/kidach1/items/ee2de96a41672ca64dbb

サーバーBのシステム情報:
・debian 8 (Jessie)
・OpenSSL 1.0.1t 3 May 2016
・PHP 5.6.13

サービスになるべく影響が出ないようにPHPのバージョンは変えずに対応したい。
対応方法としては以下の2つ:

  1. Opensslのバージョンを上げるためにOSのバージョンを上げる
  2. ルート証明書からDST Root X3を削除し、ISRG Root X1を追加(参考:https://zenn.dev/kjur/articles/9f3028b5c47d75)

最終的には2.で対応したが、1.で一通り動く環境が作成できたので記録として残しておく。

OSアップデート

Let's Encryptによるとdebian 8でも対応できそうだが、aptのアップデートを試しても更新されなかったのでOSを上げてOpensslのバージョンを上げることによって対応することにした。

Debian >= jessie / 8 (with updates applied)
参考:https://letsencrypt.org/docs/certificate-compatibility/

幸いDockerで環境構築されているので、Dockerfileを編集して対応していく。

FROM debian:jessieFROM debian:stretch

Opensslのバージョンが1.1.0lに上がっていることを確認。

# openssl version
OpenSSL 1.1.0l  10 Sep 2019

debian 9でPHP5.6の環境構築

次にPHPのインストールと設定を行なっていたが、ここでcurlのエラーが発生。

# ./configure \
		--with-config-file-path=/usr/local/etc/php \
		--with-config-file-scan-dir=/usr/local/etc/php/conf.d \
		--with-apxs2 \
		--disable-cgi \
		--with-mysql=/usr \
		--with-mysqli=mysqlnd \
		--with-pdo-mysql=mysqlnd \
		--with-curl \
		--with-openssl \
		--with-pcre \
		--with-readline \
		--with-recode \
		--with-zlib

~~略~~

checking for cURL support... yes
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/

findで探してもeasy.hはないことが判明。

PHPのバージョンを7以上にすると動くらしいが、影響範囲が大きいので今回はバージョンを上げずに対応したい。

Expected Result
the installation of the later versions to 7 I can install normal everything perfect.
But the installation of the 5th version I can not
参考:https://github.com/phpbrew/phpbrew/issues/925

対応策として以下の方法が記載されていたが、「libcurl4-openssl-dev」を使用していたため競合して「libcurl4-gnutls-dev」をインストールすることができなかった。
また/usr/include/x86_64-linux-gnu/curl/がそもそも無かったので、シンボリックリンクを作成することもできなかった。

sudo apt-get install libcurl4-gnutls-dev
cd /usr/local/include/
sudo ln -s /usr/include/x86_64-linux-gnu/curl/
参考:https://github.com/phpbrew/phpbrew/issues/925#issuecomment-454216651

php5.6-curlを入れると動きそうだったので、インストールしようとしたらパッケージが見つからない。

Unable to locate package php5.6-curl
Couldn't find any package by glob 'php5.6-curl'
Couldn't find any package by regex 'php5.6-curl'

リポジトリを追加するといいらしいが、参考した環境はdebian10で動いていたので再度OSをアップデートして試した。

the same problem was fixed by adding
to the source list such line
deb https://packages.sury.org/php/ buster main
and after that
sudo apt update
sudo apt install php5.6-curl
参考:https://github.com/oerdnj/deb.sury.org/issues/1249#issuecomment-748468974

Dockerfileに以下を追加

RUN echo "deb https://packages.sury.org/php/ buster main" >> /etc/apt/sources.list.d/php.list

するとパッケージの認証に失敗

Err:4 https://packages.sury.org/php buster InRelease
   The following signatures couldn't be verified because the public key is not available: NO_PUBKEY B188E2B695BD4743
 Reading package lists...
W: GPG error: https://packages.sury.org/php buster InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY B188E2B695BD4743
E: The repository 'https://packages.sury.org/php buster InRelease' is not signed.

gpgキーを取得し直すといいらしいのでwgetで取ってくる

RUN echo "deb https://packages.sury.org/php/ buster main" >> /etc/apt/sources.list.d/php.list \
	&& wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg \
	&& apt-get update && apt-get install -y php5.6-curl

Appears fixed after re-downloading the gpg key:
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
参考:https://github.com/oerdnj/deb.sury.org/issues/1575#issuecomment-801183214

するとeasy.hが入ってること確認できたので、シンボリックリンクを作成したところPHPの設定はできた。
しかしPHPのビルドでエラーが発生。

make: *** [Makefile:521: ext/openssl/openssl.lo] Error 1

PHP5.6でOpenssl1.1に対応する

OSをdebian10にした影響で、Opensslのバージョンが1.1.1nになっていた。
1.1.0で環境構築している例はいくつか見つかったが、1.1.1では見つけることができなかった。。。
https://github.com/oerdnj/deb.sury.org/issues/566
https://www.howtoforge.com/community/threads/php-5-6-additional-version-on-debian-9.77460/

Opensslのバージョンを1.1.0にするために再度debian9に戻してphp5.6-curlを入れるための対応を再度行う。

RUN apt-get update && apt-get install -y apt-transport-https
RUN echo "deb https://packages.sury.org/php/ stretch main" >> /etc/apt/sources.list.d/php.list \
	&& wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg \
	&& apt-get update && apt-get install -y php5.6-curl \
	&& ln -s /usr/include/x86_64-linux-gnu/curl /usr/local/include/curl	

最終的に以下のようにOSとPHPのバージョンを変更してパッチを当てることによって対応した
・debian 9(stretch)
・PHP 5.6.31
・パッチ:https://bugs.php.net/patch-display.php?bug_id=72360&patch=openssl&revision=latest

パッチを当てる方法は以下を参考にした:

download PHP source
cd source folder and patch -p1 < ../patchname
and the normal build process
https://github.com/oerdnj/deb.sury.org/issues/566#issuecomment-330321649

動作確認:

root@949be36c8668:/# openssl version
OpenSSL 1.1.0l  10 Sep 2019

root@949be36c8668:/# php -v
PHP 5.6.31 (cli) (built: Mar 31 2022 09:07:22) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

一通り確認したところPHPの動作は問題がなかったが、OSを上げた影響でmysql-clientのバージョンが上がり不具合が発生したので切り戻し。

結論:バージョンのアップデートは影響が大きいので慎重に。

0
0
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
0
0