skip to Main Content

I have seen similar questions and solutions here before but they were not a solution. I want to run a wordpress site on local using docker but it doesn’t work. I get “localhost did not send any data ERR_EMPTY_RESPONSE”. I edited the codes according to the comments. Now im getting 502 Bad Gateway error. I would appreciate if you can help me how to solve it. You can find my two related code files at the end of this post.

When I type docker-compose up -d I get the output:

docker-compose up -d

When I type docker-compose ps I get the output:

ps

Docker:

dockerss

nginx.conf

    server {
    listen 80;
    server_name localhost;

    root /var/www/html;
    index index.php;


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

    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

docker-compose.yml

 services:
  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_DATABASE: wordpress_db
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: wp_password
      MYSQL_ROOT_PASSWORD: '1'
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - "3306:3306"

  wordpress:
    image: wordpress:latest
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress_db
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: wp_password
    volumes:
      - wp_data:/var/www/html
    depends_on:
      - db
    ports:
      - "9000:9000"

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - wp_data:/var/www/html
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - wordpress

volumes:
  db_data:
  wp_data:

2

Answers


  1. This setup will use Docker Compose to orchestrate the containers. Here’s a comprehensive guide to set up your WordPress environment:

    Docker Compose Setup for WordPress with Nginx and MySQL

    docker-compose.yml

    services:
      wordpress:
        image: wordpress:latest
        restart: always
        environment:
          WORDPRESS_DB_HOST: db
          WORDPRESS_DB_USER: wordpress_user
          WORDPRESS_DB_PASSWORD: wordpress_pass
          WORDPRESS_DB_NAME: wordpress_db
        volumes:
          - wordpress_data:/var/www/html
        depends_on:
          - db
    
      db:
        image: mysql:latest
        restart: always
        environment:
          MYSQL_DATABASE: wordpress_db
          MYSQL_USER: wordpress_user
          MYSQL_PASSWORD: wordpress_pass
          MYSQL_RANDOM_ROOT_PASSWORD: '1'
        volumes:
          - db_data:/var/lib/mysql
    
      nginx:
        image: nginx:latest
        ports:
          - "80:80"
          - "443:443"
        volumes:
          - ./nginx.conf:/etc/nginx/conf.d/default.conf
          - wordpress_data:/var/www/html
        depends_on:
          - wordpress
    
    volumes:
      wordpress_data:
      db_data:
    

    nginx.conf

    server {
        listen 80;
        server_name localhost;
    
        root /var/www/html;
        index index.php;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ .php$ {
            fastcgi_split_path_info ^(.+.php)(/.+)$;
            fastcgi_pass wordpress:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
    
    docker-compose up -d
    
    http://localhost
    
    Login or Signup to reply.
  2. At least:

    • as of 2024.11.14 wordpress:latest tag maps to 6.6.2-apache which is definitely not the image you want to use with nginx. I think you need something like wordpress:6-fpm (WordPress 6.6.2 and PHP-FPM 8.2 as of 2024.11.14);

    • expose port 3306 from MySQL container and port 9000 from WordPress container (see What is the difference between docker-compose ports vs expose).

    • map your wordpress_data volume as /var/www/html for wordpress container (as it is right now) and as /usr/share/nginx/html for nginx container.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search