DatabaseLinuxOraclePHPProgrammingServerTutorial

Connect oracle database ke php di server Redhat/CentOS/Fedora

1154
oracle-php-oci
oracle-php-oci

oracle-php-oci

Di posting ini, saya akan mencoba menjelaskan bagaimana cara menghubungkan database Oracle dari PHP di server Redhat/ CentOS/Fedora, Secara step by step. Cara yang paling mudah mengkonfigurasi php untuk mengakses database oracle dengan menggunakan Oracle Instant Client. Tahapannya mulai dari cara menginstall PHP dengan OCI8 Extension dan cara menginstall Oracle Instant Client di Server Redhat/CentOS/Fedora. OCI8 sendiri adalah ekstensi PHP untuk menghubungkan ke Oracle Database. Yang perlu di Install yaitu : oracle-instant-client dan php oci8 extension. Saya asumsikan PHP dan pecl sudah terinstall di server.

Tahap Pertama :

Periksa arsitektur server dan versi OS Anda :

# uname -a
Linux localhost 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST
2011 x86_64 x86_64 x86_64 GNU/Linux

el6 berarti itu adalah perusahaan linux 6 dan x86_64 berarti itu adalah mesin 64 bit. Untuk mesin 32 bit itu adalah i386.

Tahap kedua  :Download dua RPM paket:

  1. oracle-instantclient basic
  2. oracle-instantclient devel

 Download Instant client di sini

1. Instant Client Linux x86-64(64-bit)

Instant Client Linux x86-64(64-bit)

2. Instant Client Devel Linux x86-64(64-bit) dengan Gulir ke bawah dan download paket instant instant  devel rpm.

Instant Client Devel Linux x86-64(64-bit)

Tahap Ketiga : Instal RPM di root user

# rpm -Uvh oracle-instantclient12.1-basic-12.2.0.1.0-1.x86_64.rpm
Preparing...                ########################################### [100%]
   1:oracle-instantclient12.########################################### [100%]
# rpm -Uvh oracle-instantclient12.1-devel-12.2.0.1.0-1.x86_64.rpm
Preparing...                ########################################### [100%]
   1:oracle-instantclient12.########################################### [100%]

Instant client atau file hasil instalasi di simpan secara default di   /usr/lib/oracle/12.2/client64/ untuk OS 64 bit sedangkan untuk OS 32 bit tersimpan di  /usr/lib/oracle/12.1/client/

Tahap Empat : Set environment variables ORACLE_HOME dan LD_LIBRARY_PATH

# ORACLE_HOME=/usr/lib/oracle/12.2/client64; export ORACLE_HOME
# LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH

Anda juga harus menambahkan dua baris di atas di file ~ / .bash_profile. Edit file dengan vim editor dan tambahkan baris di atas PATH = $ PATH: $ HOME / bin

#vim  ~/.bash_profile

Setelah diedit file akan menjadi :

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

# User specific environment and startup programs

ORACLE_HOME=/usr/lib/oracle/12.2/client64; export ORACLE_HOME
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
PATH=$PATH:$HOME/bin

export PATH

Tahap kelima : Periksa paket php-pear dan php-devel yang diinstal oleh perintah ini. Anda harus menginstal paket ini

# rpm -qa | grep -i php
php-5.3.3-3.el6_1.3.x86_64
php-cli-5.3.3-3.el6_1.3.x86_64
php-devel-5.3.3-3.el6_1.3.x86_64
php-common-5.3.3-3.el6_1.3.x86_64
php-mysql-5.3.3-3.el6_1.3.x86_64
php-soap-5.3.3-3.el6_1.3.x86_64
php-pear-1.9.4-4.el6.noarch
php-gd-5.3.3-3.el6_1.3.x86_64

Tahap ke-Enam : Install ektensi oci8

# pecl install oci8

Setelah itu anda akan ditanya tempat penyampanan direktori dari instant client seperti berikut : kemudian masukan alamatnya instant client :

instantclient,/usr/lib/oracle/12.2/client64/lib

Saat proses dijalankan :

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] :
instantclient,/usr/lib/oracle/12.2/client64/lib

Setelah menekan enter, pada bagian akhir anda akan melihat baris “Build process completed successfully”

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

Tambahkan “extension = oci8.so” ke file php.ini tepat di bawah baris [PHP] dan sepertinya

[PHP]
extension=oci8.so
;;;;;;;;;;;;;;;;;;;
; About php.ini   ;
;;;;;;;;;;;;;;;;;;;
; PHP's initialization file, generally called php.ini, is responsible for
; configuring many of the aspects of PHP's behavior.

Tahap ke-Tujuh : Restart Web Server(Apache or Nginx)

# /etc/init.d/httpd restart

Sekarang cek oci8 telah terinstal , karena oci8 sudah diaktifkan.

# echo "<?php print phpinfo();?>" | php | grep -i oci
oci8
OCI8 Support => enabled
OCI8 DTrace Support => disabled
OCI8 Version => 2.0.6
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

Sekarang Anda telah menginstal oci8 dan Anda dapat menulis kode untuk menghubungkan ke Oracle Database. Berikut adalah contoh Kode PHP

<?php

$conn = oci_connect('test_user', 'test_password', 'mymachine.mydomain/orcl');

$stid = oci_parse($conn, 'select table_name from user_tables');
oci_execute($stid);

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

?>
test_user is user of the  Database
test_password is the password of the database
mymachine.mydomain is the hostname.domain of the Database. You can put IP directly here.
orcl is the SID of the Database.

Prosedur di atas mengenai “Connect Oracle Database dari PHP ke server Redhat / CentOS  / Fedora “.

Sekian tutorial kali ini. semoga bermanfaat 🙂

Exit mobile version
Verified by MonsterInsights