Linux+Apache 2.4+PHP 5.4+达梦数据库

环境说明

操作系统:Centos 7

Apache:2.4.6

PHP:5.4.16

达梦数据库:DM V8 1-2-38

php7版本连接达梦数据库参考:
PHP | 达梦技术社区文档 (eco.dameng.com)
达梦E学公众号:php7.4连接DM数据库环境搭建

环境安装

Apache和PHP可以采用源码安装,也可以使用yum直接安装。这里使用yum直接安装。

防火墙和SELINUX,可以提前放开相应策略或者关闭。

配置本地yum源

备份移除所有自带的repo文件,创建本地yum仓库文件

/etc/yum.repos.d/cdrom.repo

[Centos7.8]
name= Centos 7.8
baseurl=file:///cdrom
enabled=1
gpgcheck=0

安装Apache环境

yum install -y httpd

[root@localhost ~]# yum install -y httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Centos7.8                                                                         | 3.6 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-93.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-93.el7.centos for package: httpd-2.4.6-93.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-93.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-93.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-93.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-93.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================
 Package                 Arch               Version                          Repository             Size
=========================================================================================================
Installing:
 httpd                   x86_64             2.4.6-93.el7.centos              Centos7.8             2.7 M
Installing for dependencies:
 apr                     x86_64             1.4.8-5.el7                      Centos7.8             103 k
 apr-util                x86_64             1.5.2-6.el7                      Centos7.8              92 k
 httpd-tools             x86_64             2.4.6-93.el7.centos              Centos7.8              92 k
 mailcap                 noarch             2.1.41-2.el7                     Centos7.8              31 k

Transaction Summary
=========================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 3.0 M
Installed size: 10 M
Downloading packages:
---------------------------------------------------------------------------------------------------------
Total                                                                     10 MB/s | 3.0 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                                                1/5 
  Installing : apr-util-1.5.2-6.el7.x86_64                                                           2/5 
  Installing : httpd-tools-2.4.6-93.el7.centos.x86_64                                                3/5 
  Installing : mailcap-2.1.41-2.el7.noarch                                                           4/5 
  Installing : httpd-2.4.6-93.el7.centos.x86_64                                                      5/5 
  Verifying  : apr-1.4.8-5.el7.x86_64                                                                1/5 
  Verifying  : httpd-tools-2.4.6-93.el7.centos.x86_64                                                2/5 
  Verifying  : mailcap-2.1.41-2.el7.noarch                                                           3/5 
  Verifying  : httpd-2.4.6-93.el7.centos.x86_64                                                      4/5 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                           5/5 

Installed:
  httpd.x86_64 0:2.4.6-93.el7.centos                                                                     

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7       apr-util.x86_64 0:1.5.2-6.el7  httpd-tools.x86_64 0:2.4.6-93.el7.centos 
  mailcap.noarch 0:2.1.41-2.el7 

Complete!
[root@localhost ~]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Apr  2 2020 13:13:23
[root@localhost ~]# systemctl start httpd

安装PHP

Centos 7 中自带的PHP版本为5.4(Centos 8中自带的PHP版本为7.2)

安装php和php-pdo

php -v

php -m

[root@localhost yum.repos.d]# yum install -y php php-pdo
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.4.16-48.el7 will be installed
--> Processing Dependency: php-common(x86-64) = 5.4.16-48.el7 for package: php-5.4.16-48.el7.x86_64
--> Processing Dependency: php-cli(x86-64) = 5.4.16-48.el7 for package: php-5.4.16-48.el7.x86_64
---> Package php-pdo.x86_64 0:5.4.16-48.el7 will be installed
--> Running transaction check
---> Package php-cli.x86_64 0:5.4.16-48.el7 will be installed
---> Package php-common.x86_64 0:5.4.16-48.el7 will be installed
--> Processing Dependency: libzip.so.2()(64bit) for package: php-common-5.4.16-48.el7.x86_64
--> Running transaction check
---> Package libzip.x86_64 0:0.10.1-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================
 Package               Arch              Version                   Repository            Size
==============================================================================================
Installing:
 php                   x86_64            5.4.16-48.el7             Centos7.8            1.4 M
 php-pdo               x86_64            5.4.16-48.el7             Centos7.8             99 k
Installing for dependencies:
 libzip                x86_64            0.10.1-8.el7              Centos7.8             48 k
 php-cli               x86_64            5.4.16-48.el7             Centos7.8            2.7 M
 php-common            x86_64            5.4.16-48.el7             Centos7.8            565 k

Transaction Summary
==============================================================================================
Install  2 Packages (+3 Dependent packages)

