oracledba.help
SpecialTopics

Apache, PHP7 and Oracle 19c

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 & Run Oracle Test Page
 gedit /var/www/html/oracle_test.php
 Run: localhost/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'

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);

httpdDiag.sh

#!/bin/bash

MEM_LIMIT=$(free | grep -i mem | awk '{print $2}')
MAX_CLIENTS=$(if [ $(httpd -V | grep "Server MPM:" | awk '{ print $3 }') == 'Prefork' ]; then echo "$(grep -c proc /proc/cpuinfo) * 200" | bc; else echo "$(grep -c proc /proc/cpuinfo) * 300" | bc; fi)
THREAD_STACK_SIZE=$(ulimit -s)

echo "System Memory Limit: $MEM_LIMIT"
echo "System Thread Stack Size: $THREAD_STACK_SIZE"
echo "Max Clients Setting: $MAX_CLIENTS"
echo "Max Memory Needed for Optimal configuration: $(echo "$MAX_CLIENTS * $THREAD_STACK_SIZE" | bc) bytes"

if [ $MEM_LIMIT -gt $(echo "$MAX_CLIENTS * $THREAD_STACK_SIZE" | bc) ]; then
    echo "You have Enough RAM to reach the optimal configuration."
else
    echo "You can not reach the optimal configuration because of the amout of RAM your system has."
fi

.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.

Common Errors

PHP Parse error: syntax error, unexpected '}'

Fix

Did you forget a semi-colon somewhere?

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