Sunday, December 06, 2009

FreeBSD Install and Configure Webmin Web-based Interface

Q. How do I install webmin control panel for my FreeBSD server?

A. Webmin is a web-based interface for system administration for Unix including FreeBSD. Using any browser that supports tables and forms, you can setup user accounts, Apache, DNS, file sharing, firewall and so on. Webmin consists of a simple web server, and a number of CGI programs which directly update system files like /etc/inetd.conf and /etc/master.passwd.

Install webmin

To install webmin, update your ports, enter:
# portsnap fetch update
Install webmin from /usr/ports/sysutils/webmin, enter:
# cd /usr/ports/sysutils/webmin
# make install clean

Configure webmin

Now, webmin is installed. Start webmin on startup, enter:
# vi /etc/rc.conf
Append following line:
webmin_enable="YES"
Save and close the file. You need to run /usr/local/lib/webmin/setup.sh script in order to setup the various config files, enter:
# /usr/local/lib/webmin/setup.sh
Sample output:

*********************************************************************** *            Welcome to the Webmin setup script, version 1.420        * *********************************************************************** Webmin is a web-based interface that allows Unix-like operating systems and common Unix services to be easily administered.  Installing Webmin in /usr/local/lib/webmin ...  *********************************************************************** Webmin uses separate directories for configuration files and log files. Unless you want to run multiple versions of Webmin at the same time you can just accept the defaults.  Log file directory [/var/log/webmin]: [Press Enter]  *********************************************************************** Webmin is written entirely in Perl. Please enter the full path to the Perl 5 interpreter on your system.  Full path to perl (default /usr/bin/perl):  [Press Enter]  Testing Perl ... Perl seems to be installed ok  *********************************************************************** Operating system name:    FreeBSD Operating system version: 7.0  *********************************************************************** Webmin uses its own password protected web server to provide access to the administration programs. The setup script needs to know :  - What port to run the web server on. There must not be another    web server already using this port.  - The login name required to access the web server.  - The password required to access the web server.  - If the webserver should use SSL (if your system supports it).  - Whether to start webmin at boot time.  Web server port (default 10000):  [Press Enter] Login name (default admin):  [Press Enter] Login password: [type password] Password again: Use SSL (y/n): y *********************************************************************** Creating web server config files.. ..done  Creating access control file.. ..done  Creating start and stop scripts.. ..done  Copying config files.. ..done  Changing ownership and permissions .. ..done  Running postinstall scripts .. ..done 

How do I view webmin?

Fire a webbrowser and enter url:
https://your-domain.com:10000/
OR
https://your-server-ip:10000/
You should see login html form as follows:

(Fig. 01: - Webmin in action under FreeBSD)

Wednesday, December 17, 2008

How To Configure Web Access To Subversion Repositories Using Apache


This how to is going to describe the steps to get the mod_dav_svn module to work in an Apache web server. First I'll assume that we don't have Apache and Subversion installed on our FreeBSD box, in a second part I'll explain how to add the module using our current installation.

First we have to install our Apache 2.0.x with Berkeley DB support (because Subversion will use Berkeley DB to save the repositories). To do this we have to go to the ports dir and do this:

box# cd /usr/ports/www/apache20/
box# make -DWITH_BDB4 install clean
some installation steps...

Add apache ability to start automatically at boot time:

box# echo 'apache2_enable="YES"' >> /etc/rc.conf

After we have apache20 installed with bdb support, we'll have to install Subversion:

box# cd /usr/ports/devel/subversion
box# make -DWITH_MOD_DAV_SVN install clean
some installation steps...

After installation we'll have to ensure that mod_dav_svn module was properly installed on apache.

box# cat /usr/local/etc/apache2/httpd.conf | grep svn

LoadModule dav_svn_module libexec/apache2/mod_dav_svn.so
LoadModule authz_svn_module libexec/apache2/mod_authz_svn.so

We have apache with mod_dav_svn module installed properly. At this point we can create a repository. This will help us to test our instalation:

