Apache 2.x mod_dbd DBD driver for Oracle


#How to make Apache mod_dbd driver : apr_dbd_oracle.so
 - Apache DBD API :  http://people.apache.org/~niq/dbd.html</dd>
 - DBDs available with MySQL, PostgreSQL ... with examples
 - none available with **ORACLE**, so let me build it.

![mod_DBD_per_file.jpg](https://qiita-image-store.s3.amazonaws.com/0/114634/6fa8c73d-e1f1-95e1-0129-3061b008708c.jpeg)
![mod_DBD_per_rate.jpg](https://qiita-image-store.s3.amazonaws.com/0/114634/1bdf2f20-fb18-b796-0962-b0c8deedb996.jpeg)


## Preliminary 
<dl>
  <dt>[users@httpd] Re: Regarding Apache HTTP Authentication with an Oracle Database
</dt>
  <dd>http://mail-archives.apache.org/mod_mbox/httpd-users/200811.mbox/raw/%3c49142D36.1050706@sbcmaroc.com%3e/2</dd>


  <dt>mod_rewriteの考え方。</dt>
  <dd>http://koseki.hatenablog.com/entry/20090611/ModRewrite</dd>

  <dt>Apache認証をSQLでRDBMSへ連携する mod_authn_dbd と mod_dbを使う。</dt>
  <dd>http://takuya-1st.hatenablog.jp/entry/2015/11/13/205953</dd>

  <dt>Windows上のApache2.2で ODBCデータソースを使った Basic認証を行う</dt>
  <dd>http://www.walbrix.com/jp/blog/2007-10-windowsapache22-odbc-basic.html</dd>

</dl>
***

## Steps - How to build "**apr_dbd_oracle.so**"
### Environment
- OracleLinux 6.7 installed (Red Hat Enterprise Linux Server 6.7)

```sh:/etc/issue
Oracle Linux Server release 6.7
```

### Required : PCRE - Perl Compatible Regular Expressions

 `# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz`
 `# tar zxvf pcre-8.37.tar.gz`
 `# cd pcre-8.37`
 `# ./configure`
 `# make install`

### Oracle Instant Client 11.2.0.4.0
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

 - oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
 - oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
 - oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm  (optional)

 `# rpm -UVh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm`
 `# rpm -UVh oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm`


**/usr/include/oracle/11.2/client64 -> /usr/lib/oracle/11.2/client64/include**
 `# ln -s /usr/include/oracle/11.2/client64 /usr/lib/oracle11.2/client64/include`

**ORACLE environment variables**


```sh:/~/.bashrc
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export PATH=$PATH:$ORACLE_HOME/bin
CPPFLAGS=-I/usr/include/oracle/11.2/client64; export CPPFLAGS
LDFLAGS=-L/usr/lib/oracle/11.2/client64; export LDFLAGS
```


### apache 2.4.18 / apr 1.5.2 / apr-util 1.5.4

 1. apr
 2.  -> apr-util
 3.  --> apache 

 `# wget http://ftp.yz.yamagata-u.ac.jp/pub/network/apache//httpd/httpd-2.4.18.tar.gz`
 `# tar zxvf httpd-2.4.18.tar.gz`
 `# wget http://ftp.kddilabs.jp/infosystems/apache/apr/apr-1.5.2.tar.gz`
 `# tar zxvf apr-1.5.2.tar.gz`
 `# wget http://ftp.kddilabs.jp/infosystems/apache/apr/apr-util-1.5.4.tar.gz`
 `# tar zxvf apr-util-1.5.4.tar.gz`


#### apr 1.5.2

 `# cd apr-1.5.2`
 `# ./configure`
 `# make`
 `# make install`


#### apr-util 1.5.4

 `# cd apr-util-1.5.4`
 `# ./configure --with-apr=/usr/local/apr --with-oracle=/usr/lib/oracle/11.2/client64 --with-oracle-include=/usr/include/oracle/11.2/client64/`
 `# make`
 `# make install`

 **Now we got "apr_dbd_oracle.so"**
 `# ls -aslrt /usr/local/apr/lib/apr-util-1`

```sh:ls
 0 lrwxrwxrwx 1 root root    20 Mar  6 18:08 apr_dbd_sqlite3.so -> apr_dbd_sqlite3-1.so
 4 -rwxr-xr-x 1 root root  1004 Mar  6 18:08 apr_dbd_sqlite3.la
64 -rw-r--r-- 1 root root 59680 Mar  6 18:08 apr_dbd_sqlite3.a
48 -rwxr-xr-x 1 root root 47016 Mar  6 18:08 apr_dbd_sqlite3-1.so
 0 lrwxrwxrwx 1 root root    18 Mar  6 18:08 apr_dbd_pgsql.so -> apr_dbd_pgsql-1.so
 4 -rwxr-xr-x 1 root root   998 Mar  6 18:08 apr_dbd_pgsql.la
80 -rw-r--r-- 1 root root 76214 Mar  6 18:08 apr_dbd_pgsql.a
64 -rwxr-xr-x 1 root root 57639 Mar  6 18:08 apr_dbd_pgsql-1.so
 0 lrwxrwxrwx 1 root root    19 Mar  6 18:08 apr_dbd_oracle.so -> apr_dbd_oracle-1.so
 4 -rwxr-xr-x 1 root root  1068 Mar  6 18:08 apr_dbd_oracle.la
92 -rw-r--r-- 1 root root 86460 Mar  6 18:08 apr_dbd_oracle.a
72 -rwxr-xr-x 1 root root 67999 Mar  6 18:08 apr_dbd_oracle-1.so
 4 drwxr-xr-x 2 root root  4096 Mar  6 18:08 .
 4 drwxr-xr-x 4 root root  4096 Mar  6 18:08 ..
```




#### apache 2.4.18

 `# cd httpd-2.4.18`
 `# CPPFLAGS=-I/usr/include/oracle/11.2/client64; export CPPFLAGS`
 `# LDFLAGS=-L/usr/lib/oracle/11.2/client64; export LDFLAGS`
 `# ./configure`
 `# make`
 `# make install`

 `# /usr/local/apache2/bin/httpd -V`




## Steps - How to configure httpd.conf for mod_dbd Oracle module"

**/usr/local/apache2/conf/httpd.conf**
  uncomment dbd_module to be loaded

```conf:httpd.conf
LoadModule dbd_module modules/mod_dbd.so
```



**/usr/local/apache2/conf/default.conf**
   describe DBD driver, DB connection string, SQL to be executed, rewrite map rule

```conf:default.conf
DBDriver oracle
DBDParams "user=ngas pass=ngas$dba dbname=ALMA10 server=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=133.40.204.77)(PORT=1521)))(CONNECT_DATA=(SERVICE_N
AME=alma10.arc.ea)))"

DBDMin 4
DBDKeep 8
DBDMax 20
DBDExptime 300

#DBDPrepareSQL "select 'file://'||FILE_NAME from NGAS_FILES where FILE_ID = @KEY" map_select

RewriteMap ngas "dbd: select 'http://' || regexp_replace(HOST_ID,':[0-9.]+') || '.mtk.nao.ac.jp' || MOUNT_POINT || '/'|| FILE_NAME from NGAS.NGAS_FILES f, NGAS.NGA
S_DISKS d where f.DISK_ID = d.DISK_ID and f.FILE_ID = %s"


RewriteEngine On

RewriteCond %{QUERY_STRING} ^file_id=(.*)$
RewriteRule ^/RETRIEVE.* ${ngas:%1} [R,L]

```