去除php7编译安装的模块sqlite3

我是研究僧i 提交于 2020-08-18 21:45:28

去除php7编译安装的模块,这里以sqlite3为例

SQLite3 扩展自 PHP 5.3.0 起已默认启用。 允许在编译时使用 --without-sqlite3 禁用之

背景

我在开发项目的时候需要使用php连接加密的sqlite数据库,但是php默认编译的sqlite扩展不支持连接加密的sqlite数据库,因此我必须把编译安装的sqlite3移除掉,安装支持连接加密的sqlite数据库的扩展

前提

假设你已经编译安装好了php7,且在编译安装php7的时候未显示禁止 --without-sqlite3,导致这个模块已经被编译安装进php,满足这个前提下,再来看这篇文章 如果不知道如何编译安装php7,请参考这篇文章centos7编译安装php7 

查看文章的过程中,一定要注意各个命令的操作路径,需要将这些路径替换成你自己开发环境中的路径,切记 文章中php7.1 命令只是对php命令的一个软连接,如果不清楚,请仔细阅读这篇文章centos7编译安装php7 

查看php安装好的模块

[root@localhost ~]# php7.1 -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
libxml
mbstring
mcrypt
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_dblib
pdo_mysql
pdo_sqlite
Phar
posix
rdkafka
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
swoole
sysvsem
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
zip
zlib

[Zend Modules]
Xdebug

[root@localhost ~]#

可以看到sqlite3确实已经被安装好了,因为这是php默认启用的,所以在php.ini文件中,你是看不到这个扩展有被显示启用也就是类似这样的代码

extension=sqlite3.so

重新编译php

进入php的源码文件,也就是你当初编译安装php时的文件,如果你已经删除了,就使用php7.1 -v 查看php的版本,然后去php的官网,重新下载php的源码即可,获取源码的方式可以参考这个文章,当然你也可以自己去找

  1. 进入源码文件
[root@localhost php-src-php-7.1.6]# pwd
/usr/src/php7.1.6/php-src-php-7.1.6
[root@localhost php-src-php-7.1.6]#
  1. 清除历史编译文件
