
・PHP8.4+Apache2.4のWEBサーバーからOracle23aiDBサーバーへ接続するためにOCI8を設定する方法を知りたい。
・具体的な手順を教えてほしい
こういった疑問に答えます。
本記事の内容
- PHP8.4+Apache2.4のWEBサーバーからOracle23aiDBサーバーへ接続する(OCI8)手順

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。
こういった私が、解説していきます。
私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。
PHP8.4+Apache2.4のWEBサーバーからOracle23aiDBサーバーへ接続する(OCI8)手順
前回の記事
OCI8インストール
pecl install oci8
[root@a-webap01 ~]# pecl install oci8 WARNING: channel "pecl.php.net" has updated its protocols downloading oci8-3.4.0.tgz ... Starting to download oci8-3.4.0.tgz (194 .........................................done: 194 13 source files running: phpize Configuring for: PHP Version: 8.4 PHP Api Version: 20240924 Zend Module Api No: 20240924 Zend Extension Api No: 420240924 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] : ←★★★空でEnter building in /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.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 build system type... x86_64-pc-linux-gnu checking host system type... x86_64-pc-linux-gnu checking target system type... x86_64-pc-linux-gnu checking for gawk... gawk checking for pkg-config... /usr/bin/pkg-config checking pkg-config is at least version 0.9.0... yes 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 for system library directory... lib checking if compiler supports -Wl 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 debugging is enabled... no checking if PHP is built with thread safety (ZTS)... no Configuring extension checking for Oracle Database OCI8 support... yes checking PHP version... 8.4.12 checking OCI8 DTrace support... no checking size of long... 8 checking if we're at 64-bit platform... yes configure: WARNING: OCI8 extension: ORACLE_HOME is not set checking Oracle Instant Client directory... /usr/lib/oracle/23/client64/lib checking Oracle Instant Client SDK header directory... /usr/include/oracle/23/client64 checking Oracle Instant Client library version compatibility... 23.1 Configuring libtool 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-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noop checking how to convert x86_64-pc-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 Generating files configure: creating build directories configure: creating Makefile configure: patching config.h.in configure: creating ./config.status config.status: creating config.h config.status: executing libtool commands running: make /bin/sh /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/libtool --tag=CC --mode=compile cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8.c -o oci8.lo -MMD -MF oci8.dep -MT oci8.lo libtool: compile: cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8.c -MMD -MF oci8.dep -MT oci8.lo -fPIC -DPIC -o .libs/oci8.o /bin/sh /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/libtool --tag=CC --mode=compile cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_lob.c -o oci8_lob.lo -MMD -MF oci8_lob.dep -MT oci8_lob.lo libtool: compile: cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_lob.c -MMD -MF oci8_lob.dep -MT oci8_lob.lo -fPIC -DPIC -o .libs/oci8_lob.o /bin/sh /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/libtool --tag=CC --mode=compile cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_statement.c -o oci8_statement.lo -MMD -MF oci8_statement.dep -MT oci8_statement.lo libtool: compile: cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_statement.c -MMD -MF oci8_statement.dep -MT oci8_statement.lo -fPIC -DPIC -o .libs/oci8_statement.o /bin/sh /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/libtool --tag=CC --mode=compile cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_collection.c -o oci8_collection.lo -MMD -MF oci8_collection.dep -MT oci8_collection.lo libtool: compile: cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_collection.c -MMD -MF oci8_collection.dep -MT oci8_collection.lo -fPIC -DPIC -o .libs/oci8_collection.o /bin/sh /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/libtool --tag=CC --mode=compile cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_interface.c -o oci8_interface.lo -MMD -MF oci8_interface.dep -MT oci8_interface.lo libtool: compile: cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_interface.c -MMD -MF oci8_interface.dep -MT oci8_interface.lo -fPIC -DPIC -o .libs/oci8_interface.o /bin/sh /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/libtool --tag=CC --mode=compile cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_failover.c -o oci8_failover.lo -MMD -MF oci8_failover.dep -MT oci8_failover.lo libtool: compile: cc -I. -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/oci8/oci8_failover.c -MMD -MF oci8_failover.dep -MT oci8_failover.lo -fPIC -DPIC -o .libs/oci8_failover.o /bin/sh /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/libtool --tag=CC --mode=link cc -shared -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/23/client64 -DHAVE_CONFIG_H -g -O2 -D_GNU_SOURCE -o oci8.la -export-dynamic -avoid-version -prefer-pic -module -rpath /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/modules oci8.lo oci8_lob.lo oci8_statement.lo oci8_collection.lo oci8_interface.lo oci8_failover.lo -Wl 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/23/client64/lib -lclntsh -g -O2 -Wl libtool: link: ( cd ".libs" && rm -f "oci8.la" && ln -s "../oci8.la" "oci8.la" ) /bin/sh /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/libtool --tag=CC --mode=install cp ./oci8.la /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/modules libtool: install: cp ./.libs/oci8.so /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/modules/oci8.so libtool: install: cp ./.libs/oci8.lai /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/modules/oci8.la libtool: finish: PATH="/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin" ldconfig -n /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/modules ---------------------------------------------------------------------- Libraries have been installed in: /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/oci8-3.4.0/modules If you ever happen to want to link against installed libraries in a given directory specify the full pathname of the library 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 - have your system administrator add LIBDIR to '/etc/ld.so.conf' See any operating system documentation about shared libraries for more information ---------------------------------------------------------------------- Build complete. Don't forget to run 'make test'. running: make INSTALL_ROOT="/var/tmp/pear-build-roote5745rhgrfcg56cHlfz/install-oci8-3.4.0" install Installing shared extensions: /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/install-oci8-3.4.0/usr/lib64/php/modules/ running: find "/var/tmp/pear-build-roote5745rhgrfcg56cHlfz/install-oci8-3.4.0" | xargs ls -dils 33719367 0 drwxr-xr-x 3 root root 17 9月 4 16:32 /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/install-oci8-3.4.0 4996 0 drwxr-xr-x 3 root root 19 9月 4 16:32 /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/install-oci8-3.4.0/usr 16811845 0 drwxr-xr-x 3 root root 17 9月 4 16:32 /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/install-oci8-3.4.0/usr/lib64 33719369 0 drwxr-xr-x 3 root root 21 9月 4 16:32 /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/install-oci8-3.4.0/usr/lib64/php 51998193 0 drwxr-xr-x 2 root root 21 9月 4 16:32 /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/install-oci8-3.4.0/usr/lib64/php/modules 51998194 704 -rwxr-xr-x 1 root root 717144 9月 4 16:32 /var/tmp/pear-build-roote5745rhgrfcg56cHlfz/install-oci8-3.4.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-3.4.0 configuration option "php_ini" is not set to php.ini location You should add "extension=oci8.so" to php.ini
追加されたモジュール

php.iniに追記
/etc/php.ini
1632 [ffi] 1633 ; see /etc/php.d/20-ffi.ini 1634 1635 1636 1637 extension=oci8.so ←★★★追記
Apache再起動
[root@a-webap01 ~]# systemctl restart httpd
phpinfoの確認

Apacheの環境変数の追加
/etc/httpd/conf/httpd.conf
SetEnv NLS_LANG Japanese_Japan.AL32UTF8 SetEnv LD_LIBRARY_PATH /usr/lib/oracle/23/client64/lib:$LD_LIBRARY_PATH

この設定が足りないと,以下のようなエラーがでます。
AH01071: Got error 'PHP message: PHP Fatal error: Uncaught Error: Call to undefined function oci_connect() in
テストページで動作確認
<?php error_reporting(E_ALL); ini_set('display_errors', 1); $conn = oci_connect("findtuser01", "★★★パスワード", "a-oracl01.blue-planet.internal/FINDTPDB"); if (!$conn) { $e = oci_error(); var_dump($e); } else { echo "Connected!\n"; oci_close($conn); }
やったぜ!
