そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

【CentOS8】PHP7.2からOracle19cへ接続する【OCI8インストール手順】

※[PR]当ブログの記事の中にはプロモーションが含まれています。

PHP7.2からOracle19cへ接続する【OCI8インストール手順】

【CentOS8】PHP7.2+Apache2.4からOracle19cへ接続するためのOCI8インストール手順

CentOS8+PHP7.2+Apache2.4の環境で、Oracleデータベース(19c)へ接続するための設定方法を探している方へ。

CentOS8+PHP7.2+Apache2.4から、Oracle19cへ接続できるようにする方法を知りたい

CentOS8+PHP7.2+Apache2.4の環境から、別のサーバ上にあるOracleデータベース(19c)へ
接続するための方法を知りたい
、と考えていませんか?

CentOS8+PHP+Apacheの環境に「OCI8」をインストールして、別サーバ上にあるOracleへ接続できるようにする手順を紹介します

本記事では、下記の内容を解説します。

  1. Linuxサーバ用のOracle Instant Client をインストールする手順
  2. CentOS8+PHP7.2環境にOCI8を追加インストールする手順
  3. php.iniの編集手順
  4. PHPからOracle利用に必要な環境変数を設定する手順
  5. PHPからOracleへ接続して取得した値を表示するサンプルコードでテスト
  6. インフラエンジニアになるための勉強のやり方

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。
私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

前提条件(本記事の場合の環境)

本記事で設定したサーバ構成は以下です。

項目 データベースサーバ(Oracle) WEB-APサーバ(PHP+Apache)
OS Oralce Linux 7.7 CentOS 8.0
サーバ名 SV-PLUTO SV-NEPTUNE
DB Oracle 19c -
PDB FINDTPDB -
PHP - PHP7.2
WEB - Apache2.4

DBサーバと、WEB/APサーバの2台構成にしているシステムを想定しています。

CentOS8+PHP7.2+Apache2.4の環境構築は、下記の記事に記載した手順ですでに済ませた状態です。

souiunogaii.hatenablog.com

souiunogaii.hatenablog.com

souiunogaii.hatenablog.com

souiunogaii.hatenablog.com

Linuxサーバ用のOracle Instant Client をインストールする手順

CentOS8のPHPからOracleデータベースへ接続するためには、
Oracle Instant Client」のインストールが必要です。

これについては、下記の記事に紹介した手順ですでに実施済みの状態です。

souiunogaii.hatenablog.com

CentOS8+PHP7.2環境にOCI8を追加インストールする手順

PHPからOracleへ接続するための関数を使えるようにするには、
「OCI8」を追加インストールして設定する必要があります。

PHPからOracleに接続する関数を使えるように、「OCI8」をインストールします

参考リンク (PHPマニュアルのページ)

https://www.php.net/manual/ja/book.oci8.php

準備(php-pearphp-develのインストール)

既に構築済みのPHP環境に、OCI8を追加インストールするためには、 先に以下をインストールしておく必要があります。

php-pear インストール
dnf install php-pear
[root@sv-neptune ~]# dnf install php-pear
CentOS-8 - AppStream                                                                    9.2 kB/s | 4.3 kB     00:00
CentOS-8 - Base                                                                         8.6 kB/s | 3.8 kB     00:00
CentOS-8 - Extras                                                                       2.7 kB/s | 1.5 kB     00:00
依存関係が解決しました。
========================================================================================================================
 パッケージ             アーキテクチャー  バージョン                                         リポジトリー         サイズ
========================================================================================================================
インストール:
 php-pear               noarch            1:1.10.5-9.module_el8.1.0+209+03b9a8ff             AppStream            358 k
依存関係のインストール:
 php-process            x86_64            7.2.11-2.module_el8.1.0+209+03b9a8ff               AppStream             84 k

トランザクションの概要
========================================================================================================================
インストール  2 パッケージ

ダウンロードサイズの合計: 442 k
インストール済みのサイズ: 2.2 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/2): php-pear-1.10.5-9.module_el8.1.0+209+03b9a8ff.noarch.rpm                         2.3 MB/s | 358 kB     00:00
(2/2): php-process-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64.rpm                      392 kB/s |  84 kB     00:00
------------------------------------------------------------------------------------------------------------------------
合計                                                                                     80 kB/s | 442 kB     00:05
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                1/1
  インストール中   : php-process-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                        1/2
  インストール中   : php-pear-1:1.10.5-9.module_el8.1.0+209+03b9a8ff.noarch                                         2/2
  scriptletの実行中: php-pear-1:1.10.5-9.module_el8.1.0+209+03b9a8ff.noarch                                         2/2
  検証             : php-pear-1:1.10.5-9.module_el8.1.0+209+03b9a8ff.noarch                                         1/2
  検証             : php-process-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                        2/2