[root@localhost php-src-php-7.1.6]# make clean
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f 
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp7.la sapi/cli/php sapi/cgi/php-cgi   sapi/fpm/php-fpm  modules/* libs/*
  1. 重新生成编译配置
[root@localhost php-src-php-7.1.6]# ./buildconf --force
Forcing buildconf
Removing configure caches
  1. 查看历史编译配置
[root@localhost php-src-php-7.1.6]# php7.1 -i | grep configure
Configure Command =>  './configure'  '--prefix=/usr/local/php7.1' '--exec-prefix=/usr/local/php7.1' '--bindir=/usr/local/php7.1/bin' '--sbindir=/usr/local/php7.1/sbin' '--includedir=/usr/local/php7.1/include' '--libdir=/usr/local/php7.1/lib/php' '--mandir=/usr/local/php7.1/php/man' '--with-config-file-path=/etc/php7.1.6' '--with-mysql-sock=/tmp/mysql.sock' '--with-mcrypt' '--with-mhash' '--with-openssl' '--with-mysqli=shared,mysqlnd' '--with-pdo-mysql=shared,mysqlnd' '--with-gd' '--with-iconv' '--with-zlib' '--enable-zip' '--enable-inline-optimization' '--disable-debug' '--disable-rpath' '--enable-shared' '--enable-xml' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-mbregex' '--enable-mbstring' '--enable-ftp' '--enable-gd-native-ttf' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-soap' '--without-pear' '--with-gettext' '--enable-session' '--with-curl' '--with-jpeg-dir' '--with-freetype-dir' '--enable-opcache' '--enable-fpm' '--with-fpm-user=nginx' '--with-fpm-group=nginx' '--without-gdbm' '--enable-fast-install' '--disable-fileinfo'
  1. 修改编译配置如下
./configure --prefix=/usr/local/php7.1 --exec-prefix=/usr/local/php7.1 --bindir=/usr/local/php7.1/bin --sbindir=/usr/local/php7.1/sbin --includedir=/usr/local/php7.1/include --libdir=/usr/local/php7.1/lib/php --mandir=/usr/local/php7.1/php/man --with-config-file-path=/etc/php7.1.6 --with-mysql-sock=/tmp/mysql.sock --with-mcrypt --with-mhash --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --enable-fast-install --disable-fileinfo --without-sqlite3 --without-pdo_sqlite

注意将里面的文件路径换成你本地的,这个编译参数最主要的是在历史的编译参数的末尾加上了--without-sqlite3 --without-pdo_sqlite,这作用就是去除原来默认编译安装的sqlite3模块,而without-pdo_sqlite也去掉,是因为它依赖sqlite3,如果不去掉,是会报错的

  1. 运行编译配置
[root@localhost php-src-php-7.1.6]# ./configure --prefix=/usr/local/php7.1  --exec-prefix=/usr/local/php7.1  --bindir=/usr/local/php7.1/bin  --sbindir=/usr/local/php7.1/sbin  --includedir=/usr/local/php7.1/include  --libdir=/usr/local/php7.1/lib/php  --mandir=/usr/local/php7.1/php/man  --with-config-file-path=/etc/php7.1.6  --with-mysql-sock=/tmp/mysql.sock  --with-mcrypt  --with-mhash  --with-openssl   --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd  --with-gd  --with-iconv  --with-zlib  --enable-zip  --enable-inline-optimization  --disable-debug  --disable-rpath  --enable-shared  --enable-xml  --enable-bcmath  --enable-shmop  --enable-sysvsem  --enable-mbregex  --enable-mbstring  --enable-ftp  --enable-gd-native-ttf  --enable-pcntl  --enable-sockets  --with-xmlrpc  --enable-soap  --without-pear  --with-gettext  --enable-session  --with-curl  --with-jpeg-dir  --with-freetype-dir  --enable-opcache  --enable-fpm  --with-fpm-user=nginx  --with-fpm-group=nginx  --without-gdbm  --enable-fast-install  --disable-fileinfo --without-sqlite3 --without-pdo_sqlite

# 省略部分输出,如果最后出现如下提示,表示没有错误,可以进行下一步的make


Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
[root@localhost php-src-php-7.1.6]#
  1. 进行make

加-j 8,是为了加快编译速度

[root@localhost php-src-php-7.1.6]# make -j 8

出现如下提示表示make成功

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
directorygraphiterator.inc
directorytreeiterator.inc
invertedregexiterator.inc
pharcommand.inc
phar.inc

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

  1. 安装
[root@localhost php-src-php-7.1.6]# make install
Installing shared extensions:     /usr/local/php7.1/lib/php/extensions/no-debug-non-zts-20160303/
Installing PHP CLI binary:        /usr/local/php7.1/bin/
Installing PHP CLI man page:      /usr/local/php7.1/php/man/man1/
Installing PHP FPM binary:        /usr/local/php7.1/sbin/
Installing PHP FPM defconfig:     skipping
Installing PHP FPM man page:      /usr/local/php7.1/php/man/man8/
Installing PHP FPM status page:   /usr/local/php7.1/php/php/fpm/
Installing phpdbg binary:         /usr/local/php7.1/bin/
Installing phpdbg man page:       /usr/local/php7.1/php/man/man1/
Installing PHP CGI binary:        /usr/local/php7.1/bin/
Installing PHP CGI man page:      /usr/local/php7.1/php/man/man1/
Installing build environment:     /usr/local/php7.1/lib/php/build/
Installing header files:          /usr/local/php7.1/include/php/
Installing helper programs:       /usr/local/php7.1/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php7.1/php/man/man1/
  page: phpize.1
  page: php-config.1
/usr/src/php7.1.6/php-src-php-7.1.6/build/shtool install -c ext/phar/phar.phar /usr/local/php7.1/bin
ln -s -f phar.phar /usr/local/php7.1/bin/phar
Installing PDO headers:           /usr/local/php7.1/include/php/ext/pdo/
  1. 替换原来安装的php7.1-fpm,这个很重要,如果不替换当重启php7.1-fpm会报错

如果不知道php7.1-fpm是怎么来的,请认真阅读这个文章

[root@localhost php-src-php-7.1.6]# cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php7.1-fpm
cp: overwrite ‘/etc/init.d/php7.1-fpm’? y
[root@localhost php-src-php-7.1.6]#
  1. 重启php7.1-fpm
[root@localhost php-src-php-7.1.6]# service php7.1-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm [18-Jun-2020 15:02:39] NOTICE: PHP message: PHP Warning:  Module 'pdo_mysql' already loaded in Unknown on line 0
[18-Jun-2020 15:02:39] NOTICE: PHP message: PHP Warning:  Module 'mysqli' already loaded in Unknown on line 0
 done
[root@localhost php-src-php-7.1.6]#

会报错类似上面的错误,那就去php.ini文件中将对应模块注释就好了,注意这里的注释并不会影响这些模块的实际使用

  1. 注释对应模块,本例中是pdo_mysql和mysqli
[root@localhost php-src-php-7.1.6]# vim /etc/php7.1.6/php.ini

;extension=pdo_mysql.so
;extension=mysqli.so
  1. 重新启动php7.1-fpm,如下就代表启动成功了
[root@localhost php-src-php-7.1.6]# service php7.1-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
  1. 查看php的模块,会发现sqlite3模块已经被移除了,而其他模块依然存在,至此模块已经移除成功,如果你不需要再通过扩展安装sqlite3模块,你就不需要再往下看了
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
libxml
mbstring
mcrypt
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_dblib
pdo_mysql
Phar
posix
rdkafka
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
standard
swoole
sysvsem
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
zip
zlib

[Zend Modules]
Xdebug

[root@localhost sqlite3]#

进入php的sqlite3源码重新编译安装sqlite3扩展

  1. 进入php的sqlite3源码重新编译扩展
[root@localhost php-src-php-7.1.6]# cd ext/sqlite3/
[root@localhost sqlite3]# pwd
/usr/src/php7.1.6/php-src-php-7.1.6/ext/sqlite3
[root@localhost sqlite3]#  /usr/local/php7.1/bin/phpize
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303
[root@localhost sqlite3]# ./configure --with-php-config=/usr/local/php7.1/bin/php-config
-bash: ./configure: No such file or directory

会报错./configure: No such file or directory,解决方法如下

[root@localhost sqlite3]# cp config0.m4 config.m4
[root@localhost sqlite3]# ./configure --with-php-config=/usr/local/php7.1/bin/php-config
  1. make并且安装
[root@localhost sqlite3]# make && make install
#省略过程
Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php7.1/lib/php/extensions/no-debug-non-zts-20160303/
Installing header files:          /usr/local/php7.1/include/php/
[root@localhost sqlite3]# 

  1. 进入扩展目录,使用我提供的sqlite3.so文件,替换目录里面的这个sqlite3.so文件,并修改文件权限为755,替换的sqlite3.so文件,会在文章结尾处提供
[root@localhost sqlite3]# cd /usr/local/php7.1/lib/php/extensions/no-debug-non-zts-20160303/
[root@localhost no-debug-non-zts-20160303]# ll
total 30820
-rwxr-xr-x 1 root root  3618150 Nov 26  2018 fileinfo.so
-rwxr-xr-x 1 root root  1375702 May 12  2019 mysqli.a
-rwxr-xr-x 1 root root   649400 May 12  2019 mysqli.so
-rwxr-xr-x 1 root root  3419404 Jun 18 14:59 opcache.a
-rwxr-xr-x 1 root root  1635776 Jun 18 14:59 opcache.so
-rwxr-xr-x 1 root root   124455 Nov 20  2018 pdo_dblib.so
-rwxr-xr-x 1 root root   497966 Nov  6  2018 pdo_mysql.a
-rwxr-xr-x 1 root root   229814 Nov  6  2018 pdo_mysql.so
-rwxr-xr-x 1 root root   348013 Nov 19  2018 pdo_sqlsrv.so
-rwxr-xr-x 1 root root   466649 Jun 13  2019 rdkafka.so
-rwxr-xr-x 1 root root  2379481 May 13  2019 redis.so
-rwxr-xr-x 1 root root  4718648 Jun 18 15:22 sqlite3.so
-rwxr-xr-x 1 root root 10758700 May 10  2019 swoole.so
-rwxr-xr-x 1 root root  1311591 Nov 20  2018 xdebug.so
[root@localhost no-debug-non-zts-20160303]# pwd
/usr/local/php7.1/lib/php/extensions/no-debug-non-zts-20160303
[root@localhost no-debug-non-zts-20160303]# rm -rf sqlite3.so

# 删除后,记得上传我提供的sqlite3.so 文件

[root@localhost no-debug-non-zts-20160303]# chmod 755 sqlite3.so 
[root@localhost no-debug-non-zts-20160303]# ll
total 27044
-rwxr-xr-x 1 root root  3618150 Nov 26  2018 fileinfo.so
-rwxr-xr-x 1 root root  1375702 May 12  2019 mysqli.a
-rwxr-xr-x 1 root root   649400 May 12  2019 mysqli.so
-rwxr-xr-x 1 root root  3419404 Jun 18 14:59 opcache.a
-rwxr-xr-x 1 root root  1635776 Jun 18 14:59 opcache.so
-rwxr-xr-x 1 root root   124455 Nov 20  2018 pdo_dblib.so
-rwxr-xr-x 1 root root   497966 Nov  6  2018 pdo_mysql.a
-rwxr-xr-x 1 root root   229814 Nov  6  2018 pdo_mysql.so
-rwxr-xr-x 1 root root   348013 Nov 19  2018 pdo_sqlsrv.so
-rwxr-xr-x 1 root root   466649 Jun 13  2019 rdkafka.so
-rwxr-xr-x 1 root root  2379481 May 13  2019 redis.so
-rwxr-xr-x 1 root root   855199 Sep 17  2019 sqlite3.so
-rwxr-xr-x 1 root root 10758700 May 10  2019 swoole.so
-rwxr-xr-x 1 root root  1311591 Nov 20  2018 xdebug.so
[root@localhost no-debug-non-zts-20160303]# 

  1. 修改php.ini开启sqlite3.so扩展
[root@localhost no-debug-non-zts-20160303]# vim /etc/php7.1.6/php.ini
extension=sqlite3.so
  1. 重启php7.1-fpm
[root@localhost no-debug-non-zts-20160303]# service php7.1-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

至此全部完成,你就可以写代码去连接加密的sqlite库了

sqlite.so文件地址

链接:https://pan.baidu.com/s/1zE3i_Dh2Z2ew_IaptSfjBg 
提取码:wbxt

如果有疑问,可以在评论区提问,看到会回复,转载请注明出处

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!