box# mkdir /usr/home/svn
box# mkdir /usr/home/svn/repos
box# svnadmin create /usr/home/svn/repos/test

Then we have to create the files that are going to be used to authenticate the users.

box# mkdir /usr/home/svn/access
box# cd /usr/home/svn/access
box# htpasswd -cm users root
password:****
box# htpasswd -m users viewer
password:*****

box# vi control

[test:/]
root = rw
viewer = r.


At this point we have apache with bdb support, subversion with mod_dav_svn module installed, our repository created, the users and the control to our repository. Now we will configure apache to read the repositories:

box# cd /usr/local/etc/apache2/Includes/
box# cat svn.conf


DAV svn
SVNParentPath /usr/home/svn/repos
SVNIndexXSLT "http://svn.example.com/svnindex.xsl"
AuthzSVNAccessFile /usr/home/svn/access/control
# anonymous first
Satisfy Any
Require valid-user
# authenticating them valid ones
AuthType Basic
AuthName "Subversion Repositories at example.com"
AuthUserFile /usr/home/svn/access/users

Apache will read all the files that are under the Includes directory, so our svn.conf will be loaded when apache starts, note that we are loading svnindex.xsl that is the file where the transformations are done, if you would like to give to your repository some look and feel work these file will be the appropiate. The file skeletons are under /usr/local/share/subversion/xslt/ directory, there are two files, one .xsl and another .css. Copy these files to your document root. I have a virtual server called svn.example.com in my machine. I have all my virtual servers under /usr/local/www/pages, so I have svn.example.com directory and I've configured that virtual server in /usr/local/etc/apache2/httpd.conf.

NameVirtualHost *:80

ServerAdmin ecruz@example.com
ServerName svn.example.com
DocumentRoot /usr/local/www/pages/svn.example.com
CustomLog /var/log/svn.example.com-access_log common

Restart the web server:

/usr/local/etc/rc.d/apache2.sh restart

If all went ok, we have our web server working properly, to test it, open in your Firefox or whatever browser and go to http://svn.example.com/svn/repos/test. It will ask you for the credentials, so use root or viewer. It must display the test repository at revision 0. I'll suggest to install TortoiseSVN on Windows boxes to get access to the repositories.

Now, as a plus, we will configure an alert in our subversion test repository to send a notification when a commit was done. To do this we will have to create an executable file under the hooks directory:

box# cd /usr/home/svn/repos/test/hooks/
box# cat post-commit

[code]
#!/usr/local/bin/php

[/code]

To get this to work with your current installation you have to change only the subversion installation step:

box# cd /usr/ports/devel/subversion
box# make deinstall
box# make -DWITH_MOD_DAV_SVN -DWITHOUT_BDB4 install clean

Ok, this is the end of this howto, any improvements are welcome. Regards!

Thursday, November 06, 2008

nohup Execute Commands After You Exit From a Shell Prompt

Most of the time you login into remote server via ssh. If you start a shell script or command and you exit (abort remote connection), the process / command will get killed. Sometime job or command takes a long time. If you are not sure when the job will finish, then it is better to leave job running in background. However, if you logout the system, the job will be stopped. What do you do?

nohup command

Answer is simple, use nohup utility which allows to run command./process or shell script that can continue running in the background after you log out from a shell:

nohup Syntax:

nohup command-name &

Where,

  • command-name : is name of shell script or command name. You can pass argument to command or a shell script.
  • & : nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an & symbol.

nohup command examples

1) Login to remote server
$ ssh user@remote.server.com
2) Execute script called pullftp.sh
# nohup pullftp.sh &
Type exit or press CTRL + D exit from remote server.
# exit
3) Find all programs and scripts with setuid bit set on, enter:
# nohup find / -xdev -type f -perm +u=s -print > out.txt &
Type exit or press CTRL + D exit from remote server.
# exit
Please note that nohup does not change the scheduling priority of COMMAND; use nice for that:
# nohup nice -n -5 ls / > out.txt &
As you can see nohup keep processes running after you exit from a shell. Read man page of nohup and nice command for more information. Please note that nohup is almost available on Solaris/BSD/Linux/UNIX variant.