インストール済み:
  php-pear-1:1.10.5-9.module_el8.1.0+209+03b9a8ff.noarch     php-process-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64

完了しました!
php-devel インストール
dnf install php-devel
[root@sv-neptune ~]# dnf install php-devel
メタデータの期限切れの最終確認: 0:02:54 時間前の 20200321230627秒 に実施しました。
依存関係が解決しました。
========================================================================================================================
 パッケージ             アーキテクチャー   バージョン                                       リポジトリー          サイズ
========================================================================================================================
インストール:
 php-devel              x86_64             7.2.11-2.module_el8.1.0+209+03b9a8ff             AppStream             712 k
依存関係のインストール:
 pcre-cpp               x86_64             8.42-4.el8                                       BaseOS                 47 k
 pcre-devel             x86_64             8.42-4.el8                                       BaseOS                551 k
 pcre-utf16             x86_64             8.42-4.el8                                       BaseOS                195 k
 pcre-utf32             x86_64             8.42-4.el8                                       BaseOS                186 k

トランザクションの概要
========================================================================================================================
インストール  5 パッケージ

ダウンロードサイズの合計: 1.7 M
インストール済みのサイズ: 7.8 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/5): pcre-cpp-8.42-4.el8.x86_64.rpm                                                   516 kB/s |  47 kB     00:00
(2/5): pcre-devel-8.42-4.el8.x86_64.rpm                                                 3.0 MB/s | 551 kB     00:00
(3/5): pcre-utf32-8.42-4.el8.x86_64.rpm                                                 4.5 MB/s | 186 kB     00:00
(4/5): php-devel-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64.rpm                        2.4 MB/s | 712 kB     00:00
(5/5): pcre-utf16-8.42-4.el8.x86_64.rpm                                                 589 kB/s | 195 kB     00:00
------------------------------------------------------------------------------------------------------------------------
合計                                                                                    1.5 MB/s | 1.7 MB     00:01
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                1/1
  インストール中   : pcre-utf32-8.42-4.el8.x86_64                                                                   1/5
  インストール中   : pcre-utf16-8.42-4.el8.x86_64                                                                   2/5
  インストール中   : pcre-cpp-8.42-4.el8.x86_64                                                                     3/5
  インストール中   : pcre-devel-8.42-4.el8.x86_64                                                                   4/5
  インストール中   : php-devel-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                          5/5
  scriptletの実行中: php-devel-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                          5/5
  検証             : php-devel-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                          1/5
  検証             : pcre-cpp-8.42-4.el8.x86_64                                                                     2/5
  検証             : pcre-devel-8.42-4.el8.x86_64                                                                   3/5
  検証             : pcre-utf16-8.42-4.el8.x86_64                                                                   4/5
  検証             : pcre-utf32-8.42-4.el8.x86_64                                                                   5/5

インストール済み:
  php-devel-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64   pcre-cpp-8.42-4.el8.x86_64     pcre-devel-8.42-4.el8.x86_64
  pcre-utf16-8.42-4.el8.x86_64                            pcre-utf32-8.42-4.el8.x86_64

完了しました!

OCI8の追加インストール

pecl(ピクル)」という、PHP拡張機能をインストールするコマンドを使います。

pecl install oci8
[root@sv-neptune ~]# pecl install oci8
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading oci8-2.2.0.tgz ...
Starting to download oci8-2.2.0.tgz (196,449 bytes)
.........................................done: 196,449 bytes
11 source files, building
running: phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client [autodetect] :

途中で
「Please provide the path to the ORACLE_HOME directory.
Use 'instantclient,/path/to/instant/client/lib'
if you're compiling with Oracle Instant Client [autodetect] :」
という部分がありますが、今回はすでにOracle Instant Clientをインストール済なので
「autodetect」はそのまま何も入力せずEnterキー押下しました

