Windows上的本地Apache服务器- PHP网站使用SSL连接到云SQL Postgres数据库

wwwo4jvm  于 2023-10-19  发布在  Windows
关注(0)|答案(1)|浏览(144)

我们有一个Windows桌面服务于Apache 2 PHP网站,目前从本地postgresql示例访问数据。我们正在将数据库迁移到Google SQL Postgres示例,并使用SSL加密的公共IP地址。
我已经在Google Cloud上创建了必要的证书。数据库已经启动并运行,我已经成功地让所有Windows应用程序(.Net和.Net Core)连接到Cloud SQL数据库示例。
到目前为止,我在让PHP连接到同一个数据库方面完全受阻。我尝试了连接字符串和参数的许多组合,但无法使任何东西工作。
示例pg_connect字符串(已清理):

  1. host={secret information} port=5432 dbname={secret information} user={secret information} password={secret information} sslmode=require sslcert=client-cert.pem sslkey=client-key.pem

client-cert.pem和client-key.pem是从Google生成/下载的,在Windows应用程序上运行良好。这些文件与我的html/php源代码一起保存在c:\apache24\htdocs中。我还存储了server-ca.pem,并尝试引用它。
我创建了(从另一个用户那里复制的-为什么要重新发明轮子)一个样本页面来测试,但没有得到任何结果。测试页面:

  1. <?php
  2. // Create a custom exception error handler for pg_connect
  3. function exception_error_handler($errno, $errstr, $errfile, $errline ) {
  4. throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
  5. }
  6. // Set the error handler
  7. set_error_handler("exception_error_handler");
  8. // Force output of all errors
  9. error_reporting(E_ALL);
  10. // Define constants
  11. define('DB_HOST', '{secret information}');
  12. define('DB_USER', '{secret information}');
  13. define('DB_PASS', '{secret information}');
  14. define('DB_NAME', '{secret information}');
  15. define('DB_PORT', '5432');
  16. // Connection string with SSL certificate files
  17. $conn_str = 'host=' . DB_HOST . ' ';
  18. $conn_str .= 'port=' . DB_PORT . ' ';
  19. $conn_str .= 'dbname=' . DB_NAME . ' ';
  20. $conn_str .= 'user=' . DB_USER . ' ';
  21. $conn_str .= 'password=' . DB_PASS . ' ';
  22. $conn_str .= 'sslmode=require ';
  23. $conn_str .= 'sslcert=client-cert.pem ';
  24. $conn_str .= 'sslkey=client-key.pem ';
  25. // Try catch block grabbing stored exception
  26. try {
  27. echo "Attempting pg_connect: " . $conn_str . "<br>";
  28. $conn=@pg_connect($conn_str);
  29. } catch (Exception $e) {
  30. echo $e->getMessage();
  31. }
  32. // Attempt Connection
  33. $conn = pg_connect($conn_str) or die('Cannot connect to database.');
  34. // Set SQL String
  35. $sql = 'SELECT * FROM client';
  36. // Attempt query and iterate results
  37. if (result = pg_query($conn, $sql))
  38. {
  39. while($row=pg_fetch_row($result))
  40. {
  41. var_dump($row);
  42. }
  43. }
  44. // Close it up
  45. pg_close();
  46. ?>

我使用测试时遇到的错误:

  1. Attempting pg_connect: host={secret information} port=5432 dbname={secret information} user={secret information} password={secret information} sslmode=require sslcert=client-cert.pem sslkey=client-key.pem
  2. pg_connect(): Unable to connect to PostgreSQL server: FATAL: connection requires a valid client certificate
  3. Fatal error: Uncaught ErrorException: pg_connect(): Unable to connect to PostgreSQL server: FATAL: connection requires a valid client certificate in C:\apache24\htdocs\test.php:38 Stack trace: #0 [internal function]: exception_error_handler(2, 'pg_connect(): U...', 'C:\\apache24\\htd...', 38, Array) #1 C:\apache24\htdocs\test.php(38): pg_connect('host={secret information}...') #2 {main} thrown in C:\apache24\htdocs\test.php on line 38

根据我在其他地方找到的提示,我尝试将完整路径添加到证书和密钥。已尝试包含server-ca.pem和sslpassword参数。
如果你能给我一些新的想法我会感激不尽的...

hgqdbh6s

hgqdbh6s1#

我的问题是如何键入证书和密钥的完整路径。Windows和'nix路径之间的差异。
将路径前缀设置为:

  1. c://apache24/htdocs/

一切正常
该死的/ \问题。

相关问题