Total download size: 4.8 M
Installed size: 17 M
Downloading packages:
----------------------------------------------------------------------------------------------
Total                                                          55 MB/s | 4.8 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libzip-0.10.1-8.el7.x86_64                                                 1/5 
  Installing : php-common-5.4.16-48.el7.x86_64                                            2/5 
  Installing : php-cli-5.4.16-48.el7.x86_64                                               3/5 
  Installing : php-5.4.16-48.el7.x86_64                                                   4/5 
  Installing : php-pdo-5.4.16-48.el7.x86_64                                               5/5 
  Verifying  : php-5.4.16-48.el7.x86_64                                                   1/5 
  Verifying  : php-pdo-5.4.16-48.el7.x86_64                                               2/5 
  Verifying  : libzip-0.10.1-8.el7.x86_64                                                 3/5 
  Verifying  : php-cli-5.4.16-48.el7.x86_64                                               4/5 
  Verifying  : php-common-5.4.16-48.el7.x86_64                                            5/5 

Installed:
  php.x86_64 0:5.4.16-48.el7                  php-pdo.x86_64 0:5.4.16-48.el7                 

Dependency Installed:
  libzip.x86_64 0:0.10.1-8.el7                    php-cli.x86_64 0:5.4.16-48.el7              
  php-common.x86_64 0:5.4.16-48.el7              

Complete!
[root@localhost ~]# php -v
PHP 5.4.16 (cli) (built: Apr  1 2020 04:07:17) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

[root@localhost lib64]# php -m
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
dm
ereg
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
json
libxml
mhash
openssl
pcntl
pcre
PDO
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
xml
zip
zlib

[Zend Modules]

Apache和PHP安装完成后,需要检查Apache配置文件是否已加载php库文件,默认是已自动配置加载。

测试PHP环境

创建php测试页面:

[root@localhost ~]# echo "<?php phpinfo();?>" >/var/www/html/index.php

测试:IP/index.php

Linux+Apache 2.4+PHP 5.4+达梦数据库

安装达梦数据库软件

参考:达梦技术社区:Linux 达梦数据库安装

只需要安装软件,或者将已安装达梦数据库软件的bin目录拷贝到服务器上。

使用达梦php驱动时需要应用相关依赖库。

配置PHP加载达梦驱动

(1)首先拷贝达梦数据库PHP接口驱动文件到php服务器上,达梦数据库PHP驱动存放在数据库安装目录下drivers/php_pdo目录下

Linux+Apache 2.4+PHP 5.4+达梦数据库

需要注意,如果PHP版本是TS版,需要选择对应的TS版本的达梦数据库dm php和dm pdo驱动库文件。拷贝对应驱动文件到php默认的扩展模块路径下。

使用yum安装的自带的PHP,默认扩展模块的路径为/usr/lib64/php/modules,可以将对应版本的驱动文件拷贝到/usr/lib64/php/modules下。也可以在php.ini中指定扩展模块的路径。

默认扩展模块路径查看: php -i|grep -i extension_dir

(2)修改PHP配置文件php.ini,添加达梦数据库模块库文件

方式1:

php.ini文件中添加:

extension=libphp54_dm.so

/etc/php.d/pdo.ini文件中添加:

extension=php54_pdo_dm.so
##不要添加在最上面

方式2:

在/etc/php.d目录下,创建两个达梦驱动配置文件dm8.ini和pdo_dm8.ini,分别配置达梦php驱动扩展和pdo驱动扩展。

[root@localhost php.d]# pwd
/etc/php.d
[root@localhost php.d]# cat dm8.ini 
extension=libphp54_dm.so

[root@localhost php.d]# cat pdo_dm8.ini 
extension=php54_pdo_dm.so

(3)配置LD_LIBRARY_PATH环境变量

##临时生效:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/dmdbms/bin
##永久生效,添加到/etc/profile配置文件中

(4)检查dm扩展模块

方法1:通过php -m命令查看

Linux+Apache 2.4+PHP 5.4+达梦数据库

方法2:重启httpd服务,然后通过前面的index.php测试页面查看

Linux+Apache 2.4+PHP 5.4+达梦数据库

Linux+Apache 2.4+PHP 5.4+达梦数据库

测试连接达梦数据库

新建一个测试php页面:

[root@localhost html]# pwd
/var/www/html
[root@localhost html]# cat test.php 
<?php
try {
$link = dm_connect("192.168.15.35", "SYSDBA", "SYSDBA") or die("Could not connect : " . dm_error());
print "Connected successfully!数据库版本为\n";
$query = "select *,id_code from v\$version where rownum=1;";
$result = dm_query($query) or die("Query failed : " . dm_error());
while ($line = dm_fetch_array($result, DM_ASSOC)) {
foreach ($line as $col_value) {
print_r ($col_value);
}
}
dm_free_result($result);
dm_close($link);
}
catch(PDOException $e){
   print( "Error: " . $e->getMessage())."\n";
}
?>

