docker 搭建多版本php环境

下载相关镜像

php5.4版本:docker pull php:5.4-fmp

php7.4版本:docker pull php:7.4-fpm

nginx版本:docker pull ngxin:latest(最新版本)

-- 运行php
docker run -d -v 本地代码路径:容器内代码路径 -p 本地端口:容器端口 --name 镜像的名字 刚下载的php版本

eg: 

  docker run -d -v /Users/yanjun/code/test/php/php54:/var/www/html/php54 -p 9000:9000 --name dk_php54 php:5.4-fpm // 创建php5.4版本的容器 名字为 dk_php54 

  docker run -d -v /Users/yanjun/code/test/php/php74:/var/www/html/php74 -p 9001:9000 --name dk_php74 php:7.4-fpm // 创建php7.4版本的容器 名字为 dk_php74

 

启动nginx

  docker run -d -p 80:80 --name dk_nginx -v /Users/yanjun/code/test/php/php54:/var/www/html/php54 -v /Users/yanjun/code/test/php/php74:/var/www/html/php74 -v /Users/yanjun/docker/nginx/conf:/etc/nginx/conf.d --link dk_php54:php54 --link dk_php74:php74 nginx:latest

命令解析:

/Users/yanjun/code/test/php/php54:本地php54版本所对应的代码路径

/var/www/html/php54:nginx容器内 代码的位置 会自动同步

/Users/yanjun/code/test/php/php74: 本地php74版本 所对应的 代码路径

/var/www/html/php74:nginx容器内 php74版本 代码的位置 可以自定义路径

/Users/yanjun/docker/nginx/conf:本地配置nginx conf文件的位置 

/etc/nginx/conf.d:nginx 容器内 配置文件的位置(自动同步 /Users/yanjun/docker/nginx/conf 处的文件 所以只需在本地修改conf 文件  重启服务 即可生效)

Tip: 上述配置完成后 下次开启服务前 需确保 dk_php54 和 dk_php74容器 已运行 否则无法启动服务

相关nginx 配置文件如下:

php54.conf

server
{
    listen       80;
    server_name  test.brady.com;
    location  /  {
        root   /var/www/html/php54;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_pass   php54:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/php54/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

 

php74.conf

server
{
    listen       80;
    server_name  test.php74.com;
    location  /  {
        root   /var/www/html/php74;
        index  index.html index.htm index.php;
    }
 
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
    location ~ \.php$ {
        fastcgi_pass   php74:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/php74/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

 

posted @ 2024-01-18 15:02  幸福捕手  阅读(226)  评论(0编辑  收藏  举报