LinuxからPHPでinformixに接続

informixのデータをなんとか見に行きたい。
今回はPHPのドライバーをcompile,buildして設定していきたいと思います。
MySQLやPostgreSQLを使っている皆様はapt-getやyum一発で終了。
しかし、Cult DB,InformixはいちいちCompile Buildです。

StackOverFlowに素敵なスレッドがありました。
PHP and Informix on Debianです。
これを参考にして最近オシャレになったCentOS7でPHPのInformixドライバーをBuildしてみたいと思います。

## 環境

- Client
    - CentOS7 (Linux)
    - ja_JP.UTF-8 (日本語UTF-8)
    - Informix CSDK Installed
- Server
    - HP-UX
    - ja_JP.932 (Shift-JIS)

## 手順

まずは、PHPが実行できる環境を作ります。
Apache2を入れてPHPをインストールします。

```bash:apache2とPHPをインストール
$ sudo yum groupinstall "ベーシック Webサーバー"
$ sudo yum install php php-devel php-mbstring php-pdo

```

```bash:httpdの起動確認
$ sudo systemctl status httpd #起動しているか確認
$ sudo systemctl start httpd #httpd起動
$ sudo systemctl enable httpd #httpd有効化 (OS起動後に起動するようにする)
```

PHPとApacheが動いているか確かめてみましょう。

```bash:apacheとPHPの起動確認
$ sudo vi /var/www/html/hello.php
<?php
phpinfo();
?>
# 保存する
```

CentOSにアクセスしてphpinfoが見えていればOK。
CentOSから観に行く場合は、
http://localhost/hello.php
僕はVagrantに仕込んでるので、
http://192.168.33.10/hello.php

