window10中利用docker安装php,mysql,nginx环境及compose利用yml管理应用

1.mysql8.0安装

docker pull mysq

docker run -p 3306:3306 --name mysql8.0 -d -v D:\docker\mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql

2,php8.0安装

docker pull php:8.0-rc-fpm

#D:\code为本地php文件保存路径,自行修改
docker run --name  php8.0 -p 9000:9000 -v D:\code:/var/www   -d php:8.0-rc-fpm

3.nginx安装

docker pull nginx

为了方便本地配置可使用的环境,先建一个测试容器获取各类配置

docker run --name nginx-test -p 80:80 -d nginx
#host配置,D:\docker\nginx\conf.d/自行修改为自己本地路径
docker cp nginx-test:/etc/nginx/conf.d/default.conf D:\docker\nginx\conf.d/
#nginx配置文件
docker cp nginx-test:/etc/nginx/nginx.conf D:\docker\nginx\conf
#删除测试容器
docker stop nginx-test
docker rm nginx-test

 创建新的容器

docker run --name nginx -p 80:80 -v D:\docker\nginx\conf:/etc/nginx/conf -v D:\docker\nginx\conf.d:/etc/nginx/conf.d -v D:\docker\nginx\logs:/var/log/nginx -v D:/code:/var/www -d --link php8.0:php nginx

修改本地host配置:D:\docker\nginx\conf.d\default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www; #修改此处
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #此处修改
    location ~ \.php$ {
        root           /var/www;
        fastcgi_pass   php8.0:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

 安装php-mysql扩展

docker exec -it php8.0 /bin/bash

docker-php-ext-install pdo_mysql mysqli

重启下php8.0容器即可!

4.docker-compose管理应用

在对应的项目目录下新建docker-compose.yml文件

version: '3'
services:
  nginx:
    image: nginx:latest
    container_name: local_nginx  #注意留个空格
    ports:
      - "80:80"
    volumes:
      - d:/code:/var/www
      - d:/docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf  ###这里为防止host多了之后出现打架情况,最好本地使用一个单独的目录分别将不同的host映射到docker,例如d:/docker/nginx/host/test.conf:/etc/nginx/conf.d/test.conf
      - d:/docker/nginx/conf:/etc/nginx/conf
      - d:/docker/nginx/logs:/var/log/nginx
  php:
    image: php:8.0-rc-fpm
    container_name: local_php8.0
    ports:
      - "9000:9000"
    volumes:
      - d:/code:/var/www

 修改d:/docker/nginx/conf.d/default.conf(根据自己的目录自行修改)

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www;
        fastcgi_pass   local_php8.0:9000;  ######此处换成yml文件中php的容器名称
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

docker-compose up -d 启动即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yishiwucheng

码字不易,多多益善

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值