docker中安装php扩展

php自带的一些扩展

在Dockerfile文件中直接使用关键字 docker-php-ext-install

RUN docker-php-ext-install -j$(nproc) iconv \
  && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
  && docker-php-ext-install -j$(nproc) gd
  && docker-php-ext-install pdo_mysql \
  && docker-php-ext-install bcmath \
  && docker-php-ext-install sockets \
  && docker-php-ext-install zip \
  && docker-php-ext-install sysvmsg 

pecl的扩展

在该网站上https://pecl.php.net的扩展可以使用pecl install安装,例如redis swoole等扩展,使用该命令安装时需要注意在镜像中是否有pecl命令,没有则安装

RUN pecl install swoole-4.2.12 \
    && docker-php-ext-enable swoole \
    && pecl install inotify-2.0.0 \
    && docker-php-ext-enable inotify 

pecl install后会先从该https://pecl.php.net上下载包,然后在安装,所以以下方式也是可以的。

RUN wget http://pecl.php.net/get/redis-${PHPREDIS_VERSION}.tgz -O /tmp/redis.tar.tgz \
    && pecl install /tmp/redis.tar.tgz \
    && rm -rf /tmp/redis.tar.tgz \
    && docker-php-ext-enable redis

下载安装包安装扩展

有的扩展是上面俩种方式没有的或者是想用源码安装的,就可以使用这种方式。其实上面俩种方法本质也是下载安装的方法,所以也要确认phpize等命令是否存在,如果没有配置phpize的环境变量,得写全路径。,也要查看是否安装make命令

以下是swoft下载swoole扩展安装方法

RUN wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \
    && mkdir -p swoole \
    && tar -xf swoole.tar.gz -C swoole --strip-components=1 \
    && rm swoole.tar.gz \
    && ( \
        cd swoole \
        && phpize \
        && ./configure --enable-mysqlnd --enable-sockets --enable-openssl --enable-http2 \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r swoole \
    && docker-php-ext-enable swoole 

以下是php官网下载扩展方法

扫描二维码关注公众号,回复: 9864030 查看本文章
FROM php:5.6-cli
RUN curl -fsSL 'https://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz' -O xcache.tar.gz \
    && mkdir -p xcache \
    && tar -xf xcache.tar.gz -C xcache --strip-components=1 \
    && rm xcache.tar.gz \
    && ( \
        cd xcache \
        && phpize \
        && ./configure --enable-xcache \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r xcache \
    && docker-php-ext-enable xcache

更详细的用法请看docker php镜像说明
使用这种方法需要注意以下几点:

  1. RUN 的最后一条命令不能有斜杠,否则报错, 如以下
&& docker-php-ext-enable xcache \
  1. 下载的包后缀和解压后的后最一定要相同,不然解压的时候会报错
  2. 在wget第一步参数是-O,不是-o,大写的。docker hup上是错的

问题

  • 问题1
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

ERROR: `phpize' failed

解决方案,安装autoconf。centos使用yum,unbantu使用apt-get, alpine是用apk

RUN apk update \
    && apk add autoconf \
    && docker-php-ext-install pcntl \
    && pecl install redis-5.0.0 \
    && docker-php-ext-enable redis
COPY . /var/www/html
COPY ./docker/nginx-php7/sites-enabled /etc/nginx/sites-enabled
  • 问题2
    在./configure 时候报错 no acceptable C compiler found in $PATH
configure: error: in `/var/www/html/redis':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

解决方法在;安装gcc

apk add gcc
  • 问题3
    同样是在./configure时候报错,C compiler cannot create executables
configure: error: in `/var/www/html/redis':
configure: error: C compiler cannot create executables
See `config.log' for more details

由于我使用的是docker的alpine镜像,解决方法请参考,解决办法如下:

apk add gcc g++


以上3个问题是我在安装redis扩展时候遇到的,Dockerfile如下

FROM richarvey/nginx-php-fpm:latest

RUN apk update \
    && apk add autoconf \
    && apk add gcc g++\
    && apk add make \
    && docker-php-ext-install pcntl \
    && wget https://pecl.php.net/get/redis-5.0.1.tgz -O redis.tgz \
    && mkdir -p redis \
    && tar -xf redis.tgz -C redis --strip-components=1 \
    && rm redis.tgz \
    && ( \
        cd redis \
        && phpize \
        && ./configure --with-php-config=/usr/local/bin/php-config \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r redis \
    && docker-php-ext-enable redis
COPY . /var/www/html
COPY ./docker/nginx-php7/sites-enabled /etc/nginx/sites-enabled
发布了80 篇原创文章 · 获赞 96 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/Alen_xiaoxin/article/details/104877279