Memo

メモ > サーバ > 各論: Vagrant > PostgreSQL検証

■PostgreSQL検証
■基本設定 ※SELinuxを無効にしておく ※言語とタイムゾーンを設定しておく ■Apacheをインストール
# yum -y install httpd # vi /etc/httpd/conf/httpd.conf
ServerName localhost.localdomain:80
# service httpd start # chkconfig httpd on # vi /var/www/html/index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>テスト</title> </head> <body> <p>テスト。</p> </body> </html>
http://192.168.33.10/ ■PHPをインストール
# yum -y install epel-release # rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm # yum -y install --enablerepo=remi-php73 php php-cli php-common php-devel php-fpm php-mbstring php-pgsql php-pdo php-gd php-xml # vi /etc/php.ini
#error_reporting = E_ALL & ~E_DEPRECATED error_reporting = E_ALL #display_errors = Off display_errors = On
# service httpd restart # vi /var/www/html/phpinfo.php
<?php phpinfo() ?>
■PostgreSQLをインストール
# yum -y install postgresql-server # service postgresql initdb データベースを初期化中: [ OK ] # vi /var/lib/pgsql/data/postgresql.conf
# 59行目:コメント解除して変更(他ホストからのアクセスも受け付ける場合) listen_addresses = '*' # 397行目:コメント解除して変更(ログの形式を [日時 ユーザー DB 〜]とする) log_line_prefix = '%t %u %d '
# service postgresql start # chkconfig postgresql on
■PostgreSQL用のユーザとデータベースを作成 PostgreSQLの仕様上、同名のLinuxユーザも作成しておく必要がある
# useradd webmaster # passwd webmaster 1234
引き続き、PostgreSQLのユーザとデータベースを作成
# su - postgres -bash-4.2$ createuser webmaster -bash-4.2$ createdb test -bash-4.2$ exit
作成したユーザで接続テスト
# su - webmaster $ psql -l $ psql test test=> CREATE TABLE test(no INT, name TEXT); test=> INSERT INTO test VALUES(1, '山田太郎'); test=> INSERT INTO test VALUES(2, '山田花子'); test=> SELECT * FROM test; test=> \q
■PostgreSQLにPHPから接続
# vi /var/lib/pgsql/data/pg_hba.conf
# "local" is for Unix domain socket connections only … 丸ごとコメントアウト #local all all ident # IPv4 local connections: #host all all 127.0.0.1/32 ident # IPv6 local connections: #host all all ::1/128 ident local all all trust … 代わりに以下の設定を追加 host all all 127.0.0.1/32 trust host all all ::1/128 trust host all all 0.0.0.0/0 password
# service postgresql restart # vi /var/www/html/pgsql.php
<?php try { $pdo = new PDO( 'pgsql:dbname=test;host=localhost', 'webmaster', '1234' ); $stmt = $pdo->query('SELECT NOW() AS now;'); $data = $stmt->fetch(PDO::FETCH_ASSOC); echo "<p>" . $data['now'] . "</p>\n"; } catch (PDOException $e) { exit($e->getMessage()); } $pdo = null;

Advertisement