Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP connection to remote mysql with ssl works via command but not in browser

I want to connect to a remote (AWS) mysql server using ssl in PHP.

My script works when I execute it via command line, but doesn't when I call it from the browser.

$con=mysqli_init();
mysqli_ssl_set($con,NULL,NULL,"path/to/cacert.pem",NULL,NULL);
$link = mysqli_real_connect($con, "host", "username", "password");

I am using php7/Apache/CentOs. I tried changing the ownership and permissions of the CA file, and noticed that it requires read permission the be executed on console. But in browser even if I give full permission to everybody (chmod 777) it still doesn't work.

The error i get is: Warning: failed loading cafile stream. When I check existence of file it returns true, but when I check is_readible, then also error.

Can somebody help?Thanks!

like image 416
andrisbandris Avatar asked Jul 05 '17 15:07

andrisbandris


1 Answers

So as I figured there was (is) something wrong with readability and maybe permissions. I could narrow down the problem to the certificate file being existent but not readable. I moved it to my server with filezilla via ftp.

I could solve my problem by creating a new .pem file and simply copying the content of my original file into it. This one is readable now and works in browser, but I can't figure out why as they both have the same chmod xxx permissions and chown ownership.

Detailed description(for users with similar problem using AWS MYSQL):

  1. open rds-combined-ca-bundle.pem file(download link: https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem) in text editor.

  2. Copy content.

  3. On your server, where php script should run create new file and paste text into it.(it might require some additional editing, begin/end tags in separate line and new lines end at the same place as in original text)

like image 187
andrisbandris Avatar answered Nov 15 '22 00:11

andrisbandris