postgresqlでssl通信をする
2017/02/03
webサーバとDBサーバはローカルIPで通信しているので非SSL通信で設定していたのですが、グローバルIPからDBに接続する必要ができたのでSSLの設定をしました。
環境
CentOS6
PostgreSQL 9.1.8
PGHOME=/usr/local/pgsql
必要パッケージのインストール
1 |
yum install openssl-devel |
PostgreSQLが起動していたら停止する。
コンパイル
PostgreSQLは既にソースからコンパイルしていました。
opensslを組み込んで再コンパイルします。
1 2 3 4 5 |
cd /usr/local/src/postgresql-9.1.8 make clean ./configure --without-readline --without-zlib --with-openssl make make install |
opensslが組み込まれたか確認
1 2 |
# ldd /usr/local/pgsql/bin/postgres |grep ssl libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007f828d684000) |
SSLを有効化
1 2 3 4 |
vim /usr/local/pgsql/data/postgresql.conf #ssl = off ↓↓↓↓ ssl = on |
外部からSSLで接続するように設定
1 2 3 |
vim /usr/local/pgsql/data/pg_hba.conf 追記 hostssl all all グローバルIP password |
証明書の作成
1 2 3 4 5 6 7 |
# cd /usr/local/pgsql/data # openssl req -new -text -out server.req # openssl rsa -in privkey.pem -out server.key # rm privkey.pem # openssl req -x509 -in server.req -text -key server.key -out server.crt # chmod og-rwx server.key # chown postgres:postgres server.key |
PosgreSQLを再起動してSSL通信ができているか確認します。
参考サイト
https://www.postgresql.jp/document/8.4/html/ssl-tcp.html