Back to top

纸喵docker-php-fpm镜像

纸喵php镜像,基于官方镜像,增加了一些常用的扩展

启动方式

docker run -it --rm --name zhimiao-php -p 9073:9000 -v /usr/share/nginx/:/var/www/html/ -d zhimiao/php

nginx配置方案如下,本文宿主机采用Centos7,所有软件皆系yum安装

/etc/nginx/docker.server

# docker快速执行站,变量app_path
location ~ \.php(.*)$ {
    root /var/www/html/$app_path;
    # 此处如果当前机器的很多网站所用版本不同,可以将此处端口号改为变量,在下方虚拟主机中自定义,同时运行多个php-fpm docker镜像实现多版本同时存在的特性
    fastcgi_pass 127.0.0.1:9073; 
    fastcgi_index index.php;
    fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    include  fastcgi_params;
}

/etc/nginx/conf.d/vhost1.conf

server {
  listen 80;
  server_name www.zhimiao.org;
  set $app_path "zhimiao_www/site";
  root /usr/share/nginx/$app_path;
  location / {
    index index.html index.htm index.php;
    autoindex off;
  }
  include docker.server;
}

docker辅助命令

# 查看正在运行的docker容器
docker ps

# 停止指定的docker容器
docker stop CONTAINERID

# 查看正在运行的docker容器ip
docker inspect --format='{{.NetworkSettings.IPAddress}}' zhimiao-php

如果镜像不能满足需求,可以自己从新编译,正常情况下用上面的就够了

Dockerfile 文件内容如下

FROM php:7.3-fpm

# 改系统时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone

# 升级系统依赖
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
RUN apt-get install -y libfreetype6-dev libgmp-dev libjpeg62-turbo-dev libpng-dev libxml2-dev libcurl3-dev libbz2-dev

# 安装常用扩展
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) \
	iconv \
	gd \
	pdo \
	pdo_mysql \
	xml \
	mbstring \
	bcmath \
	curl \
	session \
	bz2 \
	calendar \
	fileinfo \
	gettext \
	gmp \
	json \
	opcache \
	pcntl \
	sockets
# 安装redis扩展
ENV PHPREDIS_VERSION 4.2.0
RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz \
    && tar xfz /tmp/redis.tar.gz \
    && rm -r /tmp/redis.tar.gz \
    && mkdir -p /usr/src/php/ext \
    && mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
    && docker-php-ext-install redis \
    && rm -rf /usr/src/php #如果这段不加构建的镜像将大100M

# 修改PHP的时区
RUN printf '[PHP]\ndate.timezone = "Asia/Shanghai"\n' > /usr/local/etc/php/conf.d/tzone.ini

# 清理产生数据
RUN apt-get purge -y --auto-remove \
        -o APT::AutoRemove::RecommendsImportant=false \
        -o APT::AutoRemove::SuggestsImportant=false \
        $buildDeps \
    && rm -rf /var/cache/apt/* \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get autoremove \
    && apt-get clean

编译Dockerfile

docker build -t zhimiao/php .