通过访问IP/test.php,测试是否连接数据库成功

Linux+Apache 2.4+PHP 5.4+达梦数据库

问题处理

报错1 PHP Startup: Unable to load dynamic library

pdo模块加载失败

[root@localhost ~]# php -v
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/php54_pdo_dm.so' - /usr/lib64/php/modules/php54_pdo_dm.so: undefined symbol: php_pdo_register_driver in Unknown on line 0
PHP 5.4.16 (cli) (built: Apr  1 2020 04:07:17) 

处理办法:

方法1:在php 5.4版本中,pdo扩展配置文件在/etc/php.d/pdo.ini,将dm php pdo驱动添加到pdo.ini文件中

[root@localhost php.d]# cat pdo.ini
; Enable pdo extension module
extension=pdo.so
extension=php54_pdo_dm.so

##注意extension=pdo.so要放到最上面

方法2:在/etc/php.d目录下,创建两个达梦驱动配置文件dm8.ini和pdo_dm8.ini,分别配置达梦php驱动扩展和pdo驱动扩展。

[root@localhost php.d]# cat pdo_dm8.ini 
extension=php54_pdo_dm.so

报错2 PHP Fatal error:Unable to start dm module

[root@localhost php.d]# php -m
PHP Fatal error:  Unable to start PDO_DM module in Unknown on line 0
或者
[root@localhost lib64]# php -m
PHP Fatal error:  Unable to start dm module in Unknown on line 0

处理办法:

方法1:LD_LIBRARY_PATH环境变量,指定为达梦数据库软件安装目录下的bin目录

##临时生效:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/dmdbms/bin
##永久生效,添加到/etc/profile配置文件中

方法2:见报错3的处理办法。

报错3 PHP正常,httpd服务启动报错

某些环境下(php 5.*)php -m可以看到已正常加载dm和PDO_DM扩展模块,但是重启httpd服务时报错,httpd的error_log中报错信息如下:

[root@localhost bin]# systemctl start httpd
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

###httpd error_log报错信息:
PHP Fatal error:  Unable to start dm module in Unknown on line 0

这个很奇怪,因为单独使用php查看相关扩展模块的情况都是成功的,而且使用php测试连接数据库也是正常的,但是启动httpd服务就会报错。

初步判断可能是httpd服务启动时用户环境变量加载动态链接库出现问题。

处理办法:

(1)在/etc/ld.so.conf.d目录下创建dm8.conf配置文件:

[root@localhost ld.so.conf.d]# cat dm8.conf 
/opt/dmdbms/bin

(2)root执行ldconfig命令生效

[root@localhost ld.so.conf.d]# ldconfig 
ldconfig: /opt/dmdbms/bin/libxerces-c-3.1.so is not a symbolic link
##这个报错可以忽略

(3)然后启动httpd服务

[root@localhost ld.so.conf.d]# systemctl restart httpd
[root@localhost ld.so.conf.d]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1180/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1186/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1437/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      3614/sshd: root@pts 
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      3916/sshd: root@pts 
tcp        0      0 127.0.0.1:6012          0.0.0.0:*               LISTEN      4809/sshd: root@pts 
tcp        0      0 127.0.0.1:6013          0.0.0.0:*               LISTEN      4954/sshd: root@pts 
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      700/rpcbind         
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1433/dnsmasq        
tcp6       0      0 :::22                   :::*                    LISTEN      1180/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1186/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1437/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      3614/sshd: root@pts 
tcp6       0      0 ::1:6011                :::*                    LISTEN      3916/sshd: root@pts 
tcp6       0      0 ::1:6012                :::*                    LISTEN      4809/sshd: root@pts 
tcp6       0      0 ::1:6013                :::*                    LISTEN      4954/sshd: root@pts 
tcp6       0      0 :::111                  :::*                    LISTEN      700/rpcbind         
tcp6       0      0 :::80                   :::*                    LISTEN      53798/httpd    ---->已启动

报错4 页面访问报错 Create SOCKET connection failure

页面访问报错:

Error: SQLSTATE[HY000] dpi_login: -70028 Create SOCKET connection failure 

处理办法:

分析日志可以发现系统开启了selinux策略,关闭selinux即可。

(1)修改/etc/selinux/config ,将SELINUX设置为disabled,重启生效。

SELINUX=disabled

(2)执行setenforce 0 设置为Permissive模式

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive

更多资讯请上达梦技术社区了解:https://eco.dameng.com

上一篇:MysqL应该考虑到的安全策略


下一篇:PHP中的PDO对象操作学习(一)初始化PDO及原始SQL语句操作