building in /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0
running: /var/tmp/oci8/configure --with-php-config=/usr/bin/php-config --with-oci8
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib
checking for PHP extension directory... /usr/lib64/php/modules
checking for PHP installed headers prefix... /usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking for Oracle Database OCI8 support... yes, shared
checking PHP version... 7.2.11, ok
checking OCI8 DTrace support... no
checking size of long int... 8
checking checking if we're on a 64-bit platform... yes
configure: WARNING: OCI8 extension: ORACLE_HOME is not set, looking for default Oracle Instant Client instead
checking Oracle Instant Client directory... /usr/lib/oracle/19.6/client64/lib
checking Oracle Instant Client SDK header directory... /usr/include/oracle/19.6/client64
checking Oracle Instant Client library version compatibility... 19.1
checking how to print strings... printf
checking for a sed that does not truncate output... (cached) /usr/bin/sed
checking for fgrep... /usr/bin/grep -F
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking for gawk... (cached) gawk
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for sysroot... no
checking for a working dd... /usr/bin/dd
checking how to truncate binary pipes... /usr/bin/dd bs=4096 count=1
checking for mt... no
checking if : is a manifest tool... no
checking for dlfcn.h... yes
checking for objdir... .libs
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC -DPIC
checking if cc PIC flag -fPIC -DPIC works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking if cc supports -c -o file.o... (cached) yes
checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
configure: creating ./config.status
config.status: creating config.h
config.status: executing libtool commands
running: make
/bin/sh /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/libtool --mode=compile cc  -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64  -DHAVE_CONFIG_H  -g -O2   -c /var/tmp/oci8/oci8.c -o oci8.lo
libtool: compile:  cc -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64 -DHAVE_CONFIG_H -g -O2 -c /var/tmp/oci8/oci8.c  -fPIC -DPIC -o .libs/oci8.o
/bin/sh /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/libtool --mode=compile cc  -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64  -DHAVE_CONFIG_H  -g -O2   -c /var/tmp/oci8/oci8_lob.c -o oci8_lob.lo
libtool: compile:  cc -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64 -DHAVE_CONFIG_H -g -O2 -c /var/tmp/oci8/oci8_lob.c  -fPIC -DPIC -o .libs/oci8_lob.o
/bin/sh /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/libtool --mode=compile cc  -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64  -DHAVE_CONFIG_H  -g -O2   -c /var/tmp/oci8/oci8_statement.c -o oci8_statement.lo
libtool: compile:  cc -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64 -DHAVE_CONFIG_H -g -O2 -c /var/tmp/oci8/oci8_statement.c  -fPIC -DPIC -o .libs/oci8_statement.o
/bin/sh /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/libtool --mode=compile cc  -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64  -DHAVE_CONFIG_H  -g -O2   -c /var/tmp/oci8/oci8_collection.c -o oci8_collection.lo
libtool: compile:  cc -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64 -DHAVE_CONFIG_H -g -O2 -c /var/tmp/oci8/oci8_collection.c  -fPIC -DPIC -o .libs/oci8_collection.o
/bin/sh /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/libtool --mode=compile cc  -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64  -DHAVE_CONFIG_H  -g -O2   -c /var/tmp/oci8/oci8_interface.c -o oci8_interface.lo
libtool: compile:  cc -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64 -DHAVE_CONFIG_H -g -O2 -c /var/tmp/oci8/oci8_interface.c  -fPIC -DPIC -o .libs/oci8_interface.o
/bin/sh /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/libtool --mode=compile cc  -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64  -DHAVE_CONFIG_H  -g -O2   -c /var/tmp/oci8/oci8_failover.c -o oci8_failover.lo
libtool: compile:  cc -I. -I/var/tmp/oci8 -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64 -DHAVE_CONFIG_H -g -O2 -c /var/tmp/oci8/oci8_failover.c  -fPIC -DPIC -o .libs/oci8_failover.o
/bin/sh /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/libtool --mode=link cc -DPHP_ATOM_INC -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/include -I/var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/main -I/var/tmp/oci8 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/oracle/19.6/client64  -DHAVE_CONFIG_H  -g -O2   -o oci8.la -export-dynamic -avoid-version -prefer-pic -module -rpath /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/modules  oci8.lo oci8_lob.lo oci8_statement.lo oci8_collection.lo oci8_interface.lo oci8_failover.lo -Wl,-rpath,/usr/lib/oracle/19.6/client64/lib -L/usr/lib/oracle/19.6/client64/lib -lclntsh
libtool: link: cc -shared  -fPIC -DPIC  .libs/oci8.o .libs/oci8_lob.o .libs/oci8_statement.o .libs/oci8_collection.o .libs/oci8_interface.o .libs/oci8_failover.o   -L/usr/lib/oracle/19.6/client64/lib -lclntsh  -g -O2 -Wl,-rpath -Wl,/usr/lib/oracle/19.6/client64/lib   -Wl,-soname -Wl,oci8.so -o .libs/oci8.so
libtool: link: ( cd ".libs" && rm -f "oci8.la" && ln -s "../oci8.la" "oci8.la" )
/bin/sh /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/libtool --mode=install cp ./oci8.la /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/modules
libtool: install: cp ./.libs/oci8.so /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/modules/oci8.so
libtool: install: cp ./.libs/oci8.lai /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/modules/oci8.la
libtool: finish: PATH="/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/sbin" ldconfig -n /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /var/tmp/pear-build-rootlCMdIb/oci8-2.2.0/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