Update:
# 1: As pointed out by Jason you can use at command to queue a job for later execution. For example, you can run pullftp.sh script to queue (one minute) later execution
$ echo "pullftp.sh" | at now + 1 minute
# 2: You can also use screen command for same. Brock pointed out disown shell internal command for same purpose. Here is how you can try it out:
$ pullftp.sh &
$ disown -h
$ exit

According to bash man page:

By default, removes each JOBSPEC argument from the table of active jobs. If the -h option is given, the job is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. The -a option, when JOBSPEC is not supplied, means to remove all jobs from the job table; the -r option means to remove only running jobs.

Friday, October 31, 2008

autostart apache during boot

i just finished my apache, php, mysql installation for my new server.  i used Centos 5.1. Those not started when i reboot my server. Oh..i forgot to make it autostart during boot time. 

Here the steps to auto run apache or mysql during the boot time..

run command runlevel to check your current Centos run level

Running runlevel command on my centos linux machines displays default run level as 5.You must be wondering what runlevel has to do with by autostarting apache server. There is reason for that Linux uses different run levels to boot and you should know default runlevel where you would like to configure apache to autostart during boot.

Next you have to use magic command called chkconfig on fedora to list the apache (httpd) services runlevel:

[root@node12 ~]# chkconfig --list httpd

httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

In chkconfig output off means apache (httpd) service is set with no autostart for associated runlevels. Since my default runlevel is 5 , I need to set apache to autostart in runlevel 5 using chkconfig. Here is how you can turn apache autostart for your default run level :

[root@node12 ~]# chkconfig --level 5 httpd on


If your default run level is 3 then you should use 3 in above command. You can also run 

 [root@node12 ~]# chkconfig --level 235 httpd on

to make ur apache run on level 2,3 and 5. 

Now to check if autostart is set as on during boot


Run checkconfig again :

[root@node12 ~]# chkconfig --list httpd

httpd 0:off 1:off 2:off 3:off 4:off 5:on 6:off

that's all..Centos your life!! 




configure: error: C compiler cannot create executables

Today I have tried to compile a software in a Linux machine, when I run

./configure

I got

checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for gawk... no checking for mawk... mawk checking whether make sets $(MAKE)... yes checking for multicast... checking for gcc... no checking for cc... no checking for cl.exe... no configure: error: no acceptable C compiler found in $PATH See `config.log' for more details. 

Then I installed gcc with:

apt-get install gcc 

for Ubuntu and for Centos i used

yum install gcc 

and try again, this time I got:

and try again, this time I got:

checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for gawk... no checking for mawk... mawk checking whether make sets $(MAKE)... yes checking for multicast... checking for gcc... gcc checking for C compiler default output file name... configure: error: C compiler cannot create executables See `config.log' for more details. 

To solve this I had to install g++

apt-get install g++

And then when I tried again it worked, so to compile software with Linux it seems that you mostly need to install both

gcc and g++

Update: Thanks to Thadeu Penna a friend of the blog for this tip.

using

apt-get install build-essential

Will install all the basic tools for developing in Debian and I know it also works for Ubuntu.

enable and disable SElinux

Important

Changes you make to files while SELinux is disabled may give them an unexpected security label, and new files will not have a label. You may need to relabel part or all of the file system after re-enabling SELinux.

From the command line, you can edit the /etc/sysconfig/selinux file. This file is a symlink to/etc/selinux/config. The configuration file is self-explanatory. Changing the value of SELINUX orSELINUXTYPE changes the state of SELinux and the name of the policy to be used the next time the system boots.

[root@host2a ~]# cat /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: #       enforcing - SELinux security policy is enforced. #       permissive - SELinux prints warnings instead of enforcing. #       disabled - SELinux is fully disabled. SELINUX=permissive # SELINUXTYPE= type of policy in use. Possible values are: #       targeted - Only targeted network daemons are protected. #       strict - Full SELinux protection. SELINUXTYPE=targeted  # SETLOCALDEFS= Check local definition changes SETLOCALDEFS=0 
Changing the Mode of SELinux Using the GUI

