创建Nginx和PHP容器

# https://blog.csdn.net/qq_40880022/article/details/136591098
version: '3.8'
services:
  php:
    container_name: php56
    image: php:5.6.40-fpm
    restart: always
    privileged: true
    ports:
      - "9500:9000"
    #volumes:
      #- "./www:/var/www" # php代码目录
      #- "./php/conf.d:/usr/local/etc/php/conf.d" 
      #- "./php/php-fpm.d:/usr/local/etc/php-fpm.d"
  nginx:
    container_name: nginx
    image: nginx:1.25.4
    privileged: true
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "8088:80"
    depends_on:
      - "php"
    #volumes:
      #- "./nginx/nginx.conf:/etc/nginx/nginx.conf" # 主配置文件-不是目录
      #- "./nginx/conf.d:/etc/nginx/conf.d"
      #- "./www:/var/www" # php代码目录
      #- "./nginx/log:/var/log/nginx"
      

将所有挂载的目录、文件先用docker cp 命令复制到宿主机。

# 复制docker容器内的文件到宿主机
docker cp 容器ID:容器内文件路径 宿主机文件路径

# 复制文件到docker容器
docker cp 宿主机文件路径 容器ID:容器内文件路径

例如:

# 复制docker容器内的文件到宿主机
docker cp php56:/usr/src/php/ext/redis ./redis-4.3.0

# 复制文件到docker容器
docker cp ./redis-4.3.0 php56:/usr/src/php/ext/redis

复制完成后将容器删除,再重新创建新的容器。

# 删除容器
docker-compose down

修改docker-compose.yml

# https://blog.csdn.net/qq_40880022/article/details/136591098
version: '3.8'
services:
  php:
    container_name: php56
    image: php:5.6.40-fpm
    restart: always
    privileged: true
    ports:
      - "9500:9000"
    volumes:
      - "./www:/var/www" # php代码目录
      - "./php/conf.d:/usr/local/etc/php/conf.d" 
      - "./php/php-fpm.d:/usr/local/etc/php-fpm.d"
  nginx:
    container_name: nginx
    image: nginx:1.25.4
    privileged: true
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "8088:80"
    depends_on:
      - "php"
    volumes:
      - "./nginx/nginx.conf:/etc/nginx/nginx.conf" # 主配置文件-不是目录
      - "./nginx/conf.d:/etc/nginx/conf.d"
      - "./www:/var/www" # php代码目录
      - "./nginx/log:/var/log/nginx"
      

重新创建并启动容器

docker-compose up -d

修改nginx.conf

在nginx.conf的http域内加入以下内容:

    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;
        root /var/www/;
    
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            index  index.php index.html index.htm;
            autoindex off;
        }
    
        location ~ \.php(.*)$ {
            fastcgi_pass   php:9000;
            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;
        }
    }

重启Nginx容器。

修改php.ini

./php/conf.d/路径下的php.ini-production文件复制并重命名为php.ini

安装Redis扩展

在宿主机上下载redis扩展压缩包,php5.6只能安装redis-4.3.0.tgz,再高版本安装不上。

# 下载redis扩展压缩包
wget https://pecl.php.net/get/redis-4.3.0.tgz
# 解压
tar -zxvf redis-5.3.4.tgz 

将解压后的文件复制到PHP容器内部。

docker cp ./redis-4.3.0 php56:/usr/src/php/ext/redis

进入容器内部安装Redis

# 进入容器
docker exec -it php56 /bin/bash

# 容器内安装Redis
/usr/local/bin/docker-php-ext-install redis

docker-php-ext-install会输出log。

(cd .libs && rm -f redis.la && ln -s ../redis.la redis.la)
/bin/bash /usr/src/php/ext/redis/libtool --mode=install cp ./redis.la /usr/src/php/ext/redis/modules
cp ./.libs/redis.so /usr/src/php/ext/redis/modules/redis.so
cp ./.libs/redis.lai /usr/src/php/ext/redis/modules/redis.la
PATH="$PATH:/sbin" ldconfig -n /usr/src/php/ext/redis/modules

启用Redis。

/usr/local/bin/docker-php-ext-enable redis

安装 pdo_mysql

/usr/local/bin/docker-php-ext-install pdo_mysql

安装 GD

修改国内源

# 备份原来的
cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 写入阿里云源
echo  "deb http://mirrors.aliyun.com/debian bullseye main" >/etc/apt/sources.list
echo  "deb http://mirrors.aliyun.com/debian-security bullseye-security main" >>/etc/apt/sources.list
echo  "deb http://mirrors.aliyun.com/debian bullseye-updates main" >>/etc/apt/sources.list

apt-get update

apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev --allow-unauthenticated

下载freetype

wget https://sourceforge.net/projects/freetype/files/freetype2/2.8.1/freetype-2.8.1.tar.gz
tar -xzvf freetype-2.8.1.tar.gz

docker cp freetype-2.8.1 php56:/root
docker exec -it php56 /bin/bash

cd /root/freetype-2.8.1
./configure --prefix=/usr/local/freetype
make && make install

配置并安装GD

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

docker-php-ext-install gd

docker-php-ext-enable gd 

最后,重启docker容器。

参考链接:
https://www.kancloud.cn/m1980251203/php_docker/3162789
https://www.wpcode.cn/114.html
https://bugs.php.net/bug.php?id=76324

最后修改:2024 年 04 月 07 日,缓存于2025-01-16 16:11:31
看都看了,点个赞吧!