Docker WordPress + MySQL + phpmyadmin + nginx

rwqw0loc  于 2023-11-19  发布在  PHP
关注(0)|答案(1)|浏览(187)

我试图为我的本地开发项目准备docker-compose文件,但我坚持为phpmyadmin设置域。
我在windows上添加了主机到主机文件。

127.0.0.1 phpmyadmin.test
127.0.0.1 wordpress1.test
127.0.0.1 worpresss2.test

字符串
我的dev文件夹的结构:

dockerfiles/
   |-wordpress1
     - Dockerfile
   |-wordpress2
     - Dockerfile
.docker/nginx/conf.d/
   - nginx.conf
   - phpmyadmin.conf
sites/
docker-compose.yml


docker-compose.yml:

version: '3.7'

services:
  # Nginx Service
  nginx:
    image: nginx:1.17-alpine
    ports:
      - 80:80
    volumes:
      - ./sites:/var/www/html
      - ./.docker/nginx/conf.d:/etc/nginx/conf.d:ro
      - phpmyadmindata:/usr/src/phpmyadmin:ro
    depends_on:
      - mysql
      - phpmyadmin

  # Database
  mysql:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    volumes:
      - mysql:/var/lib/mysql

  # phpmyadmin
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:5-fpm-alpine
    restart: always
    environment:
      PMA_HOST: mysql
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - phpmyadmindata:/usr/src/phpmyadmin
    depends_on:
      - mysql

  #wordpress1
  wordpress1:
    build: ./.docker/dockerfiles/wordpress1
    restart: always
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    volumes:
      - ./sites/wordpress1:/var/www/html/wordpress1

  #wordpress2
  wordpress2:
    build: ./.docker/dockerfiles/wordpress2
    restart: always
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_NAME: wordpress2
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    volumes:
      - ./sites/wordpress2:/var/www/html/wordpress2

volumes:
  phpmyadmindata:
  mysql:


dockerfile for wordpress1:

FROM  wordpress:5.5.1-php7.4-fpm
WORKDIR /var/www/html/wordpress1


dockerfile for wordpress2:

FROM  wordpress:5.5.1-php7.4-fpm
WORKDIR /var/www/html/wordpress2


nginx.conf

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

    server_name wordpress1.test;
    root /var/www/html/wordpress1;

    index index.php;
  
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass wordpress1:9000;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|woff|woff2)$ {
        expires max;
        log_not_found off;
    }
}

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

    server_name wordpress2.test;
    root /var/www/html/wordpress2;

    index index.php;
  
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass wordpress2:9000;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|woff|woff2)$ {
        expires max;
        log_not_found off;
    }
}


我的phpmyadmin.conf

server {
    listen      80;
    listen      [::]:80;
    server_name phpmyadmin.test;
    root        /usr/src/phpmyadmin;
    index       index.php;

    location ~* \.php$ {
        fastcgi_pass   phpmyadmin:9000;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME     $fastcgi_script_name;
    }
}


所以,域:wordpress1.test和wordpress2.test工作正常,但当我去phpmyadmin。测试我得到403错误,我不知道,为什么。你能帮助我吗?
顺便说一句.为什么我必须使用构建Dockerfiles而不是写:图像:WordPress:5.5.1-fpm

rsaldnfx

rsaldnfx1#

我认为phpmyadmin,wordpress1和wordpress2都使用相同的默认端口80。
尝试修改compose文件,为这三个容器分配唯一的端口。例如,

ports:     #for wordpress1 container
  - 81:80  
ports:     #for wordpress2 container
  - 82:80  
ports:     #for phpmyadmin container
  - 83:80

字符串
最后编辑nginx配置文件以反映这些端口[81,82和82]。

相关问题