HOW-TO: Install PHP5, Oracle Client v10.2 and OCI8 on Ubuntu Server Linux

Posted by in Oracle on Unix Server
10 Comments

Connecting an Oracle Database via PHP, requires a not easy procedure. The bash scripts OracleViaPHP.sh and TNSconnect.sh will automate the set up process for you.
The scripts are tested on a virtual machine running OS Linux Ubuntu Server 8.04.1 i386 created on a Mac OS X running Leopard 10.5.3. OracleViaPHP.sh will install and configure Apache2, PHP5, the Oracle Client v10.2 and the OCI8 and TNSconnect.sh will create the file tnsnames.ora to connect the Oracle DB. The scripts should also work on Ubuntu 9.x.

Pre-installation:
1. Download Ubuntu Server 8.04.1 from www.ubuntu.com
2. Download from the Oracle website the Oracle Client for Linux x86 Version 10.2.0.4 and the SDK:
– the Oracle Client ‘Instant Package – Basic Lite’ [oracle-instantclient-basiclite-10.2.0.4-1.i386.zip]
– the Oracle Client ‘Instant Package – SDK’ [oracle-instantclient-devel-10.2.0.4-1.i386.zip]
3. Download the bash scripts OracleViaPHP.sh and TNSconnect.sh. Last version can be found in the script page.
4. Install Ubuntu Server 8.04.1

Installation:
1. Set a root password and log in as root
# sudo passwd
# su –
2. Copy the files oracle-instantclient-basiclite-10.2.0.4-1.i386.zip, oracle-instantclient-devel-10.2.0.4-1.i386.zip, OracleViaPHP.sh and TNSconnect.sh into the /root directory.
3. Enter the /root directory and execute the installation script.
# cd /root
# ./OracleViaPHP.sh

Post-Installation: [Steps Required]
1. Make sure you know SID, SERVER and PORT of the server you want to connect.
2. Execute the script TNSconnect.sh
# cd /root
# ./TNSconnect.sh
3. Type: Oracle SID, Oracle Server, Oracle Server Port, Oracle Database Username, Oracle Database Password.

Installation Check:
1. Connecting http://your_ip_address/info.php you should get the PHP info page showing the module oci8 installed.
2. Connecting http://your_ip_address/test.php you should be able to connect the Oracle DB you defined.

Trouble Shooting
If you get the error:
Warning: ocilogon() [function.ocilogon]: ORA-12154: TNS:could not resolve the connect identifier specified in /var/www/test.php on line 2
Could not connect to Oracle database!

It is possible that:
1. You typed the Oracle Server details in a wrong way. You can edit manually this information in the files /opt/oracle/tns/tnsnames.ora and /var/www/test.php
2. The permission to the file /opt/oracle/tns/tnsnames.ora are not right. You can to execute the following command:
# chmod 777 /opt/oracle/tns/tnsnames.ora
3. The environment variables in the file /etc/init.d/apache2 are not defined properly. Make sure that the file /etc/init.d/apache2 contains at the beginning the following string:
ENV=”env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin
ORACLE_HOME=/opt/oracle/instantclient_10_2
TNS_ADMIN=/opt/oracle/tns
LD_LIBRARY_PATH=/opt/oracle/instantclient_10_2″

If you get the error:
/usr/bin/ld: skipping incompatible /opt/oracle/instantclient_10_2/libclntsh.so when searching for -lclntsh
/usr/bin/ld: cannot find -lclntsh
collect2: ld returned 1 exit status
make: *** [oci8.la] Error 1
ERROR: `make' failed

it’s possible that your server has a 64-bit architecture. You will need to download from the Oracle website the Oracle Client and the SDK package for Linux 64-bit architecture and manually unzip the files in the directory /opt/oracle/instantclient_10_2.

Your comment, error reports and improvement requests are always welcome ;D

# # # # # #

10 thoughts

  1. Grazie!
    Finalmente sono riuscito a farmi un ambiente locale per sviluppare PHP/Oracle su Ubuntu Gutsy: se non c’eri tu…

    Uniche 3 cose che ho fatto oltre a lanciare gli script:

    1) Ho scaricato da Oracle la BASIC anziché la BASICLITE poiché la basiclite non supportava il charset con cui erano impostati i server a cui dovevo accedere;

    2) non avevo phpize:
    # apt-get install php5-dev;

    3) ho riavviato a mano apache, fallendo lo script in quel punto:
    # apache2ctl restart

    ciao e grazie ancora!
    Marco

  2. I had to do just one additional process to get it to work (after having tried the troubleshooting steps):

    * create user named “oracle”
    * create group named “oinstall”
    * chown -R oracle:oinstall /opt/oracle
    * edit /etc/group, add user “www-data” to the oinstall group

    Oh, and I also had to chmod 755 /etc/init.d/apache2, as the ./OracleViaPHP.sh script left the apache2 file as 644.

  3. Anyway it’s good to specify that it’s not compulsory to create a new oracle user ;P

  4. Hi. I just try this. But I am use Oracle-8.1 server.

    Warning: ocilogon() [function.ocilogon]: ORA-03134: Connections to this server version are no longer supported. in /var/www/test.php on line 2
    Could not connect the Oracle database!

    Any solution?

  5. Hi Lufti,
    sorry but with the Oracle Client 10.2 of the client is not possible to connect an Oracle DB 8.1…

  6. Hi there,

    This is great. Thanks so much for putting this script together. It is much appreciated.

    I have had one problem. After the script creates the new apache2 file in init.d, that file needs to be set to executable or the service can’t be restarted:

    chmod +x apache2

    Perhaps add that to your script.

    Regards,
    -adam

  7. Hi adam,
    I didn’t have that problem but I will take a look!
    Thank you

  8. Albert

    March 1, 2009

    Hi,

    is it possible to connect Oracle XE using your tutorial above ? thanks.

  9. Kristjan

    May 5, 2009

    Hi,
    thank you for script, basically it works for ubuntu 9.04, too. The only problem was somehow it removed /etc/init.d/apache2 execution rights so I initially thought the file got lost. Actually I just had to set rights back again.

  10. It seems the problem of the execute rights on apache2 pops up on Ubuntu version 9. I updated the installation script so it should be solved now ;)

Reply