概要:
简要介绍nginx、php环境中加载KingbaseES V8 php驱动连接金仓数据库的配置方法
CPU: X86_64
OS: CentOS 7
nginx: nginx/1.18.0
php-fpm : 7.2.33
db: KingbaseES V8R3
浏览器:Google Chrome 86.0.4240.111
Nginx(“engine x”)是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。在高连接并发的情况下,Nginx是Apache服务器不错的替代品。
nginx+php是常见的webserver架构模式之一。
nginx 是一个高性能的http服务器和反向代理服务器,但nginx本身并不会对php文件进行解析。对PHP页面的请求将会被nginx交给FastCGI进程监听的IP地址及端口,由php-fpm(第三方的fastcgi进程管理器)作为动态解析服务器处理,最后将处理结果再返回给nginx。即nginx通过反向代理功能将动态请求转向后端php-fpm,从而实现对PHP的解析支持,这就是Nginx实现PHP动态解析的基本原理。
1、安装yum-utils
yum install yum-utils
2、设置yum源,创建文件/etc/yum.repos.d/nginx.repo,
[root@node3 yum.repos.d]# pwd
/etc/yum.repos.d
[root@node3 yum.repos.d]# cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@node3 yum.repos.d]#
3、安装nginx
yum install nginx
4、启动nginx服务: service nginx start
[root@node3 yum.repos.d]# service nginx start
Redirecting to /bin/systemctl start nginx.service
[root@node3 yum.repos.d]# service nginx status
Redirecting to /bin/systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2020-11-03 00:04:56 CST; 3s ago
Docs: http://nginx.org/en/docs/
Process: 68205 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 68207 (nginx)
Tasks: 2
CGroup: /system.slice/nginx.service
├─68207 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─68208 nginx: worker process
Nov 03 00:04:56 node3 systemd[1]: Starting nginx - high performance web server...
Nov 03 00:04:56 node3 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
Nov 03 00:04:56 node3 systemd[1]: Started nginx - high performance web server.
[root@node3 yum.repos.d]#
5、测试nginx
nginx默认访问端口:80(注意关闭防火墙,或开放nginx主机的80端口)
打开浏览器,输入nginx主机ip,出现以下Welcome to nginx界面,则nginx运行正常
1、下载安装包
本人测试环境CentOS 7自带yum源的php版本为5.4, 比较老;本测试采用源码方式安装较新的php 7.2.33版本, 首先下载php 7.2.33源码包(https://www.php.net/downloads.php)
[root@node3 opt]# ls -l php-7.2.33.tar.bz2
-rw-r--r-- 1 root root 15233897 Sep 21 09:01 php-7.2.33.tar.bz2
[root@node3 opt]#
2、安装linux依赖包:
yum install gcc
yum install libxml2*
yum install libssl*
3、解压、编译、安装
tar -xvzf php-7.2.33.tar.bz2
cd php-7.2.33/
./configure –prefix=/opt/php --enable-fpm 注意:需要启用php-fpm
make & make install
1、nginx 1.18配置文件主要有两个:/etc/nginx/nginx.conf、/etc/nginx/conf.d/default.conf,此处需要修改default.conf
在location / {}段后新添加一个外理php页面的Location段,如下:
[root@node3 conf.d]# cat default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# 处理php页面请求 add by qin
location ~ \.php$ {
# 匹配php后缀的请求
root /data/www; # php页面映射本地路径/data/www
fastcgi_pass localhost:9000; # php-fpm本地端口为9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
2、在/data/www路径下,新建文本文件info.php,内容如下
[root@node3 conf.d]# cd /data/www
[root@node3 www]# ls
info.php test.php
[root@node3 www]# cat info.php
<?php
phpinfo();
?>
3、启动php-fpm服务
4、重启nginx
5、访问info.php页面,测试php页面转发配置,出现以下页面则正常
1、上Kingbase官网下载kingbase的php 7.2版本x86_64+linux环境驱动
****注意:官网目前提供的大部分都是关闭php的Thread Safety选项的,如果测试环境开启了线程安全会加载不了驱动
下载地址:https://www.kingbase.com.cn/index/download/c_id/401.html
选择linux-x64环境的驱动包:
pdo_kdb_for_php-7.2.22.tar.gz
解压后得到文件:pdo_kdb.so
[root@node3 kdblib]# ls -l pdo_kdb.so
-rwxr-xr-x 1 1011 1011 189456 Apr 24 2020 pdo_kdb.so
[root@node3 kdblib]#
2、新建目录/opt/kdblib目录,并把kingbase的php驱动放在该路径
[root@node3 kdblib]# pwd
/opt/kdblib
[root@node3 kdblib]# ls -l pdo_kdb.so
-rwxr-xr-x 1 1011 1011 189456 Apr 24 2020 pdo_kdb.so
[root@node3 kdblib]#
3、kingbase的php驱动也依赖libkci库文件(安装Kingbase数据库后,libkci库文件在安装目录Server/lib子路径下),把libkci库文件拷贝至/opt/kdblib
4、把/opt/kdblib加入LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/opt/kdblib:$LD_LIBRARY_PATH
5、查看php配置文件路径
[root@node3 etc]# php -i|grep php.ini
Configuration File (php.ini) Path => /opt/php/lib
Loaded Configuration File => /opt/php/lib/php.ini
[root@node3 etc]#
6、编辑/opt/php/lib/php.ini, 把pdo_kdb.so所在路径加入extension_dir
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "/opt/kdblib"
extension=/opt/kdblib/pdo_kdb.so
7、重新访问info.php页面,查看Kingbase驱动是否加载成功
页面上有以下内容则加载成功
8、启动kingbase服务,新建连接kingbase数据库测试页面test.php
[root@node3 www]# cat test.php
<?php
$dsn = 'kdb:dbname=TEST;host=192.168.184.136;port=54321';
$user = 'SYSTEM';
$password = '******';
;try {
$dbh = new PDO($dsn, $user, $password);
;} catch (PDOException $e) {
; echo '数据库连接失败: ' . $e->getMessage();
;}
if(!$dbh){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
?>
[root@node3 www]#
9、浏览器访问test.php页面
出现以下内容则连接Kingbase数据库成功
1、http://nginx.org/en/linux_packages.html#RHEL-CentOS
2、https://www.kingbase.com.cn/index/download/c_id/401.html
3、https://www.php.net/downloads.php