如何解決 SSL Certificate Problem: Certificate Has Expired?

從 2021 年 9 月 30 日起,HTTP API 請求可能會停止在安裝了 Let’s Encrypt 證書的 WordPress 網站上工作。

這還包括了一些使用cloudflare的免費邊緣證書的網站。

這樣的錯誤隨處可見,例如:

  • 在檢查 WordPress 更新時在管理面板中。
  • 檢查插件更新時。
  • 訪問任何服務的api時。例如,將停止一些圖像壓縮請求的插件。

是什麼造成了Error 60: SSL certificate problem: certificate has expired?

具體表現為插件使用了如下代碼:

$res = wp_remote_get( 'https://www.affpeer.com/' ); 
    if( is_wp_error( $res ) ){ 
      echo $res->get_error_message(); 
}

簡而言之,WP core 有一個根證書文件,/wp-includes/certificates/ca-bundle.crt用於檢查通過 HTTP API 創建的所有請求的 SSL。

在此文件中,用於為您的站點創建證書的根證書之一已過期。因此,請求無法通過驗證並生成此錯誤。

隨著下一次 WP 更新,此錯誤消失,但如果您今天需要解決方案,或者您不打算更新 WordPress,但需要有效的 HTTP 請求,請執行以下操作。

解決方案一:更新證書文件解決Error 60 SSL 認證問題

您需要更新/wp-includes/certificates/ca-bundle.crt文件的內容,並且將其更改為https://curl.se/ca/cacert.pem文件的內容。

在這種情況下更改核心文件是可以接受的,因為等待下次更新WP時,問題就會消失。

具體步驟如下:

  • 下載此文件https://curl.se/ca/cacert.pem
  • /wp-includes/certificates/ca-bundle.crt使用下載文件中的內容更新內容。(修改前備份好這個文件)

OK, 現在一切都應該像以前一樣工作。

解決方案二:使用代碼解決Error 60 SSL 認證問題

将以下代码添加到 themes functions.php 文件中:

/**
 * Goto https://www.affpeer.com/?update-wp-ca-bundle
 */
if( isset( $_GET['update-wp-ca-bundle'] ) ){

	$crt_file = ABSPATH . WPINC . '/certificates/ca-bundle.crt';
	$new_crt_url = 'http://curl.haxx.se/ca/cacert.pem';

	if( is_writable( $crt_file ) ){
		$new_str = file_get_contents( $new_crt_url );

		if( $new_str && strpos( $new_str, 'Bundle of CA Root Certificates' ) ){
			$up = file_put_contents( $crt_file, $new_str );

			echo $up ? 'OK: ca-bundle.crt updated' : 'ERROR: can`t put data to ca-bundle.crt';
		}
		else {
			echo 'ERROR: can\'t download curl.haxx.se/ca/cacert.pem';
		}
	}
	else {
		echo 'ERROR: ca-bundle.crt not writable';
	}

	exit;
}

你可以使用兩種方法加入以上代碼,

  1. 使用theme editor修改function.php文件。 (具體打開Appearance>Theme Editor,如果你有多個主題文件,在右上角選擇你使用中的主題,並在右側主題文件中找到function.php。)
  2. 安裝Code Snippets插件,對function.php文件進行修改。

然後在瀏覽器中打開:https://XXX.com/?update-wp-ca-bundle(XXX替換為你的網域域名)

現在應該可以了,但你需要回到function.php刪除上述代碼。

Leave a Reply

Your email address will not be published.Required fields are marked *