Use the following procedure to change the mode of SELinux using the GUI.

Note

You need administrator privileges to perform this procedure.

  1. On the System menu, point to Administration and then click Security Level and Firewall to display the Security Level Configuration dialog box.

  2. Click the SELinux tab.

  3. In the SELinux Setting select either DisabledEnforcing or Permissive, and then click OK.

  4. If you changed from Enabled to Disabled or vice versa, you need to restart the machine for the change to take effect.

Changes made using this dialog box are immediately reflected in /etc/sysconfig/selinux.

Configure PHP connection to Oracle DB – the RPM way

I have CentOS 4.4 box which I want to connect to Oracle DB 10g via PHP
There are 2 way to configure PHP to connect to Oracle DB using OCI:
A. Use PHP RPMs (which included all php module in RPM package) but nowadays oci8 module no longer provider by CentOS and you might use older version of PHP - easiest and quickest way
B. Use current version of PHP and enable oci8 via PEAR. You will get current PHP version with oci8 enable but still in RPM way

Lets begin

A. Use PHP RPMs packages
1. Install Apache if not installed
CODE:
# yum install httpd


2. Download Oracle Client Software -- if you are using GUI
or download Oracle InstantClient BasicLite (the rpm version) -- I choose this and install it.
to get latest version download it from http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html
CODE:
# rpm –ivh oracle-instantclient-basiclite-*.rpm

my copy of Oracle Instant Client BasicLite is here
http://www.am3n.profusehost.net/phpoci/oracle-instantclient-basic-10.2.0.3-1.i386.rpm

3. Install PHP and makesure you installed php-oci8 module, other modules you can install it if you like
CODE:
# rpm -ivh php-5*.rpm php-oci8-*.rpm php-pear-*.rpm

I am not using CentOS version because it's not provide oci8 module. But I am using PHP from Miracle Linux - Asianux which also look-like Redhat derivatives distro. They provide complete PHP module including oci8.
Download it from my mirror here.
http://www.am3n.profusehost.net/phprh4/

Info about Miracle Linux - Asianux
http://en.wikipedia.org/wiki/Miracle_Linux
Complete PHP modules from Miracle Linux
http://www.miraclelinux.com/update/linux/list.php?errata_id=249

4. Create local variable to connect to Oracle, for NLS_LANG you can set with your locale.
CODE:

# vi /etc/profiles
LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.3/client/lib
export LD_LIBRARY_PATH
NLS_LANG=AMERICAN_AMERICA.UTF8
export NLS_LANG


5. Update linux Library thingy... (locate the oracle library if you use differrent version)
CODE:
# echo /usr/lib/oracle/10.2.0.3/client/bin/ >> /etc/ld.so.conf
# echo /usr/lib/oracle/10.2.0.3/client/lib/ >> /etc/ld.so.conf
# ldconfig
# cd /usr/lib/oracle/10.2.0.3/client/lib/
# ln -s libclntsh.so.10.1 libclntsh.so
# ln -s libocci.so.10.1 libocci.so


6. (Re-)Start the Apache
CODE:
# service httpd restart


I have CentOS 4.4 box which I want to connect to Oracle DB 10g via PHP
There are 2 way to configure PHP to connect to Oracle DB using OCI:
A. Use PHP RPMs (which included all php module in RPM package) but nowadays oci8 module no longer provider by CentOS and you might use older version of PHP - easiest and quickest way
B. Use current version of PHP and enable oci8 via PEAR. You will get current PHP version with oci8 enable but still in RPM way

Lets begin

A. Use PHP RPMs packages
1. Install Apache if not installed
CODE:
# yum install httpd


2. Download Oracle Client Software -- if you are using GUI
or download Oracle InstantClient BasicLite (the rpm version) -- I choose this and install it.
to get latest version download it from http://www.oracle.com/technology/tech/oci/instantclient/instantcl
ient.html
CODE:
# rpm –ivh oracle-instantclient-basiclite-*.rpm