running: make INSTALL_ROOT="/var/tmp/pear-build-rootlCMdIb/install-oci8-2.2.0" install
Installing shared extensions:     /var/tmp/pear-build-rootlCMdIb/install-oci8-2.2.0/usr/lib64/php/modules/
running: find "/var/tmp/pear-build-rootlCMdIb/install-oci8-2.2.0" | xargs ls -dils
17998429   0 drwxr-xr-x 3 root root     17  321 23:10 /var/tmp/pear-build-rootlCMdIb/install-oci8-2.2.0
 8909753   0 drwxr-xr-x 3 root root     19  321 23:10 /var/tmp/pear-build-rootlCMdIb/install-oci8-2.2.0/usr
17998430   0 drwxr-xr-x 3 root root     17  321 23:10 /var/tmp/pear-build-rootlCMdIb/install-oci8-2.2.0/usr/lib64
26035127   0 drwxr-xr-x 3 root root     21  321 23:10 /var/tmp/pear-build-rootlCMdIb/install-oci8-2.2.0/usr/lib64/php
  476055   0 drwxr-xr-x 2 root root     21  321 23:10 /var/tmp/pear-build-rootlCMdIb/install-oci8-2.2.0/usr/lib64/php/modules
  476056 876 -rwxr-xr-x 1 root root 893128  321 23:10 /var/tmp/pear-build-rootlCMdIb/install-oci8-2.2.0/usr/lib64/php/modules/oci8.so

Build process completed successfully
Installing '/usr/lib64/php/modules/oci8.so'
install ok: channel://pecl.php.net/oci8-2.2.0
configuration option "php_ini" is not set to php.ini location
You should add "extension=oci8.so" to php.ini

出力されている内容をじっくり見ると、自動的に、
「configure」「make」「make install」が実行されて、
「/usr/lib64/php/modules/oci8.so」というファイルが作成されているみたいです

php.iniの編集手順

OCI8インストール時に出力されたメッセージの一番最後に、以下が書かれていました。

configuration option "php_ini" is not set to php.ini location
You should add "extension=oci8.so" to php.ini

これに従って、OCI8をインストールした後には、php.iniの編集が必要です。
(1行追記するだけです。)

vi /etc/php.ini
extension=oci8.so

php.ini編集後に一度、httpdを再起動してから、phpinfo()で確認

systemctl restart httpd
systemctl status httpd
[root@sv-neptune php.d]# systemctl restart httpd
[root@sv-neptune php.d]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           mqphp-fpm.conf
   Active: active (running) since Sat 2020-03-21 23:20:46 JST; 7s ago
     Docs: man:httpd.service(8)
 Main PID: 8807 (httpd)
   Status: "Started, listening on: port 80"
    Tasks: 213 (limit: 11109)
   Memory: 24.5M
   CGroup: /system.slice/httpd.service
           tq8807 /usr/sbin/httpd -DFOREGROUND
           tq8809 /usr/sbin/httpd -DFOREGROUND
           tq8810 /usr/sbin/httpd -DFOREGROUND
           tq8811 /usr/sbin/httpd -DFOREGROUND
           mq8812 /usr/sbin/httpd -DFOREGROUND

 321 23:20:46 sv-neptune systemd[1]: Stopped The Apache HTTP Server.
 321 23:20:46 sv-neptune systemd[1]: Starting The Apache HTTP Server...
 321 23:20:46 sv-neptune systemd[1]: Started The Apache HTTP Server.
 321 23:20:46 sv-neptune httpd[8807]: Server configured, listening on: port 80

