沙滩星空的博客沙滩星空的博客

docker的PHP容器中安装扩展模块

PHP容器服务,基于下面镜像创建。最小化安装,很多扩展都没有。

  docker pull php:7.2-fpm

php.ini配置项:

/usr/local/etc/php/conf.d 目录下面的 ini 配置文件会自动加载。

vim output_buffering.ini
output_buffering=4096
Docker pull php:7.1-fpm的php.ini配置修改 https://58hualong.cn/blog/post/docker-pull-php71-fpm-php-ini-de-peizhi-xiougai

PHP容器内重启php-fpm

容器内默认不支持ps进程查询命令,安装之:

apt-get install procps

查询php-fpm进程

ps aux|grep php
1 root 0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf) 
7 www-data 0:00 php-fpm: pool www 
8 www-data 0:00 php-fpm: pool www

重启第一个php-fpm进程

kill -USR2 1

关于/conf.d目录不存在的报错

例如安装某些php扩展时,可能报错/conf.d路径不存在。在根目录创建后,执行模块安装命令docker-php-ext-install ,然后将/conf.d里的文件移动到/usr/local/etc/php/conf.d目录即可。

例如安装mysqli扩展报错:

docker-php-ext-install mysqli
/usr/local/bin/docker-php-ext-enable: 108: /usr/local/bin/docker-php-ext-enable: cannot create /conf.d/docker-php-ext-mysqli.ini: Directory nonexistent

/conf.d目录不存在,无法创建docker-php-ext-mysqli.ini文件
在根目录创建conf.d目录:

mkdir /conf.d

安装扩展:

docker-php-ext-install mysqli

把mysqli.ini文件移动到正常的目录

mv /conf.d/docker-php-ext-mysqli.ini /usr/local/etc/php/conf.d/

查看,确认

cd /usr/local/etc/php/conf.d/
ls
docker-php-ext-intl.ini    docker-php-ext-pdo_mysql.ini docker-php-ext-mysqli.ini  docker-php-ext-sodium.ini
cat docker-php-ext-mysqli.ini
extension=mysqli.so

重启php


安装intel扩展

symfony4框架运行的时候,提示缺少intel扩展。网上的文章,基本上都是运行如下命令:

docker-php-ext-install intl

pecl install intl

运行后直接提示缺少ICU依赖:

configure: error: Unable to detect ICU prefix

运行yum安装libicu-devel失败

yum install libicu-devel

查看Linux版本:

cat /etc/redhat-release
cat: /etc/redhat-release: No such file or directory
cat /etc/issue
Debian GNU/Linux 9 \n \l
uname -a
Linux f99b6692e5b9 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64 GNU/Linux
cat /proc/version
Linux version 3.10.0-957.5.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Fri Feb 1 14:54:57 UTC 2019

发现容器为Debian而非CentOS.
查找Debian的libicu库

apt-cache search libicu | grep libicu

结果如下:

libicu-dev - Development files for International Components for Unicode
libicu52 - International Components for Unicode
libicu52-dbg - International Components for Unicode
libicu4j-java - Library for Unicode support and internationalization
libicu4j-4.4-java - Library for Unicode support and internationalization
libicu4j-49-java - Library for Unicode support and internationalization
libicu57 - International Components for Unicode

1.修改sources.list文件。更换apt-get为国内源。
2.选择第一个libicu-dev.运行apt-get install命令:

apt-get install libicu-dev

3.提示找不到libicu-dev, 故执行apt-get update更新源

apt-get update

如果网络非常卡,可能1个小时都弄不完,请更换apt软件源
4.继续安装intel扩展

docker-php-ext-install intl

Done!


安装mysql扩展和pdo_mysql扩展
PHP5.6环境下:Discuz X3.2出现undefined function mysql_escape_string()报错,可安装mysql扩展

Fatal error: Call to undefined function mysql_escape_string() in /xxx.com/public_html/source/class/discuz/discuz_database.php on line 177

还有PHP7经常使用的pdo_mysql扩展

docker-php-ext-install mysql
docker-php-ext-install pdo_mysql

安装GD扩展

docker-php-ext-install gd

如下报错:

checking whether to enable JIS-mapped Japanese font support in GD... no
If configure fails try --with-webp-dir=<DIR>
If configure fails try --with-jpeg-dir=<DIR>
configure: error: png.h not found.

网上说是libpng,devel包没安装
更新软件包源(略),并安装依赖:

apt-get update
apt-get install libpng libpng-devel

报错:

Reading package lists... Done

Building dependency tree       
Reading state information... Done
E: Unable to locate package libpng
E: Unable to locate package libpng-devel

照抄网上的文章害死人,下面自己手动找下debian下的png库的软件名,选择:libpng-dev,再加一个libjpeg-dev

apt-cache search libpng | grep libpng
libpng-dev - PNG library - development (version 1.6)
libpng-tools - PNG library - tools (version 1.6)
libpng16-16 - PNG library - runtime (version 1.6)
libpng++-dev - C++ interface to the PNG (Portable Network Graphics) library
libpng-sixlegs-java - Sixlegs Java PNG Decoder
libpng-sixlegs-java-doc - Documentation for Sixlegs Java PNG Decoder
libpnglite-dev - lightweight C library for loading and writing PNG images
libpnglite0 - lightweight C library for loading and writing PNG images

下面是验证通过后的安装流程:

1. 安装libpng-dev和libjpeg-dev依赖:

apt-get install libpng-dev libjpeg-dev

GD扩展不编译,直接安装。使用imagettftext()会报错函数不存在。编译前,必须开启FreeType支持,否则报错

configure: error: freetype-config not found.

2. 安装FreeType库

apt-get install libfreetype6-dev 

3. 编译gd模块

docker-php-ext-configure gd --enable-gd-native-ttf --with-freetype-dir=/usr/include/freetype2 --with-png-dir=/usr/include --with-jpeg-dir=/usr/include

PHP7.2和PHP7.3编译时开启--enable-gd-native-ttf会报错。因为该选项已被废弃。

configure: WARNING: unrecognized options: –enable-gd-native-ttf

去掉该选项即可:

docker-php-ext-configure gd --with-freetype-dir=/usr/include/freetype2 --with-png-dir=/usr/include --with-jpeg-dir=/usr/include

4. 安装扩展:

docker-php-ext-install gd


安装ZIP扩展

docker-php-ext-install zip

重点:apt源必须选对,否则会各种依赖报错,软件版本不兼容。比如debian9不能选择debian8的源

中科大源:http://mirrors.ustc.edu.cn/help/debian.html

docker完整配置nginx+php+mysql的方法步骤 https://m.jb51.net/article/156426.htm
php7.3 -enable-gd-native-ttf 已被废弃 https://blog.qzze.com/813.html
更改apt源 https://blog.catmes.com/archives/apt-sources.html
configure: error: png.h not found.解决 https://www.zyku.net/gongju/1709.html

docker 中安装PHP扩展 https://www.cnblogs.com/lglblogadd/p/9173627.html
Debian和Ubuntu更换apt软件源 https://blog.catmes.com/archives/apt-sources.html
未经允许不得转载:沙滩星空的博客 » docker的PHP容器中安装扩展模块

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址