my copy of Oracle Instant Client BasicLite is here
http://www.am3n.profusehost.net/phpoci/oracle-instantclient-basic
-10.2.0.3-1.i386.rpm

3. Install PHP and makesure you installed php-oci8 module, other modules you can install it if you like
CODE:
# rpm -ivh php-5*.rpm php-oci8-*.rpm php-pear-*.rpm

I am not using CentOS version because it's not provide oci8 module. But I am using PHP from Miracle Linux - Asianux which also look-like Redhat derivatives distro. They provide complete PHP module including oci8.
Download it from my mirror here.
http://www.am3n.profusehost.net/phprh4/

Info about Miracle Linux - Asianux
http://en.wikipedia.org/wiki/Miracle_Linux
Complete PHP modules from Miracle Linux
http://www.miraclelinux.com/update/linux/list.php?errata_id=249

4. Create local variable to connect to Oracle, for NLS_LANG you can set with your locale.
CODE:

# vi /etc/profiles
LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.3/client/lib
export LD_LIBRARY_PATH
NLS_LANG=AMERICAN_AMERICA.UTF8
export NLS_LANG


5. Update linux Library thingy... (locate the oracle library if you use differrent version)
CODE:
# echo /usr/lib/oracle/10.2.0.3/client/bin/ >> /etc/ld.so.conf
# echo /usr/lib/oracle/10.2.0.3/client/lib/ >> /etc/ld.so.conf
# ldconfig
# cd /usr/lib/oracle/10.2.0.3/client/lib/
# ln -s libclntsh.so.10.1 libclntsh.so
# ln -s libocci.so.10.1 libocci.so


6. (Re-)Start the Apache
CODE:
# service httpd restart



B. Use your current PHP version from CentOS
If you already install your PHP and don't want to replace it with older version and/or your current version of PHP didn't provide php-oci8 packages you can install it using the following steps
This how to is modification of PHP-OCI8 in Ubuntu Forum
http://www.ubuntuforums.org/archive/index.php/t-92528.html

1. Install Oracle InstantClient
download it from http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html
CODE:
# rpm -ivh oracle-instantclient-basic-10.2.0.3-1.i386.rpm
# rpm -ivh oracle-instantclient-devel-10.2.0.3-1.i386.rpm

My copy is here
http://www.am3n.profusehost.net/phpoci/oracle-instantclient-basiclite-10.2.0.3-1.i386.rpm
http://www.am3n.profusehost.net/phpoci/oracle-instantclient-devel-10.2.0.3-1.i386.rpm

2. Configure linux library thingie...
CODE:
# echo /usr/lib/oracle/10.2.0.3/client/bin/ >> /etc/ld.so.conf
# echo /usr/lib/oracle/10.2.0.3/client/lib/ >> /etc/ld.so.conf

# ldconfig

3. Install oci8 via pear
Make sure you already install php-devel package (and have upgrade to latest version)

CODE:
# cd /usr/local/src
# pear download oci8
# tar xzf oci8-1.2.3.tgz
# cd oci8-1.2.3
# phpize
# ./configure --with-oci8=shared,instantclient,/usr/lib/oracle/10.2.0.3/client/lib/
# make
# make install

My copy of oci8 is here
http://www.am3n.profusehost.net/phpoci/oci8-1.2.3.tgz

4. Enable oci8
CODE:
# vi /etc/php.d/oci8.ini
; Enable oci8 extention module
extension=oci8.so


5. (Re-)Start the Apache
CODE:
# service httpd restart




Just want to make sure if your php is already configure with oci8 module create a php file with phpinfo() function
Create phpinfo file
CODE:
# vi /var/www/html/phpinfo.php


View phpinfo. http://yourserver/phpinfo.php
You should see something like this
CODE:

oci8
OCI8 Support enabled
Revision $Revision: 1.257.2.8 $
Active Persistent Links 0
Active Links 0
Oracle Version 10.1
Compile-time
ORACLE_HOME /opt/oracle/product/10.1.0
Libraries Used -Wl,-rpath,/opt/oracle/product/10.1.0/lib -
L/opt/oracle/product/10.1.0/lib -lclntsh
Temporary Lob support enabled
Collections support enabled