phpinfoでOCI8が反映されたことを確認します。

php -r 'phpinfo();'
[root@sv-neptune php.d]# php -r 'phpinfo();'
phpinfo()
PHP Version => 7.2.11
(途中省略)
oci8

OCI8 Support => enabled
OCI8 DTrace Support => disabled
OCI8 Version => 2.2.0
Oracle Run-time Client Library Version => 19.6.0.0.0
Oracle Compile-time Instant Client Version => 19.6

Directive => Local Value => Master Value
oci8.connection_class => no value => no value
oci8.default_prefetch => 100 => 100
oci8.events => Off => Off
oci8.max_persistent => -1 => -1
oci8.old_oci_close_semantics => Off => Off
oci8.persistent_timeout => -1 => -1
oci8.ping_interval => 60 => 60
oci8.privileged_connect => Off => Off
oci8.statement_cache_size => 20 => 20

Statistics =>
Active Persistent Connections => 0
Active Connections => 0
(以下省略)

PHPからOracle利用に必要な環境変数を設定する手順

  • Oracle Instant Clientのインストール
  • OCI8のインストール、
  • php.iniの編集

をしただけでは、まだ、PHPからOracleへ接続するためのoci関数が使えません。

Oracleを利用するための環境変数を、Apacheの実行ユーザー(今回の環境では、www)に設定する必要があります。

vi /home/www/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then

fi

# User specific environment and startup programs
export NLS_LANG=Japanese_Japan.AL32UTF8
export LD_LIBRARY_PATH=/usr/lib/oracle/19.6/client64/lib:$LD_LIBRARY_PATH
  • NLS_LANG=Japanese_Japan.AL32UTF8
  • LD_LIBRARY_PATH=/usr/lib/oracle/19.6/client64/lib:$LD_LIBRARY_PATH

この環境変数の設定がされてないと、
「Call to undefined function oci_connect()」のようなエラーが出て、
ブラウザで表示しても500エラーになってしまいます

Got error 'PHP message: 
PHP Fatal error:  
Uncaught Error: 
Call to undefined function oci_connect() in ...

「Call to undefined function oci_connect()」エラーの原因が、Oracle用の環境変数だとすぐに分からずに、結構悩んでしまいました。環境変数を設定して一度再起動したらすぐ解消しました。

環境変数の設定後、一度、サーバー再起動

reboot

ocitest.php というファイルをサーバ上に作成して、下記のテスト用コードを書いて表示してみます。

<?php
    phpinfo();
?>

oci8というセクションが表示されるようになりました。

PHPからOracleへ接続して取得した値を表示するサンプルコードでテスト

下記のようなサンプルコードで、実際にOracleデータベースへ接続して、
dualテーブルからsysdateを取得して表示させてみます。

<?php

  $con = oci_connect('●●●ユーザー名','●●●パスワード','●●●データベース名');
  
  $sql = "select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual";

  echo 'SQL = ' . $sql . '<br>';

  $stmt = oci_parse($con, $sql);

  $rslt = oci_execute($stmt);

  $row = oci_fetch_row($stmt);

  echo '結果 = ' . $row[0];

  oci_close($con);

?>

上手く表示できました

まとめ

CentOS8+PHP+Apacheの環境に「OCI8」をインストールして、別サーバ上にあるOracleへ接続できるようにする手順を紹介しました

  1. Linuxサーバ用のOracle Instant Client をインストール
  2. CentOS8+PHP7.2環境にOCI8を追加インストール(pecl install oci8)
  3. php.ini に「extension=oci8.so」を追記
  4. PHPからOracle利用に必要な環境変数を設定(NLS_LANG, LD_LIBRARY_PATH)
  5. PHPからOracleへ接続して取得した値を表示するサンプルコードでテスト

参考リンク

https://interest-speaker.com/cooperation-of-php-and-oracle-db/interest-speaker.com

https://www.php.net/manual/ja/book.oci8.php

qiita.com

インフラエンジニアになるための勉強のやり方

私は、某SIerに勤務しながら、WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

