docker构造php执行环境

docker构造php执行环境

该文章只对于有docker基础经验的人且是小白... 如果没有docker经验或大神级别的 那么请跳过

文章树状结构解析

docker_nginx_php
│  docker-compose.yml         --- compose配置,创建docker容器
│  Dockerfile                 --- 构建docker镜像配置
│
└─nginx                       --- nginx配置文件夹
    ├─conf                    --- 配置文件存放位置
    │  └─conf.d               --- 配置文件存放位置
    │       default.conf      --- 初始化配置
    │
    ├─log                     --- log文件存放位置
    └─www                     --- 项目文件存放位置

docker-compose文件编写

version: "3"
services:
  # redis镜像
  redis:
    image: redis:latest
    container_name: redis_compose
    ports:
    - 6379:6379
  # php运行环境
  php:
    image: php7.3
    container_name: php_compose
    privileged: true
    ports:
    - 9000:9000
    # 本地文件映射到docker镜像里面
    volumes:
    - /f/allProject:/var/www/html
  # nginx镜像
  nginx:
    image: nginx:latest
    container_name: nginx_compose
    # 映射端口 local访问9191映射80端口
    ports:
    - 9191:80
    # nginx配置文件映射到docker里面
    volumes:
    - /f/docker-nginx-php/nginx/conf/conf.d:/etc/nginx/conf.d
    - /f/allProject:/var/www/html
    links:
    - php
    - redis

Dockerfile文件编写

# Version 1.0
FROM php:7.3-fpm
# 维护者信息
MAINTAINER qizaictt@qq.com
# 对外展示端口
EXPOSE 9000
# 更新软件源
RUN apt-get update
# 安装zip扩展所需的依赖扩展
RUN apt-get install -y zlib1g-dev
RUN apt-get install -y libzip-dev
# 安装GD扩展
RUN apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev
RUN docker-php-source extract
RUN cd /usr/src/php/ext/gd
RUN 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/include/freetype2
RUN docker-php-ext-install gd
# 安装Mysql
RUN docker-php-ext-install pdo_mysql
# 安装Redis
RUN pecl install redis
RUN docker-php-ext-enable redis
# 安装Openssl
RUN apt-get install openssl

default.conf文件编写

server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    location / {
       root   #docker里面的项目文件夹 例如: /var/www/html/.... ;
       index  index.php index.html index.htm;

       # 如果没有以下4行,laravel将只能访问首页,其他页面都是404
       try_files $uri $uri/ /index.php?$query_string;
       if (!-e $request_filename){  
           rewrite ^/(.*) /index.php last;  
       }
    }

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

    location ~ \.php$ {
        root           #docker里面的项目文件夹 例如: /var/www/html/.... ;
        index          index.php index.html;
        # 坑在这里,需将原有的127.0.0.1:9000 替换成 php_compose:9000 因为container_name
        fastcgi_pass   php_compose:9000;      
        fastcgi_index  index.php;
        # 下面这行也改了  中间的$document_root
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

###################有新增加的项目 就增加server{}###################

docker运行 & 重启 & 进入镜像查看

  1. 在与docker-compose.yml文件平级目录下 执行 运行
    -d 代表后台运行
    docker-compose up -d
    
  2. docker-compose重启
    down: 命令将停止运行的容器,并且会删除已停止的容器以及已创建的所有网络。
    stop: 命令将停止运行的容器,但不会删除它们。
    docker-compose stop
    或
    docker-compose down  
    
  3. 查看运行/已停止的 容器
    docker ps        查看正在运行的容器
    
    docker ps -a     查看正在运行和已停止的容器
    
  4. docker-compose运行的项目 进入容器查看(文件夹是否映射等…)
    docker exec -it 镜像id/name /bin/bash
    

注意:localhost是nginx配置里面的server_name,且9191端口是compose文件里面的映射关系
那么此时在浏览器上输入localhost:9191就能打开看到你的php项目了

修改windows下的hosts文件

hosts文件路径: C:\Windows\System32\drivers\etc

注意: 修改hosts文件需要权限, 可以复制到桌面修改完成后覆盖原有的即可

#增加一行映射127.0.0.1
127.0.0.1    对应的你在nginx配置里面的server_name
127.0.0.1    ...可添加多个
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值