0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[OCI]ウォレットを使用せずにPHPアプリケーションからAutonomous DatabaseにTLS認証で接続してみた

Last updated at Posted at 2022-02-23

はじめに

こちらの記事では、ウォレットを使用せずにSQL*PlusからAutonomous DatabaseにTLS認証で接続できることを確認しました。

今回は、php-oci8を使用したPHPアプリケーションから、ウォレットを使用せずにSQL*PlusからAutonomous DatabaseにTLS認証で接続してみます。

こちらの記事で作成したAutonomous DatabaseとComputeインスタンスを使用して環境を構築します。

1.PHP、php-oci8、httpd(Apache2)のインストールと設定

yumコマンドでPHP、php-oci8とApache2をインストールします。

[opc@compute1 ~]$ sudo yum install -y oracle-php-release-el7
Loaded plugins: langpacks, ulninfo
Resolving Dependencies
--> Running transaction check
---> Package oracle-php-release-el7.x86_64 0:1.0-5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================
 Package                              Arch                 Version                  Repository                Size
===================================================================================================================
Installing:
 oracle-php-release-el7               x86_64               1.0-5.el7                ol7_latest                14 k

Transaction Summary
===================================================================================================================
Install  1 Package

Total download size: 14 k
Installed size: 19 k
Downloading packages:
oracle-php-release-el7-1.0-5.el7.x86_64.rpm                                                 |  14 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : oracle-php-release-el7-1.0-5.el7.x86_64                                                         1/1 
  Verifying  : oracle-php-release-el7-1.0-5.el7.x86_64                                                         1/1 

Installed:
  oracle-php-release-el7.x86_64 0:1.0-5.el7                                                                        

Complete!
[opc@compute1 ~]$ sudo yum install -y php php-oci8-19c
Loaded plugins: langpacks, ulninfo
ol7_developer_php74                                                                         | 3.0 kB  00:00:00     
(1/2): ol7_developer_php74/x86_64/updateinfo                                                |   71 B  00:00:00     
(2/2): ol7_developer_php74/x86_64/primary_db                                                | 420 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:7.4.28-1.0.1.el7 will be installed
--> Processing Dependency: httpd-mmn = 20120211x8664 for package: php-7.4.28-1.0.1.el7.x86_64
--> Processing Dependency: php-cli(x86-64) = 7.4.28-1.0.1.el7 for package: php-7.4.28-1.0.1.el7.x86_64
--> Processing Dependency: php-common(x86-64) = 7.4.28-1.0.1.el7 for package: php-7.4.28-1.0.1.el7.x86_64
--> Processing Dependency: httpd for package: php-7.4.28-1.0.1.el7.x86_64
---> Package php-oci8-19c.x86_64 0:7.4.28-1.0.1.el7 will be installed
--> Processing Dependency: php-pdo(x86-64) = 7.4.28-1.0.1.el7 for package: php-oci8-19c-7.4.28-1.0.1.el7.x86_64
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-97.0.5.el7_9.4 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-97.0.5.el7_9.4 for package: httpd-2.4.6-97.0.5.el7_9.4.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-97.0.5.el7_9.4.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-97.0.5.el7_9.4.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-97.0.5.el7_9.4.x86_64
---> Package php-cli.x86_64 0:7.4.28-1.0.1.el7 will be installed
---> Package php-common.x86_64 0:7.4.28-1.0.1.el7 will be installed
---> Package php-pdo.x86_64 0:7.4.28-1.0.1.el7 will be installed
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-7.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.0.1.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-97.0.5.el7_9.4 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================
 Package                 Arch              Version                            Repository                      Size
===================================================================================================================
Installing:
 php                     x86_64            7.4.28-1.0.1.el7                   ol7_developer_php74            3.4 M
 php-oci8-19c            x86_64            7.4.28-1.0.1.el7                   ol7_developer_php74            148 k
Installing for dependencies:
 apr                     x86_64            1.4.8-7.el7                        ol7_latest                     103 k
 apr-util                x86_64            1.5.2-6.0.1.el7                    ol7_latest                      91 k
 httpd                   x86_64            2.4.6-97.0.5.el7_9.4               ol7_latest                     1.2 M
 httpd-tools             x86_64            2.4.6-97.0.5.el7_9.4               ol7_latest                      93 k
 mailcap                 noarch            2.1.41-2.el7                       ol7_latest                      30 k
 php-cli                 x86_64            7.4.28-1.0.1.el7                   ol7_developer_php74            5.1 M
 php-common              x86_64            7.4.28-1.0.1.el7                   ol7_developer_php74            1.1 M
 php-pdo                 x86_64            7.4.28-1.0.1.el7                   ol7_developer_php74            122 k