現在の会社で直接担当している業務の中では、決まった技術しか触れないけれど、
「他にも将来役に立ちそうなインフラ技術を勉強したい」「働きながらでもできる効率的なインフラエンジニアの勉強方法を知りたい」と考えている方のために、
①おすすめの書籍と、②おススメのオンライン学習サイト、③おすすめのエンジニアスクールを紹介します。

働きながら勉強するための、おススメの書籍と、オンラインで学習できるサイトの紹介です。

インフラエンジニアの勉強ができるおススメ書籍

朝の通勤時や帰宅時の電車の中や、社外打合せへの移動中などに勉強するなら、やはり書籍が一番だと思います。

インフラエンジニアの教科書

LINE社に創業時から在籍しているインフラエンジニア・プロマネの「佐野裕」さんの本です。 twitter.com

膨大なトラフィックをさばくLINE社にて構築・保守を行っている現役エンジニアが記すITインフラの必須知識と経験の数々! サーバ、OS、ネットワーク機器、データセンター、購買と商談、障害対応、大規模インフラ、成長するために必要なスキルなど、基礎知識から実践まで言及。 ITインフラの世界が理解できる珠玉の1冊!

インフラエンジニアの教科書
(C&R研究所)
佐野裕

1週間でLPICの基礎が学べる本

Linuxの資格「LPIC」の試験対策を初心者向けに書いた入門書です。

Linuxの資格として広く知られているLPICですが、あまり知識の無いまま試験対策を始める人も多いのではないでしょうか。しかし、試験対策書は試験範囲についてのみ解説しているものが多く、初心者が理解するのは困難です。本書は、初心者がスムーズに試験対策を行えるよう、事前に基礎固めを行うLinux入門書です。試験情報や模擬問題も掲載しているので、資格取得を視野に入れた効率的な基礎学習が行えます。

1週間でLPICの基礎が学べる本
第3版 徹底攻略シリーズ
(インプレス)
中島能和

インフラエンジニアの勉強ができるオンライン学習サイト

Udemy (オンライン動画学習サイト)

Udemy(ユーデミー)は、オンラインで動画で学習できるサービスです。
ITエンジニア系の講座もたくさん公開されています。

≫Udemy 世界最大級のオンライン学習サイト

Udemyの特徴
  • ユーザー登録すれば、第1回の講座は無料で視聴できる
  • 無料のコンテンツもある
  • 過去に受講した人の人数や、受講した人の評価も表示されるので選びやすい

≫Udemy 世界最大級のオンライン学習サイト

開発の人気オンラインコース

インフラエンジニアの勉強ができる専門スクール

リナックスアカデミー(ITエンジニア専門スクール)

スクールというと、昼間働いている人には難しいかもしれませんが、
以下に紹介するスクールには、
Linuxサーバとネットワークとを両分野ともに学べるコースが、
夜間・土日での5か月~7か月の期間で用意されています。

リナックスアカデミー(ITエンジニア専門スクール) http://www.linuxacademy.ne.jp

リナックスアカデミーの特徴
  • 初心者でも最短4週間でエンジニアになれる「IT教育専門機関」
  • 100%ライブ抗議の「少人数クラス担任制」
  • 実務スキルを身につける「PBL(Project Based Learning)」が充実
  • 圧倒的な合格実績と「合格保証制度」
  • 就職実績と「就職・転職サポート」が充実

リナックスアカデミーが選ばれ続ける最大の理由、それはIT技術に特化した日本初のIT教育専門機関だからということです。他を寄せ付けないカリキュラムと講師の質で日本No.1のオープンソース専門スクールだと認知されています。 スクールとしては世界で初めてLPI(Linux技術者認定機関)プラチナスポンサーに加盟し、LPI-Jアカデミック認定校第一号スクール、OSS-DBアカデミック認定校第一号スクール、PostgreSQL CE認定校、国内唯一のPHP技術者育成認定校Zend Authorized Training Center など、数々の認定を受けています。 先日来日したLPIC資格試験の開発責任者であるMatthew Rice氏にも、「世界でNo.1と言われる日本の市場で、リナックスアカデミーはNo.1のスクールだと言われている。それは素晴らしいことだ」という言葉を頂いています。

リナックスアカデミー(ITエンジニア専門スクール)の資料請求(無料)について詳しく見てみる

以上、読んでいただきありがとうございました。