nstalling PHP and the Oracle 10g Instant Client for Linux and Windows

Updated for PHP 5.1.2


Author: Christopher Jones, Consulting Technical Staff, Oracle Corporation

Updated: January 2006

Oracle 10g Instant Client (free download available) is the easiest way for PHP to connect to a remote Oracle database, requiring installation of only three libraries.

The Instant Client libraries used by PHP access Oracle's current API, called OCI8. (This C interface takes its name from being first introduced in Oracle8.) PHP Oracle 8 Functions can call Oracle 8.1.7, 9.x, or 10.x directly, or optional abstraction classes like PEAR MDB2 and ADOdb can be used for convenience.

The older PHP "oracle" extension can also be used with Instant Client but it calls a deprecated Oracle API. New development with this extension is not recommended by the PHP community or by Oracle.

To use Instant Client with PHP 4 or 5 on Apache, follow the steps below. (See this section for details about 5.1.2 and its refactored OCI8 extension.) An existing Oracle database is needed; Instant Client does not include one.

Typically the database will be on another machine. If the database is local then Oracle components will generally already be available and Instant Client is not required.

Software Requirements:

SoftwareNotes
Oracle Instant ClientDownload the "Instant Client Package - Basic." On Linux, also download the "Instant Client Package - SDK."
Apache HTTPD ServerThe PHP community still recommends Apache 1.3
PHP - PHP Hypertext ProcessorVersion 4.3 or later

Enabling the PHP OCI8 Extension on Windows

The Instant Client binaries complement PHP's prebuilt binaries for Windows.

  1. Download the PHP binary zip file (not the installer build) and Apache. Install them following Installation on Windows Systems in the PHP Manual. OTN's PHP Developer Center contains links to useful background material such as "Installing Oracle, PHP, and Apache on Windows 2000/XP," which covers installation of a traditional, full Oracle 10g footprint (which is not required with Instant Client).

    Check that PHP is working before continuing. At this stage Oracle support is not enabled.

  2. Download the Instant Client Basic package for Windows from the Instant Client page on OTN. The zip file is about 30MB in size.

  3. Create a subdirectory (e.g., c:\instantclient10_1) and copy these libraries from the zip file:

    • oraociei10.dll
    • orannzsbb10.dll
    • oci.dll

    Collectively these three files are about 80MB in size.

    To use PHP's older "oracle" extension (enabled with "extension=php_oracle.dll" in php.ini), copy ociw32.dll instead of oci.dll.

  4. Edit the environment and add c:\instantclient10_1 to PATH before any other Oracle directories.

    For example, on Windows 2000, follow Start -> Settings -> Control Panel -> System -> Advanced -> Environment Variables and edit PATH in the System variables list.

    If a tnsnames.ora file is used to define Oracle Net service names, copy tnsnames.ora to c:\instantclient10_1 and set the user environment variable TNS_ADMIN to c:\instantclient10_1. A default service name can optionally be set in the user environment variable LOCAL.

    Set necessary Oracle globalization language environment variables such as NLS_LANG. If nothing is set, a default local environment will be assumed. See An Overview on Globalizing Oracle PHP Applications for more details.

    Unset unnecessary Oracle variables such as ORACLE_HOME and ORACLE_SID.

  5. Edit php.ini and uncomment the OCI8 extension:

    extension=php_oci8.dll 

    Set the extension_dir directive to the full PHP extension DLL path. In PHP 4 the DLLs are in the "extensions" sub-directory of the PHP software. In PHP 5 they are in "ext".

  6. Restart Apache.

To check the extension is configured, create a simple PHP script phpinfo.php where the web server can read it.

 

Load the script into a browser using an "http://" URL. The browser page should contain an "oci8" section saying "OCI8 Support enabled".

Enabling the PHP OCI8 Extension on Linux

To add Oracle connectivity on Linux, PHP needs to be recompiled.

