oracledba.help

Apache, PHP7 and Oracle 19c

<- SpecialTopics

Overview

Essential steps to configure an Oracle Linux system to use PHP to interact with Oracle databases.

Process

Disable Firewalls and SELinux

Firewalls

 # systemctl disable firewalld; 
 # systemctl stop firewalld; 
 # service iptables stop; 
 # chkconfig iptables off; 
 # systemctl status firewalld

On one line: systemctl disable firewalld; systemctl stop firewalld; service iptables stop; chkconfig iptables off; systemctl status firewalld SELinux

 vi /etc/selinux/config
   Set SELINUX=disabled

Enable Oracle Database Repo and Run Oracle Preinstall

 1. vi /etc/yum.repos.d/public-yum-ol7.repo
    Find ol7_addons
    Set enabled=1
 2. yum install oracle-database-preinstall-19c -y

To stop packagekit: systemctl stop packagekit; systemctl disable packagekit; echo "OK"

Install Oracle Instant Client and SQLPLus

 yum -y install oracle-release-el7

    If IMPORTANT: A legacy Oracle Linux yum server repo file was found.
    Run: /usr/bin/ol_yum_configure.sh

 yum -y install oracle-instantclient19.5-basic 
 yum -y install oracle-instantclient19.5-sqlplus

Install PHP and OCI8

 yum install -y oracle-php-release-el7
 yum -y install php php-oci8-19c

Apache & PHP Actions

 -- Init\Test Apache Web Server
 systemctl start httpd.service
 systemctl enable httpd.service
 http://localhost

 -- Create\Test PHP Info Page
 vi /var/www/html/pi.php
 <?php phpinfo(); ?>
 http://localhost/pi.php

 -- Create Oracle Test Page
 gedit /var/www/html/oracle_test.php

Code Example: oracle_test.php

 <?php
  // Ensure All Errors Displayed on Page
  error_reporting(E_ALL);
  ini_set('display_errors', '1');

  // Connect to Oracle Database
  $connection = oci_connect("scott", "tiger","10.230.0.42/MYDB");

  // Create Cursor
  $cursor = oci_parse($connection, "SELECT sysdate FROM dual");
  oci_execute ($cursor);

  // Output
  while ($row = oci_fetch_array ($cursor)) {
     // Two ways to output the same value of a field.
     // echo $row[0] . " ";
     echo $row['SYSDATE'] . " ";
  }

  // Close Connection
  oci_close($connection);
 ?>

Init Application Dev Environment

Create User (appdev)

  su -
  useradd --gid apache --groups apache appdev
  passwd appdev

Init Files

 cd /var/www/html
 chown -R appdev:apache *.*
 chown -R appdev:apache .

Shell Entries

 # AppDev
 alias apps='cd /var/www/html/apps;pwd'
 alias dev='cd /var/www/html/dev;pwd'
 alias html='cd /var/www/html;pwd'
 alias kw='/usr/bin/kwrite' &

SYSDBA Connection Example

  1. Edit /etc/php.d/20-oci8.ini
  2. Set oci8.privileged_connect = On
  3. Bounce web server: systemctl restart httpd.service

Code Example

 $connection = oci_connect("sys","********","MySrv/MyDB",null,OCI_SYSDBA);

.bashrc Changes

# Aliases - Common
alias cl='clear;crontab -l'
alias l9='ls -alt | head -9'
alias l20='ls -alt | head -20'
alias l50='ls -alt | head -50'
alias oslog='tail -f /var/log/messages'
alias tf='date;ls -l|wc -l'

# Dev
alias apps='cd /var/www/html/apps;pwd'
alias dev='cd /var/www/html/dev;pwd'
alias html='cd /var/www/html;pwd'
alias kw='/usr/bin/kwrite' &
alias alog='tail -f /var/log/httpd/error_log'
  • For oslog: chmod 644 /var/log/messages
  • For alog (apache error log): chmod 777 /var/log/httpd

oci_connect() Timeout

If your oci_connect() commands take too long to timeout you can change it by editing the sqlnet.ora.

Change Session as root,

  1. vi /usr/lib/oracle/19.5/client64/lib/network/admin/sqlnet.ora
  2. TCP.CONNECT_TIMEOUT=5
  3. systemctl stop httpd.service
  4. systemctl start httpd.service

The default for TCP.CONNECT_TIMEOUT is 60 seconds.

Common Timeout Message:

ORA-12170: TNS:Connect timeout occurred error.

Misc

  • systemctl stop packagekit; systemctl disable packagekit; echo "OK"
  • Restart Apache: systemctl restart httpd.service
  • PHP Error Log: tail -f /var/log/httpd/error_log
  • tnsnames.ora: gedit /usr/lib/oracle/19.5/client64/lib/network/admin/tnsnames.ora
  • TNS_ADMIN: TNS_ADMIN="/usr/lib/oracle/19.5/client64/lib/network/admin"

which sqlplus

 /usr/bin/sqlplus
 /usr/lib/oracle/19.5/client64/bin

find / -name admin -print

 /usr/lib/oracle/19.5/client64/lib/network/admin

References