Transaction Summary
===================================================================================================================
Install  2 Packages (+8 Dependent packages)

Total download size: 11 M
Installed size: 47 M
Downloading packages:
(1/10): apr-1.4.8-7.el7.x86_64.rpm                                                          | 103 kB  00:00:00     
(2/10): httpd-2.4.6-97.0.5.el7_9.4.x86_64.rpm                                               | 1.2 MB  00:00:00     
(3/10): httpd-tools-2.4.6-97.0.5.el7_9.4.x86_64.rpm                                         |  93 kB  00:00:00     
(4/10): mailcap-2.1.41-2.el7.noarch.rpm                                                     |  30 kB  00:00:00     
(5/10): apr-util-1.5.2-6.0.1.el7.x86_64.rpm                                                 |  91 kB  00:00:00     
(6/10): php-7.4.28-1.0.1.el7.x86_64.rpm                                                     | 3.4 MB  00:00:00     
(7/10): php-cli-7.4.28-1.0.1.el7.x86_64.rpm                                                 | 5.1 MB  00:00:00     
(8/10): php-common-7.4.28-1.0.1.el7.x86_64.rpm                                              | 1.1 MB  00:00:00     
(9/10): php-oci8-19c-7.4.28-1.0.1.el7.x86_64.rpm                                            | 148 kB  00:00:00     
(10/10): php-pdo-7.4.28-1.0.1.el7.x86_64.rpm                                                | 122 kB  00:00:00     
-------------------------------------------------------------------------------------------------------------------
Total                                                                               19 MB/s |  11 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : php-common-7.4.28-1.0.1.el7.x86_64                                                             1/10 
  Installing : apr-1.4.8-7.el7.x86_64                                                                         2/10 
  Installing : apr-util-1.5.2-6.0.1.el7.x86_64                                                                3/10 
  Installing : httpd-tools-2.4.6-97.0.5.el7_9.4.x86_64                                                        4/10 
  Installing : php-pdo-7.4.28-1.0.1.el7.x86_64                                                                5/10 
  Installing : php-cli-7.4.28-1.0.1.el7.x86_64                                                                6/10 
  Installing : mailcap-2.1.41-2.el7.noarch                                                                    7/10 
  Installing : httpd-2.4.6-97.0.5.el7_9.4.x86_64                                                              8/10 
  Installing : php-7.4.28-1.0.1.el7.x86_64                                                                    9/10 
  Installing : php-oci8-19c-7.4.28-1.0.1.el7.x86_64                                                          10/10 
  Verifying  : php-pdo-7.4.28-1.0.1.el7.x86_64                                                                1/10 
  Verifying  : php-oci8-19c-7.4.28-1.0.1.el7.x86_64                                                           2/10 
  Verifying  : httpd-2.4.6-97.0.5.el7_9.4.x86_64                                                              3/10 
  Verifying  : mailcap-2.1.41-2.el7.noarch                                                                    4/10 
  Verifying  : apr-1.4.8-7.el7.x86_64                                                                         5/10 
  Verifying  : apr-util-1.5.2-6.0.1.el7.x86_64                                                                6/10 
  Verifying  : php-cli-7.4.28-1.0.1.el7.x86_64                                                                7/10 
  Verifying  : php-7.4.28-1.0.1.el7.x86_64                                                                    8/10 
  Verifying  : httpd-tools-2.4.6-97.0.5.el7_9.4.x86_64                                                        9/10 
  Verifying  : php-common-7.4.28-1.0.1.el7.x86_64                                                            10/10 

Installed:
  php.x86_64 0:7.4.28-1.0.1.el7                       php-oci8-19c.x86_64 0:7.4.28-1.0.1.el7                      

Dependency Installed:
  apr.x86_64 0:1.4.8-7.el7                  apr-util.x86_64 0:1.5.2-6.0.1.el7 httpd.x86_64 0:2.4.6-97.0.5.el7_9.4
  httpd-tools.x86_64 0:2.4.6-97.0.5.el7_9.4 mailcap.noarch 0:2.1.41-2.el7     php-cli.x86_64 0:7.4.28-1.0.1.el7  
  php-common.x86_64 0:7.4.28-1.0.1.el7      php-pdo.x86_64 0:7.4.28-1.0.1.el7

Complete!
[opc@compute1 ~]$ 

