Laravel with PostgreSQL in sslmode

孤人 提交于 2021-01-28 14:21:46

问题


I'm working on a project using AWS, hosted on an EC2 instance with a RDS PostgreSQL. I'm wanting to make use of SSL encryption between my server and database, something AWS RDS supports but I can't figure out how to configure Laravel to make it work. Is there a way to specify sslmode when configuring a database connections?


回答1:


This seems to be a missing feature in Laravel 4, but I have submitted a pull request to fix this.

https://github.com/laravel/framework/pull/5488 (was merged on 2014-09-08)

To patch the code just add the following snippet to the getDsn function in the PostgresConnector.php file.

if (isset($config['sslmode']))
{
    $dsn .= ";sslmode={$sslmode}";
}

After that you may specify sslmode = 'require' in the pgsql database config section to test the encryption between the web app and your database. Other possible values for sslmode are 'disable', 'allow', 'prefer', 'require', 'verify-ca', 'verify-full'. The default value is 'prefer'.

For further info check the PostgreSQL docs: http://www.postgresql.org/docs/9.3/static/libpq-ssl.html




回答2:


  1. You have to tune your pg_hba.conf on your PostgreSQL server
  2. Generate server and client certificates
  3. Store your client certificates to Laravel server
  4. edit pgsql section at config/database.php, check my gist https://gist.github.com/tigusigalpa/2d1ec6e258ed96221abf9679622d274a



回答3:


Change your Laravel configuration regrading to the official Postgresql documentation:

change your postgres config in config/database.php to

'pgsql' => [
    'driver' => 'pgsql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
    'sslmode' => env('DB_SSLMODE', 'prefer'), <-- CHANGE THIS
],

and add the new option to your .env file:

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=mytestdb
DB_USERNAME=postgres
DB_PASSWORD=secret
DB_SSLMODE=disable  <-- ADD THIS


来源:https://stackoverflow.com/questions/25272838/laravel-with-postgresql-in-sslmode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!