The PHP Developer Center contains links to useful background material such as Installing Oracle, PHP, and Apache on Linux, which covers installation of a traditional, full Oracle 10g footprint (which is not required with Instant Client).

  1. Download and install Apache. For example, to install it in your home directory:
    cd apache_1.3.31 ./configure --enable-module=so --prefix=$HOME/apache --with-port=8888 make make install 

    Edit $HOME/apache/conf/httpd.conf and add:

    AddType application/x-httpd-php        .php AddType application/x-httpd-php-source .phps 
  2. Download PHP and untar it.
  3. Download the Basic and the SDK Instant Client packages from the Instant Client page on OTN. Collectively the two RPMs are about 30MB in size.
  4. Install the RPMs as the root user.
    rpm -Uvh oracle-instantclient-basic-10.1.0.3-1.i386.rpm rpm -Uvh oracle-instantclient-devel-10.1.0.3-1.i386.rpm 

    The first RPM puts the Oracle libraries in /usr/lib/oracle/10.1.0.3/client/lib and the second creates headers in /usr/include/oracle/10.1.0.3/client

  5. Backup and then apply this patch to PHP's ext/oci8/config.m4. The patch line numbers are based on PHP 4.3.9. This patch will not be necessary when PHP bug 31084 is fixed, most likely in PHP 4.3.11 and 5.0.4.

    If you are using PHP 4.3.9 or 4.3.10 you can save the patch to a file, e.g. php_oci8ic_buildpatch, and install it using:

    patch -u config.m4 php_oci8ic_buildpatch 

    The patch creates a new PHP configuration parameter: --with-oci8-instant-client[=DIR]. On Linux, by default, it uses the latest version of the Instant Client installed from the RPMs. A directory to the Oracle libraries can be specified to use a different version. In either case, the correct SDK headers will automatically be used.

    The new parameter is mutally exclusive with the existing --with-oci8 parameter.

    For reference: on non-Linux platforms, the Instant Client package is unzipped into a directory of your choice. The --with-oci8-instant-client parameter will need this directory explicitly specified; for example, --with-oci8-instant-client=/home/instantclient10_1. The Instant Client SDK should unzipped to the same directory as the basic package so the subdirectory of header files can be located by the revised configuration script.

  6. Rebuild the "configure" script in the top-level PHP directory.
    cd php-4.3.9 rm -rf autom4te.cache config.cache ./buildconf --force 
  7. Run configure with the new option. This example uses Apache installed in the home directory.
       ./configure \       --with-oci8-instant-client \       --prefix=$HOME/php --with-apxs=$HOME/apache/bin/apxs \       --enable-sigchild --with-config-file-path=$HOME/apache/conf 
  8. Rebuild PHP.
    make make install 
  9. Copy the PHP configuration to the location given by --with-config-file-path
    cp php.ini-recommended $HOME/apache/conf/php.ini 
  10. Set LD_LIBRARY_PATH to /usr/lib/oracle/10.1.0.3/client/lib and restart Apache.

    If a tnsnames.ora file is used to define Oracle Net service names, set TNS_ADMIN to the directory containing the file.

    It is important to set all Oracle environment variables before starting Apache. A script helps do that:

    #!/bin/sh  APACHEHOME=/home/apache  LD_LIBRARY_PATH=/usr/lib/oracle/10.1.0.3/client/lib:${LD_LIBRARY_PATH} TNS_ADMIN=/home export LD_LIBRARY_PATH TNS_ADMIN  echo Starting Apache  $APACHEHOME/apachectl start 
To confirm the extension is configured, create a simple PHP script phpinfo.php where the web server can read it.
 

Load the script into a browser using a URL similar to "http://localhost:8888//phpinfo.php". The browser page should contain an "oci8" section saying "OCI8 Support enabled".

Connecting to Oracle

Oracle connection information is passed to OCILogon() to create a connection. Tools linked with Instant Client are always "remote" from any database server and an Oracle Net connection identifier must be used along with a username and password. The connection information is likely to be well known for established Oracle databases. With new systems the information is given by the Oracle installation program when the database is set up. The installer should have configured Oracle Net and created a service name.

