PHP

[PHP] MacOS에서 OpenSSL 문제 처리

gilchris 2022. 6. 17. 23:07
PHP를 빌드했는데.. curl을 이용할 때나 file_get_contents에 url을 넣어서 텍스트를 읽어올 때 아래와 같은 SSL 인증서 오류가 나면서 동작이 되지 않는 문제가 생겼다.
PHP Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in /Users/gilchris/t.php on line 2
PHP Warning: file_get_contents(): Failed to enable crypto in /Users/gilchris/t.php on line 2
PHP Warning: file_get_contents(https://google.com): failed to open stream: operation failed in /Users/gilchris/t.php on line 2
핵심 내용은 'certificate verify failed'. 즉, 인증서가 확인이 안된다는 뜻이고, 이는 PHP에 설정된 root 인증서(root ca)가 제대로 없어서 이다.
자동으로 운영체제의 root 인증서를 읽어오면 참 좋겠는데 내가 빌드한 PHP라서 그런가;; 여튼 못 읽어온다.
다행히 php.ini에 [openssl] 항목에는 openssl.cafile 이라는 항목이 있고 여기에 root 인증서를 넣어주면 된다.
 
MacOS에서는 key chain이라는 툴로 root 인증서를 관리하고 있으며 여기에 등록된 모든 인증서를 아래와 같은 방식으로 pem 파일로 빼올(export) 수 있다.
security find-certificate -a -p \
/System/Library/Keychains/SystemRootCertificates.keychain \
/Library/Keychains/System.keychain \
~/Library/Keychains/login.keychain-db > keychain_certificates.pem
여기서 만들어진 keychain_certificates.pem 파일을 openssl.cafile 에 등록해서 이용하면 된다.
 

참고