Apache, PHP7 and Oracle 19c
Overview
Essential steps to configure an Oracle Linux system to use PHP to interact with Oracle databases.
- Disable Firewalls and SELinux
- Install Oracle Instant Client and SQLPLus
- Install PHP and OCI8
- Apache & PHP Actions
- Code Example: oracle_test.php
- Init Application Dev Environment
- SYSDBA Connection Example
- .bashrc Changes
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 -- Cfg vi /usr/lib/oracle/19.5/client64/lib/network/admin/tnsnames.ora <Add Your TsnServiceNameEntries> -- Test cd /usr/lib/oracle/19.5/client64/bin ./sqlplus sys/go@MyOraDB as sysdba SELECT instance_name FROM v$instance;
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
- Edit /etc/php.d/20-oci8.ini
- Set oci8.privileged_connect = On
- 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,
- vi /usr/lib/oracle/19.5/client64/lib/network/admin/sqlnet.ora
- TCP.CONNECT_TIMEOUT=5
- systemctl stop httpd.service
- 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
OEL 7.9 Post OS Update - Session
Perform all the below steps as root user.
-- Disable OS Noise systemctl disable firewalld; systemctl stop firewalld; service iptables stop; chkconfig iptables off; systemctl status firewalld vi /etc/selinux/config SELINUX=disabled -- Check yum list installed | grep instantclient -- Purge yum remove oracle-instantclient19.5-basic.x86_64 -y yum remove oracle-release-el7 -y yum list installed | grep instantclient -- Install yum install oracle-instantclient-release-el7 -y --> /usr/bin/ol_yum_configure.sh yum install oracle-instantclient-basic -y Installed: oracle-instantclient-basic.x86_64 0:21.9.0.0.0-1 yum install oracle-instantclient-sqlplus -y Installed: oracle-instantclient-sqlplus.x86_64 0:21.9.0.0.0-1 -- Post-Check\Cfg vi /usr/lib/oracle/21/client64/lib/network/admin/tnsnames.ora cd /usr/lib/oracle/21/client64/bin ./sqlplus sys/xxx@MyDB as sysdba SELECT instance_name FROM v$instance; -- PHP\OCI yum install -y oracle-php-release-el7 Installed: oracle-php-release-el7.x86_64 0:1.0-5.el7 yum install php php-oci8-21c -y systemctl start httpd.service; systemctl enable httpd.service systemctl stop httpd.service;systemctl start httpd.service -- Test gedit /var/www/html/oracle_test.php $connection = oci_connect("scott", "tiger","192.168.1.42/MyDB"); http://localhost/ http://localhost/pi.php http://localhost/oracle_test.php -- Lock Down Version yum install yum-plugin-versionlock -y yum versionlock oracle-instantclient-release-el7 -y -- Info If the ORACLE_HOME environment variable is not set, you can set it by editing your .bashrc file and adding the following line: export ORACLE_HOME=/usr/lib/oracle/21/client64 If the LD_LIBRARY_PATH environment variable is not set, you can set it by editing your .bashrc file and adding the following line: export LD_LIBRARY_PATH=/usr/lib/oracle/21/client64/lib
References
- https://docs.oracle.com/en/database/oracle/oracle-database/21/lacli/install-instant-client-using-rpm.html#GUID-2E81E2AE-E94C-413F-99B2-AE9A3949F05D
- https://blogs.oracle.com/linux/connect-php-7-to-oracle-database-using-oracle-linux-yum-server
- https://yum.oracle.com/oracle-linux-php.html
- https://www.oracle.com/database/technologies/underground-php-oracle-manual.html
- http://yum.oracle.com/repo/OracleLinux/OL7/developer/php74/x86_64