Score:1

I get 404 error when trying interper PHP with Nginx in Docker

pt flag

Sorry if this question is answered. It my first time using Docker in web dev. Im setting up Nginx - PHP - MariaDB for my projects . And get stuck when my Nginx show 404 error when everytime i access to php file

docker-compose.yml

version: '3'
# Nginx
services: 
  web:
    image: nginx:latest
    restart: "always"
    volumes:  
      - ./web.conf:/etc/nginx/conf.d/default.conf
      # Index
      - /home/hoangtho/Projects:/data/www/projects
    ports: 
      - "80:80"
    
    depends_on:
      - mariadb
    links: 
      - mariadb
      - php
# Mariadb
  mariadb:
  # Install latest version
    image: "mariadb:latest"
    restart: "always"
    
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${MYSQL_DATABASE}"
      MYSQL_USER: "${MYSQL_USER}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD}"

    ports: # ⇐ here
      - "3306:3306" 
# PHP services
  php:
    image: php:7.3-fpm-alpine
    restart: "always"
    build:
      context: './php/'
    volumes:
      - /home/hoangtho/Projects:/data/www/projects
    

web.conf

server {
    listen              80;
    server_name         localhost;
    index index.php index.htm index.html;
    #  set root 
    location / {
        root /data/www/projects;
        index index.html index.php;
    }
     location /test {
        alias /data/www/projects;
        autoindex on;
    }
    # PHP
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
  }

Dockerfile

FROM php:7.3-fpm-alpine
Install PDO , PDO_MYSQLI
RUN docker-php-ext-install  pdo pdo_mysql 

You can see my setup is very simple. But i don't know why i keep getting 404 when access to PHP file My root dictory in /home/hoangtho/Projects (which contains all my projects - works)

Score:0
nc flag

I think it is depreciated to use links and depends_on. For the containers to communicate, they have to be in the same network.

For example, first, create a network named "projects":

docker network create projects

And I have modified some of your compose file:

version: '3'
# Nginx
services: 
  web:
    image: nginx:latest
    restart: "always"
    volumes:  
      - ./web.conf:/etc/nginx/conf.d/default.conf
      # Index
      - /home/hoangtho/Projects:/var/www/html
    networks:
      - projects

# Mariadb
  mariadb:
  # Install latest version
    image: "mariadb:latest"
    restart: "always"
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${MYSQL_DATABASE}"
      MYSQL_USER: "${MYSQL_USER}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
    networks:
      - projects

# PHP services
  php:
    image: php:7.3-fpm-alpine
    restart: "always"
    build:
      context: './php/'
    volumes:
      - /home/hoangtho/Projects:/var/www/html
    networks:
      - projects

networks:
  projects:
    external: true

There is no need to specify the default ports (80 for NGINX and 3306 for MariaDB).

links and depends_on can be replaced with "networks". You could also use two network, one for backend (MariaDB) and one for frontend (NGINX, not sure if PHP need to be in frontend, or if it works only in backend). If you use two networks, be sure to put NGINX (and maybe PHP) in the two networks.

The volume you specify does not exist inside the container (/data/www/projects), I have replaced with the working directory inside these containers (/var/www/html).

In your web.conf file,

server {
    listen              80;
    server_name         localhost;
    index index.php index.htm index.html;
    #  set root 
    location / {
        root /var/www/html;
        index index.html index.php;
    }
     location /test {
        alias /var/www/html;
        autoindex on;
    }
    # PHP
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
  }
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.