![phpinfo.png](https://qiita-image-store.s3.amazonaws.com/0/24550/c9c30950-36d6-940d-33cb-aa1e17badbbe.png)


OK。
動いていますね。

```bash:pdo-informix取得
$ cd ~
$ mkdir pdo
$ cd pdo
$ curl -O http://pecl.php.net/get/PDO_INFORMIX-1.3.1.tgz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 67566  100 67566    0     0  42004      0  0:00:01  0:00:01 --:--:-- 41992
```


```bash:解凍
$ tar -xvf PDO_INFORMIX-1.3.1.tgz
$ cd PDO_INFORMIX

```

```bash:INFORMIXDIR環境変数をセット
$ export INFORMIXDIR=/opt/IBM/informix

```

```bash:phpizeを実行
$ pwd
/home/vagrant/pdo/PDO_INFORMIX-1.3.1

$ phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
```

```bash:configure実行
$ ./configure
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php -I/usr/include/php/main -I/usr/i
nclude/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/ph
p/ext/date/lib
checking for PHP extension directory... /usr/lib64/php/modules
checking for PHP installed headers prefix... /usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate
 PHP parsers.
checking for gawk... gawk
checking for Informix driver for PDO... yes, shared
checking for PDO includes... /usr/include/php/ext
checking for includes and libraries... /opt/IBM/informix
checking Informix version... 410
checking how to print strings... printf
checking for a sed that does not truncate output... (cached) /usr/bin/sed
checking for fgrep... /usr/bin/grep -F
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking whether the shell understands some XSI constructs... yes
checking whether the shell understands "+="... yes
checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-li
nux-gnu format... func_convert_file_noop
checking how to convert x86_64-unknown-linux-gnu file names to toolchain format.
.. func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking for gawk... (cached) gawk
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for sysroot... no
checking for mt... no
checking if : is a manifest tool... no
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC -DPIC
checking if cc PIC flag -fPIC -DPIC works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking if cc supports -c -o file.o... (cached) yes
checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libra
ries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
configure: creating ./config.status
config.status: creating config.h
config.status: executing libtool commands
```

php_pdoをインストールした為か、
php_pdo_driver.hが見つからないというエラーが出ない。

気になる
WARNINGは You will need re2c 0.13.4 later.
ぐらいですが、今回は入れても入れなくても動くので、
そのまま先に進みます。


```bash:PDOをコンパイル
$ make
/bin/sh /home/vagrant/pdo/PDO_INFORMIX-1.3.1/libtool --mode=compile cc -I/usr/in
clude/php/ext -I. -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -DPHP_ATOM_INC -I/home/
vagrant/pdo/PDO_INFORMIX-1.3.1/include -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1/ma
in -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -I/usr/include/php -I/usr/include/php/
main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/u
sr/include/php/ext/date/lib -I/opt/IBM/informix/incl/cli -I/opt/IBM/informix/inc
l/esql  -DHAVE_CONFIG_H  -g -O2   -c /home/vagrant/pdo/PDO_INFORMIX-1.3.1/pdo_in
formix.c -o pdo_informix.lo
libtool: compile:  cc -I/usr/include/php/ext -I. -I/home/vagrant/pdo/PDO_INFORMI
X-1.3.1 -DPHP_ATOM_INC -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1/include -I/home/va
grant/pdo/PDO_INFORMIX-1.3.1/main -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -I/usr/
include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Z
end -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/opt/IBM/informix/i
ncl/cli -I/opt/IBM/informix/incl/esql -DHAVE_CONFIG_H -g -O2 -c /home/vagrant/pd
o/PDO_INFORMIX-1.3.1/pdo_informix.c  -fPIC -DPIC -o .libs/pdo_informix.o
/bin/sh /home/vagrant/pdo/PDO_INFORMIX-1.3.1/libtool --mode=compile cc -I/usr/in
clude/php/ext -I. -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -DPHP_ATOM_INC -I/home/
vagrant/pdo/PDO_INFORMIX-1.3.1/include -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1/ma
in -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -I/usr/include/php -I/usr/include/php/
main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/u
sr/include/php/ext/date/lib -I/opt/IBM/informix/incl/cli -I/opt/IBM/informix/inc
l/esql  -DHAVE_CONFIG_H  -g -O2   -c /home/vagrant/pdo/PDO_INFORMIX-1.3.1/inform
ix_driver.c -o informix_driver.lo
libtool: compile:  cc -I/usr/include/php/ext -I. -I/home/vagrant/pdo/PDO_INFORMI
X-1.3.1 -DPHP_ATOM_INC -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1/include -I/home/va
grant/pdo/PDO_INFORMIX-1.3.1/main -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -I/usr/
include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Z
end -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/opt/IBM/informix/i
ncl/cli -I/opt/IBM/informix/incl/esql -DHAVE_CONFIG_H -g -O2 -c /home/vagrant/pd
o/PDO_INFORMIX-1.3.1/informix_driver.c  -fPIC -DPIC -o .libs/informix_driver.o
/bin/sh /home/vagrant/pdo/PDO_INFORMIX-1.3.1/libtool --mode=compile cc -I/usr/in
clude/php/ext -I. -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -DPHP_ATOM_INC -I/home/
vagrant/pdo/PDO_INFORMIX-1.3.1/include -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1/ma
in -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -I/usr/include/php -I/usr/include/php/
main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/u
sr/include/php/ext/date/lib -I/opt/IBM/informix/incl/cli -I/opt/IBM/informix/inc
l/esql  -DHAVE_CONFIG_H  -g -O2   -c /home/vagrant/pdo/PDO_INFORMIX-1.3.1/inform
ix_statement.c -o informix_statement.lo
libtool: compile:  cc -I/usr/include/php/ext -I. -I/home/vagrant/pdo/PDO_INFORMI
X-1.3.1 -DPHP_ATOM_INC -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1/include -I/home/va
grant/pdo/PDO_INFORMIX-1.3.1/main -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -I/usr/
include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Z
end -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/opt/IBM/informix/i
ncl/cli -I/opt/IBM/informix/incl/esql -DHAVE_CONFIG_H -g -O2 -c /home/vagrant/pd
o/PDO_INFORMIX-1.3.1/informix_statement.c  -fPIC -DPIC -o .libs/informix_stateme
nt.o
/bin/sh /home/vagrant/pdo/PDO_INFORMIX-1.3.1/libtool --mode=link cc -DPHP_ATOM_I
NC -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1/include -I/home/vagrant/pdo/PDO_INFORM
IX-1.3.1/main -I/home/vagrant/pdo/PDO_INFORMIX-1.3.1 -I/usr/include/php -I/usr/i
nclude/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/p
hp/ext -I/usr/include/php/ext/date/lib -I/opt/IBM/informix/incl/cli -I/opt/IBM/i
nformix/incl/esql  -DHAVE_CONFIG_H  -g -O2   -o pdo_informix.la -export-dynamic
-avoid-version -prefer-pic -module -rpath /home/vagrant/pdo/PDO_INFORMIX-1.3.1/m
odules  pdo_informix.lo informix_driver.lo informix_statement.lo -Wl,-rpath,/hom
e/vagrant/pdo/PDO_INFORMIX-1.3.1/. -L/home/vagrant/pdo/PDO_INFORMIX-1.3.1/. -Wl,
-rpath,/opt/IBM/informix/lib/esql -L/opt/IBM/informix/lib/esql -Wl,-rpath,/opt/I
BM/informix/lib/cli -L/opt/IBM/informix/lib/cli -Wl,-rpath,/opt/IBM/informix/lib
 -L/opt/IBM/informix/lib -lifcli -lifdmr -lifsql -lifasf -lifgen -lifos -lifgls
-ldl -lcrypt -lifglx
libtool: link: cc -shared  -fPIC -DPIC  .libs/pdo_informix.o .libs/informix_driv
er.o .libs/informix_statement.o   -L/home/vagrant/pdo/PDO_INFORMIX-1.3.1/. -L/op
t/IBM/informix/lib/esql -L/opt/IBM/informix/lib/cli -L/opt/IBM/informix/lib -lif
cli -lifdmr -lifsql -lifasf -lifgen -lifos -lifgls -ldl -lcrypt -lifglx  -O2 -Wl
,-rpath -Wl,/home/vagrant/pdo/PDO_INFORMIX-1.3.1/. -Wl,-rpath -Wl,/opt/IBM/infor
mix/lib/esql -Wl,-rpath -Wl,/opt/IBM/informix/lib/cli -Wl,-rpath -Wl,/opt/IBM/in
formix/lib   -Wl,-soname -Wl,pdo_informix.so -o .libs/pdo_informix.so
libtool: link: ( cd ".libs" && rm -f "pdo_informix.la" && ln -s "../pdo_informix
.la" "pdo_informix.la" )
/bin/sh /home/vagrant/pdo/PDO_INFORMIX-1.3.1/libtool --mode=install cp ./pdo_inf
ormix.la /home/vagrant/pdo/PDO_INFORMIX-1.3.1/modules
libtool: install: cp ./.libs/pdo_informix.so /home/vagrant/pdo/PDO_INFORMIX-1.3.
1/modules/pdo_informix.so
libtool: install: cp ./.libs/pdo_informix.lai /home/vagrant/pdo/PDO_INFORMIX-1.3
.1/modules/pdo_informix.la
libtool: finish: PATH="/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/v
agrant/.local/bin:/home/vagrant/bin:/sbin" ldconfig -n /home/vagrant/pdo/PDO_INF
ORMIX-1.3.1/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /home/vagrant/pdo/PDO_INFORMIX-1.3.1/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

```

```bash:PDOをインストール
$ sudo make install
/bin/sh /home/vagrant/pdo/PDO_INFORMIX-1.3.1/libtool --mode=install cp ./pdo_inf
ormix.la /home/vagrant/pdo/PDO_INFORMIX-1.3.1/modules
libtool: install: cp ./.libs/pdo_informix.so /home/vagrant/pdo/PDO_INFORMIX-1.3.
1/modules/pdo_informix.so
libtool: install: cp ./.libs/pdo_informix.lai /home/vagrant/pdo/PDO_INFORMIX-1.3
.1/modules/pdo_informix.la
libtool: finish: PATH="/sbin:/bin:/usr/sbin:/usr/bin:/sbin" ldconfig -n /home/va
grant/pdo/PDO_INFORMIX-1.3.1/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /home/vagrant/pdo/PDO_INFORMIX-1.3.1/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Installing shared extensions:     /usr/lib64/php/modules/

$ ls -ltr /usr/lib64/php/modules
total 4728
-rwxr-xr-x. 1 root root   58376 Jun 23  2015 zip.so
-rwxr-xr-x. 1 root root   51336 Jun 23  2015 sqlite3.so
-rwxr-xr-x. 1 root root  271960 Jun 23  2015 phar.so
-rwxr-xr-x. 1 root root   29168 Jun 23  2015 pdo_sqlite.so
-rwxr-xr-x. 1 root root  116304 Jun 23  2015 pdo.so
-rwxr-xr-x. 1 root root 1305696 Jun 23  2015 mbstring.so
-rwxr-xr-x. 1 root root   44680 Jun 23  2015 json.so
-rwxr-xr-x. 1 root root 2713352 Jun 23  2015 fileinfo.so
-rwxr-xr-x. 1 root root   74648 Jun 23  2015 curl.so
-rwxr-xr-x. 1 root root  152626 Jan  4 23:00 pdo_informix.so
```

```bash:PHPでPDOを有効化
$ cd /etc/php.d/
$ sudo vi pdo_informix.ini
extension=pdo_informix.so
#保存する
```

```bash:INFOMRIXの環境変数をApacheに有効化
$ cd /etc/sysconfig/
$ sudo vi httpd
...(省略)...
#一番下に下記のものを書き込む

INFORMIXDIR=/opt/IBM/informix
```

```bash:Apache再起動
$ sudo systemctl restart httpd
```

phpinfoにアクセスしてPDO_Informixが入っているか、
etc/sysconfigで入れたINFORMIXDIRの環境変数が入っているかを確かめてみましょう。

http://localhost/hello.php
http://192.168.33.10/hello.php

phpinfoに下記のようなものがあればOKです。

![pdo_informix.png](https://qiita-image-store.s3.amazonaws.com/0/24550/dcc3879c-6504-c03c-8aa6-319ed807e407.png)

![httpd_env.png](https://qiita-image-store.s3.amazonaws.com/0/24550/d3655ea9-fe14-8b98-cf2d-77c7b9fb10cc.png)

実際に接続できるPHPのcodeを作成して確かめてみましょう。

```bash:ifx.phpを作成
$ sudo vi /var/www/html/ifx.php

```

```php:Informixに接続するコード
<?php
error_reporting(E_ALL);
ini_set("display_errors",1);

$db = new PDO("informix:host=IPAddress; service=1526; database=dbname; server=servername; protocol=onsoctcp; EnableScrollableCursors=1; db_locale=ja_JP.932; client
_locale=ja_JP.utf8", "db_user", "db_password");

print "Connection Established\n";
$stmt=$db->query("select * from 得意先m where 得意先cd=1");
$res=$stmt->fetch( PDO::FETCH_BOTH );
$rows=$res[4];
echo "Table contents: $rows.\n";

?>
```

では、ブラウザで見てみましょう。

```http:Errorメッセージ
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE=08004, SQLDriverConnect: - 908 [Informix][Informix ODBC Driver][Informix]Attempt to connect to database server (servername) failed.' in /var/www/html/ifx.php:5 Stack trace: #0 /var/www/html/ifx.php(5): PDO->__construct('informix:host=1...', 'id', 'password') #1 {main} thrown in /var/www/html/ifx.php on line 5﻿
```

エラーメッセージがでて接続できないようです。
SQLSTATE=08004はサーバーから接続拒否されたというエラーメッセージ。
Debian/Ubuntuで動いてCentOS/RedHatで動かない理由ってSELinuxぐらいしか思いつかない。
SELinuxを無効化します。

```bash:SELinux無効化
$ sudo vi /etc/selinux/config
# 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 - No SELinux policy is loaded.
# SELINUX=enforcing
SELINUX=permissive #permissiveに変更

#保存する

$ sudo reboot #再起動
```

もう一度、ブラウザーで見に行ってみましょう。

```text:Informixの接続結果
Connection Established Table contents: 僕の株式会社 !Aﾎﾞｸﾉ �!!得意先略名!.
```
データが表示できました。
実用かどうかは別として取り敢えず成功です。

## 参考文献

[PHP and Informix on Debian - how to install/configure the PDO](
http://stackoverflow.com/questions/19909075/php-and-informix-on-debian-how-to-install-configure-the-pdo)

[SELinuxを無効化する - rhythmfactory smart](http://rfs.jp/server/security/selinux01.html)

[SQLSTATE 値から Informix エラー メッセージへのマップ](http://www-01.ibm.com/support/knowledgecenter/SSGU8G_12.1.0/com.ibm.odbc.doc/ids_odbc_236.htm?lang=ja)

## Ubunt/Debian系の場合


yumの代わりにapt-getやtasksel。
Apacheの環境変数の場所が少しばかり違うだけです。
違う点だけピックアップしておきます。
pdo_informixのコンパイルの手順は全く同じです。


### LAMP環境インストール

```bash:apache2とPHPをインストール
$ sudo tasksel install lamp-server

```

### pdo_informixのconfigure

Ubuntu/Debianだと、pdo_informixドライバーをconfigureするときにphp_pdo_driver.hが見つからないというエラーがでます。
下記のように修正してもう一度、configureを実行します。

```bash:configureを修正
$ locate php_pdo_driver.h
/usr/include/php5/ext/pdo/php_pdo_driver.h


$ sed -ie 's,include/php/ext/pdo/php_pdo_driver.h,include/php5/ext/pdo/php_pdo_driver.h,g' configure

$ ./configure
configure: loading site script /usr/share/site/x86_64-unknown-linux-gnu
...(以下略)....
```

### INFORMIX の変数をapacheで有効化させる
Ubuntu/Debianだと下記の場所に環境変数を書き込む

- /etc/apache2/envvars