
WordPressをGoogle Cloud Platform(Compute Engineです)とCloudSQLで構築しています。
Compute EngineのインスタンスとCloudSQLの接続にSSLを使う方法です。
答えは、StackOverflowの中にある
WordPress Coreのエンハンスリクエスト(機能拡張)が#28625で出ています、そのうちに機能が追加されると思います。
wp-db.phpにちょいと手を入れるとSSL接続を行うことができます。
記事:Using amazon RDS with WordPress over SSL
この記事に方法が記載されていますので、この通りにwp-db.phpを修正します。
//ADDED per https://core.trac.wordpress.org/ticket/28625
// call set_ssl if mysql client flag set and settings available
if ( $client_flags & MYSQL_CLIENT_SSL ) {
$pack = array( $this->dbh );
$call_set = false;
foreach( array( 'MYSQL_SSL_KEY', 'MYSQL_SSL_CERT', 'MYSQL_SSL_CA',
'MYSQL_SSL_CAPATH', 'MYSQL_SSL_CIPHER' ) as $opt_key ) {
$pack[] = ( defined( $opt_key ) ) ? constant( $opt_key ) : null;
$call_set |= defined( $opt_key );
}
/* Now if anything was packed - unpack into the function.
* Note this doesn't check if paths exist, as per the PHP doc
* at http://www.php.net/manual/en/mysqli.ssl-set.php: "This
* function always returns TRUE value. If SSL setup is incorrect
* mysqli_real_connect() will return an error ..."
*/
if ( $call_set ) { // SSL added here!
call_user_func_array( 'mysqli_ssl_set', $pack );
}
}//END ADD - below is the point above which to insert this
(この前にペーストします)
if ( WP_DEBUG ) {
mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
wp-config.phpには、Google Developer Consoleで取得した証明書とキーを設定します。(証明書の発行後にね)
Google Developer Consoleで証明書を発行します
![]() |
Google Developers Console(Google Cloud PlatformのWeb UI) |
そこで、「クライアント証明書を作成」をクリックして名前(任意)で作成します。
![]() |
証明書とキー |
証明書とキーの取得ができたら、wp-config.phpにファイルを設定します。
define('MYSQL_CLIENT_FLAGS', MYSQL_CLIENT_SSL);
define('MYSQL_SSL_CA', 'サーバ証明書のファイル:server-ca.pem');
define('MYSQL_SSL_KEY', 'クライアントキーのファイル:client-key.pem');
define('MYSQL_SSL_CERT', 'クライアント証明書ファイル:client-crt.pem');
これでSSL接続できるようになります。
接続の確認方法
mysqlクライアントで接続確認:
shell> mysql --ssl-ca=server-ca.pem --ssl-cert=client-cert.pem --ssl-key=client-key.pem \ --host=instance-IP --user=user-name --password
mysql> \s -------------- mysql Ver 14.14 Distrib 5.5.32, for debian-linux-gnu (x86_64) using readline 6.2 Connection id: 7 Current database: Current user: user-name@client-IP SSL: Cipher in use is DHE-RSA-AES256-SHA ... TCP port: 3306 Uptime: 29 min 23 sec
です。
PHPコードで確認する場合は:
以下のスクリプトを適当なファイル名で置いて確認します。<?php
require( dirname( __FILE__ ) . '/wp-blog-header.php' ); //EDIT THIS PATH SO IT IS CORRECT FOR YOUR test.php file relative to the wp-blog-header.php file
global $wpdb;
$row = $wpdb->get_row( "SHOW STATUS LIKE 'Ssl_cipher'" );
var_dump($row);
/*
If you are connected over SSL this should output something like:
object(stdClass)#116 (2) { ["Variable_name"]=> string(10) "Ssl_cipher" ["Value"]=> string(10) "AES256-SHA" }
If you are NOT connected over SSL this should output something like:
object(stdClass)#116 (2) { ["Variable_name"]=> string(10) "Ssl_cipher" ["Value"]=> string(10) "" }
*/
?>
これでオッケー!