Apacheの設定ファイル(/etc/httpd/conf/httpd.conf)を編集します。

sudo vi /etc/httpd/conf/httpd.conf

ファイルの末尾に以下の内容を追加して、拡張子がphpのファイルをapplication/x-httpd-phpハンドラーに割り当てます。

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

httpdを起動します。

[opc@compute1 ~]$ sudo systemctl start httpd.service

httpdの自動起動を設定します。

[opc@compute1 ~]$ sudo systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[opc@compute1 ~]

2.firewalldの設定

firewalldの設定を変更し、HTTP(TCP80番)ポートを開放します。

[opc@compute1 ~]$ sudo firewall-cmd --add-port=80/tcp --zone=public --permanent
success
[opc@compute1 ~]$ sudo firewall-cmd --reload
success
[opc@compute1 ~]$ sudo firewall-cmd --list-all --zone=public
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources: 
  services: dhcpv6-client ssh
  ports: 80/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	
[opc@compute1 ~]$ 

3.SElinuxの設定

SElinuxの設定を変更します。

[opc@comp1 ~]$ sudo setsebool -P httpd_can_network_connect_db=1
[opc@comp1 ~]$ 

4.アプリケーションで使用するDBユーザとアプリケーションデータの準備

SQL*PlusからadminユーザとしてAutnomous Databaseに接続し、アプリケーションで使用するユーザ"apuser"とアプリケーションで使用するテーブル"dept"を作成し、テーブル"dept"にデータを挿入します。

[opc@compute1 ~]$ sqlplus admin/Demo#1Demo#1@testatp1_high

SQL*Plus: Release 19.0.0.0.0 - Production on 水 2月 23 11:31:34 2022
Version 19.14.0.0.0

Copyright (c) 1982, 2021, Oracle.  All rights reserved.

最終正常ログイン時間: 水 2月  23 2022 11:14:25 +00:00


Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.1.0
に接続されました。
SQL> GRANT CONNECT,RESOURCE,UNLIMITED TABLESPACE TO apuser IDENTIFIED BY Test#1Test#1;

権限付与が成功しました。

SQL> CREATE TABLE apuser.dept (
  2  	deptno NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY,
  3  	dname VARCHAR2(14),
  4  	loc VARCHAR2(13)
  5  );

表が作成されました。

SQL> INSERT INTO apuser.dept VALUES (10,'ACCOUNTING','NEW YORK');

1行が作成されました。

SQL> INSERT INTO apuser.dept VALUES (20,'RESEARCH','DALLAS');

1行が作成されました。

SQL> INSERT INTO apuser.dept VALUES (30,'SALES','CHICAGO');

1行が作成されました。

SQL> INSERT INTO apuser.dept VALUES (40,'OPERATIONS','BOSTON');

1行が作成されました。

SQL> COMMIT;

コミットが完了しました。

SQL> exit
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.1.0との接続が切断されました。
[opc@compute1 ~]$ 

5.PHPアプリケーションの作成

apuserとしてAutonomous Databaseに接続し、dept表の内容を表示するPHPのWebアプリケーションを作成します。

[opc@comp1 ~]$ sudo vi /var/www/html/test.php
/var/www/html/test.php
<html>
<head><meta charset="UTF-8">
<title>Connect from PHP Application to Autonomous Database using TLS Connection without Wallet</title>
</head>
<body>
<?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $conn = oci_connect('apuser', 'Test#1Test#1', 'testatp1_high');
    $stid = oci_parse($conn, 'SELECT deptno,dname,loc FROM dept ORDER BY deptno');

    echo "<table border='1'>\n";
    echo "<caption>DEPT Table</caption>\n";
    echo "<tr><th>Dept No.</th><th>Dept Name</th><th>Location</th></tr>\n";

	oci_execute($stid);
    while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
        echo "<tr>\n";
        foreach ($row as $item) {
            echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&amp;nbsp;") . "</td>\n";
        }
        echo "</tr>\n";
    }
	oci_free_statement($stid);
	oci_close($conn);

	echo "</table><br>\n";
	echo "Response from ".gethostname();
?>
</body>
</html>

6.PHPアプリケーションの動作確認

作成したPHPアプリケーションにブラウザからアクセスしてみます。
スクリーンショット 2022-02-23 20.42.16.png

ウォレットを使用せずにPHPアプリケーションからAutonomous DatabaseにTLS認証で接続できることが確認できました。

参考情報

[OCI]ウォレットを使用せずにSQL*PlusからAutonomous DatabaseにTLS認証で接続してみた

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?