docker-compose构建nginx+php

安装php:

1:拉取镜像

docker pull php:7.4-fpm

2:创建容器

docker run --name php7.4-fpm -p 9000:9000 -d php:7.4-fpm

3:创建目录存储php配置文件和日志

mkdir -p docker/software/php && cd docker/software/php
mkdir conf log

4:从容器中复制配置文件

1、docker cp php7.4-fpm:/usr/local/etc/php-fpm.d/www.conf /docker/software/php/conf/www.conf
2、docker cp php7.4-fpm:/usr/local/etc/php/php.ini-production /docker/software/php/conf/php.ini

5:删除容器

1、docker stop php7.4-fpm && docker rm php7.4-fpm

安装nginx:同php处理

mkdir -p docker/software/nginx && cd docker/software/nginx
mkdir conf logs conf.d

编辑nginx.conf配置文件

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
    multi_accept on;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  65;
    tcp_nodelay on;

   # server {
   #     listen       80;
   #     server_name  localhost;

   #
   #     location / {
   #         root   /usr/share/nginx/html;
   #         index  index.html index.htm index.php;
   #     }

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

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



    include /etc/nginx/conf.d/*.conf;
}

编辑默认站点:

    server {
        listen       80;
        server_name  test.com;


        location / {
            root   /usr/share/nginx/html/asset/test/public;
            index  index.html index.htm index.php;
        }

        location ~ \.php$ {
                fastcgi_pass   php:9000;
                #fastcgi_pass   php81:9000;
                fastcgi_index  index.php;
                #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  SCRIPT_FILENAME  /www/asset/test/public/$fastcgi_script_name;
                include        fastcgi_params;
        }


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

编辑整个配置文件docker-compose.yaml文件

version: '3'
services:
  nginx:
    image: nginx:latest
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    environment:
      - TZ=Asia/Shanghai
    depends_on:
      - "php"
#    deploy:
#      resources:
#        limits:
#          #cpus: '0.50'
#          memory: 20M
#        reservations:
#          #cpus: '0.25'
#          memory: 10M
    volumes:
      - /Users/zhangsan/docker/software/nginx/logs:/var/log/nginx
      - /Users/zhangsan/docker/software/nginx/conf.d:/etc/nginx/conf.d
      - /Users/zhangsan/docker/software/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - /Users/zhangsan/docker/www:/usr/share/nginx/html
      - /etc/letsencrypt:/etc/letsencrypt
    networks:
      - net-app
  php:
    image: php:7.4-fpm
    container_name: "php-7.4"
    restart: always
    ports:
      - "9000:9000"
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /Users/zhangsan/docker/www:/var/www/html
      - /Users/zhangsan/docker/software/php7.4/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf
      - /Users/zhangsan/docker/software/php7.4/conf/php.ini:/usr/local/etc/php/php.ini
    networks:
      - net-app
  php81:
    image: php:8.1.8-fpm
    container_name: "php-8.1"
    restart: always
    ports:
      - "9001:9000"
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /Users/zhangsan/docker/www:/var/www/html
      - /Users/zhangsan/docker/software/php8.1/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf
      - /Users/zhangsan/docker/software/php8.1/conf/php.ini:/usr/local/etc/php/php.ini
    networks:
      - net-app
networks:
    net-app:

启动

docker-compose up -d
指定配置文件
docker-compose -f docker-php.yaml up -d

你可能感兴趣的:(docker,PHP环境,nginx,docker,php)