1 はじめに
Webアプリ作成のための基礎知識をえるため、HTML入力フォームで
指定した文字列をデータベースから検索して表示する方法をまとめました。
2 環境
VMware Workstation 15 Player上の仮想マシン(1台)を使いました。
仮想マシンのOS版数、カーネル版数は以下のとりです。
本サーバのIPアドレスは、192.168.2.115
です。
2.1 各種版数
[root@server ~]# cat /etc/redhat-release
CentOS Linux release 8.3.2011
[root@server ~]# uname -r
4.18.0-240.el8.x86_64
[root@server ~]# php -v
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
[root@server ~]# mariadb -V
mariadb Ver 15.1 Distrib 10.5.8-MariaDB, for Linux (x86_64) using readline 5.1
2.2 SELinuxの状態
SELinuxの状態はEnforcing
です。無効にはしません!
[root@server ~]# getenforce
Enforcing
3 SELinuxルールの作成
SELinuxが有効だとhttpdがデータベースにアクセスできないので、
audit2allowコマンドを使って、アクセス拒否の理由を調べまてみました。
そして、正しいルールを作成して、httpdがデータベースにアクセスできるようにしました。
3.1 アクセス拒否理由の確認
audit2allowコマンドを実行すると、php-fpm
コマンドがconnect
システムコールを実行したとき、
アクセス拒否が発生していることがわかります。
アクセス拒否は、次のboolean値のいずれかが正しく設定されていないため、とあります。
・httpd_can_network_connect
・httpd_can_network_connect_db
[root@server ~]# audit2allow -w -a
-snip-
type=AVC msg=audit(1610774947.277:78): avc: denied { name_connect } for pid=913 comm="php-fpm" dest=3306 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:mysqld_port_t:s0 tclass=tcp_socket permissive=0
Was caused by:
One of the following booleans was set incorrectly.
Description:
Allow httpd to can network connect
Allow access by executing:
# setsebool -P httpd_can_network_connect 1
Description:
Allow httpd to can network connect db
Allow access by executing:
# setsebool -P httpd_can_network_connect_db 1
デフォルトのboolean値を確認します。
httpd_can_network_connect
とhttpd_can_network_connect_db
の
boolean値は、ともにoffとなっていることがわかります。
[root@server ~]# getsebool -a | grep httpd_can_network
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
3.2 ルールの作成
3.1
のメッセージにしたがって、boolean値をonにしました。
メッセージによると、httpd_can_network_connect
またはhttpd_can_network_connect_db
の
boolean値をonにする、と説明があるので、私の環境では、httpd_can_network_connect
を
onにしました。
[root@server ~]# setsebool -P httpd_can_network_connect 1
httpd_can_network_connect
がonになったことがわかります。
[root@server ~]# getsebool -a | grep httpd_can_network
httpd_can_network_connect --> on
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
4 テスト用データの作成
データベース(testdb
)を作成します。
なお、MariaDBの使い方は、MariaDBの使い方を参照してください。
MariaDB [testdb]> create database testdb;
使用するデータベースをtestdb
に切り替えます。
MariaDB [testdb]> use testdb;
testdb
にshop
テーブルを作成します。
shop
テーブルには、お店の住所、電話番号等を登録します。
MariaDB [testdb]> create table shop(id int unsigned, pref varchar(8), shop varchar(32), address varchar(128), tel varchar(32));
Query OK, 0 rows affected (0.006 sec)
MariaDB [testdb]> insert into shop values(1,"東京","リンガーハット","東京都町田市木曽東3丁目8-24","042-xxx-xxxx");
MariaDB [testdb]> insert into shop values(2,"東京","喜多方坂内","東京都多摩市落合1-45-1","042-xxx-xxxx");
MariaDB [testdb]> insert into shop values(3,"東京","洋庖丁","東京都新宿区高田馬場3-1-4","03-xxxx-xxxx");
MariaDB [testdb]> insert into shop values(4,"神奈川","丸亀製麺","神奈川県川崎市中原区上小田中4-2-1","044-xxx-xxxx");
MariaDB [testdb]> insert into shop values(5,"香川","手打十段うどんバカ一代","香川県高松市多賀町1-6-7","087-xxx-xxxx");
店舗情報を登録したテーブルを確認します。
MariaDB [testdb]> select * from shop;
+------+-----------+-----------------------------------+-------------------------------------------------+--------------+
| id | pref | shop | address | tel |
+------+-----------+-----------------------------------+-------------------------------------------------+--------------+
| 1 | 東京 | リンガーハット | 東京都町田市木曽東3丁目8-24 | 042-xxx-xxxx |
| 2 | 東京 | 喜多方坂内 | 東京都多摩市落合1-45-1 | 042-xxx-xxxx |
| 3 | 東京 | 洋庖丁 | 東京都新宿区高田馬場3-1-4 | 03-xxxx-xxxx |
| 4 | 神奈川 | 丸亀製麺 | 神奈川県川崎市中原区上小田中4-2-1 | 044-xxx-xxxx |
| 5 | 香川 | 手打十段うどんバカ一代 | 香川県高松市多賀町1-6-7 | 087-xxx-xxxx |
+------+-----------+-----------------------------------+-------------------------------------------------+--------------+
5 rows in set (0.000 sec)
4 HTMLファイルの作成
[root@server ~]# cat /var/www/html/shop.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="get" action="shop.php">
<select name="pref">
<option value="東京">東京</option>
<option value="神奈川">神奈川</option>
<option value="香川">香川</option>
</select>
<input type="submit" name="submit" value="検索">
</form>
</body>
</html>
5 PHPファイルの作成
HTMLファイルのactionタグから呼び出すPHPファイルを作成します。
なお、エラー処理は意図的に省略しています。
[root@server ~]# cat /var/www/html/shop.php
<?php
$res = "";
$pdo = new PDO('mysql:host=127.0.0.1;dbname=testdb', 'root', 'admin');
$sql = "select * from shop where pref=?";
$stmt = $pdo->prepare($sql);
$array = array($_GET{'pref'});
$stmt->execute($array);
$res = "<table>\n";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$res .= "<tr><td>" .$row['id'] .
"</td><td>" .$row['pref'].
"</td><td>" .$row['shop'].
"</td><td>" .$row['address'].
"</td><td>" .$row['tel'].
"</td></tr>\n";
}
$res .= "</table>\n"
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php echo $res; ?>
</body>
</html>
6 実行結果
ブラウザから下記URLにアクセスします。
http://192.168.2.115/shop.html
「東京」を選択して「検索」をクリックすると、東京の店だけが表示されていることがわかります。
Z 参考情報
PHPとMySQLのツボとコツがゼッタイにわかる本
HTML入力フォームの作成、徹底ガイド
10日でおぼえるHTML5入門教室