In new databases the demonstration schemas such as the HR user may need to be unlocked and given a password. This may also be done in SQL*Plus by connecting as the SYSTEM user and executing the statement:

ALTER USER username IDENTIFIED BY new_password ACCOUNT UNLOCK; 
There are several ways to pass the connection information to PHP. This first example uses Oracle 10g's Easy Connect syntax to connect to the HR schema in theMYDB database service running on mymachine. No tnsnames.ora or other Oracle Network file is needed:
$c = OCILogon('hr', 'hr_password', '//mymachine.mydomain/MYDB'); 

See Oracle's Using the Easy Connect Naming Method documentation for the Easy Connect syntax.

Alternatively, if /home/tnsnames.ora contains:
MYDB =  (DESCRIPTION =    (ADDRESS = (PROTOCOL = TCP)(HOST = mymachine.mydomain)(PORT = 1521))    (CONNECT_DATA =       (SERVER = DEDICATED)       (SERVICE_NAME = MYDB)     )   ) 

and the TNS_ADMIN environment variable was set to /home (before starting Apache), then the connection string could be:

$c = OCILogon('hr', 'hr_password', 'MYDB');        
If the environment variable LOCAL (on Windows) or TWO_TASK (on Linux) was set to MYDB then a connection to MYDB could also be made with:
$c = OCILogon('hr', 'hr_password'); 

Using Oracle

When the basic connection is working, try out a simple script, testoci.php. Modify the connection details to suit your database and load it in a browser. This example lists all tables owned by the user HR:

\n";  }   OCILogoff($conn);   ?> 
Troubleshooting

The Oracle PHP Troubleshooting FAQ contains helpful information on connecting to Oracle.

Oracle's SQL*Plus command line tool can be downloaded from the Instant Client page to help resolve environment and connection problems. Also see the SQL*Plus Instant Client Release Notes.

Check the environment used by SQL*Plus is the same as shown by phpinfo.php.

Windows Help

If the phpinfo.php script does not produce an "oci8" section saying "OCI8 Support enabled", verify that "extension=php_oci8.dll" is uncommented in php.ini.

If PATH is set incorrectly or the Oracle libraries cannot be found, starting Apache will give an alert: "The dynamic link library OCI.dll could not be found in the specified path." The Environment section of the phpinfo() page will show the values of PATH and the Oracle variables actually being used by PHP.

If php.ini's extension_dir directive is not correct, Apache startup will give an alert: "PHP Startup: Unable to load dynamic library php_oci8.dll."

Linux Help

Carefully check config.m4 was patched correctly. If "configure" fails, check the config.log file. Revert config.m4, remove caches files, run ./buildconf --force and configure, and verify that the problems are related to the changes made.

Make sure the timestamp on "configure" is current. Remove any cache files and rebuild it if necessary.

Set all required Oracle environment variables in the shell that starts Apache.

For PHP 5.1.2 and Later

The "re-factored" OCI8 extension introduces new syntax for Instant Client support. The re-factored extension was first included in PHP 5.1.2. It is also available frompecl.php.net/package/oci8 and pecl4win.php.net/ext.php/php_oci8.dll for earlier versions of PHP.

If you have installed the Instant Client RPMs as described in this Technical Note, configure PHP with:

./configure \     --with-oci8=instantclient,/usr/lib/oracle/10.1.0.3/client/lib \     --prefix=$HOME/php --with-apxs=$HOME/apache/bin/apxs \     --enable-sigchild --with-config-file-path=$HOME/apache/conf 
If you are using the Instant Client Basic and SDK zip files then change the --with-oci8 option to the unzipped directory, e.g:
--with-oci8=instantclient,$HOME/instantclient10_1  
To tell if you have the re-factored extension, check the output of phpinfo(). It will show seven directives with an "oci8." prefix. These are not present in the previous incarnation.

Conclusion

I hope this article has been helpful. Questions and suggestions can be posted on the OTN Instant Client or PHP forums.