Docker部署Nginx + php-fpm + Mysql + Redis

/home/nginx/conf.d下新建nginx配置default.conf

server {
    listen       80;
    server_name  localhost;

    root /var/www/html;
    index  index.html index.htm index.php;
    charset utf-8;
    
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    location ~ ^(.+\.php)(.*)$ {
        fastcgi_split_path_info       ^(.+\.php)(.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_pass php:9000;
        include        fastcgi_params;
    }
    
    location ~ /\.ht {
       deny  all;
    }
}

编写docker-compose.yaml,镜像会自动下载

version: "3.8"
services:
  nginx:
    image: nginx
    container_name: nginx
    restart: always
    ports:
      - 80:80
    links:
      - php
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /home/nginx/conf.d:/etc/nginx/conf.d
      - /home/nginx/logs:/var/log/nginx
      - /home/www:/var/www/html
    networks:
      - mynet
  php:
    image: php:8.0.3-fpm
    container_name: php
    restart: always
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /home/www:/var/www/html
    networks:
      - mynet
  mysql:
    image: mysql:8
    container_name: mysql
    restart: always
    volumes:
      - /home/mysql:/var/lib/mysql
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=
      - TZ=Asia/Shanghai
    networks:
      - mynet
  redis:
    image: redis
    container_name: redis
    restart: always
    ports:
      - 6379:6379
    volumes:
      - /home/redis:/data
    networks:
      - mynet
networks:
  mynet:
    driver: bridge

启动

docker-compose up -d

在/home/www下新建index.php

访问地址,显示PHP Version则Nginx+PHP配置成功
分别访问Mysql和Redis,均可访问(阿里云需要把对应端口开通安全组)

你可能感兴趣的:(Docker部署Nginx + php-fpm + Mysql + Redis)