phplaraveldockernginx

500 Internal Server Error for PHP Requests in Laravel Docker Setup


I am encountering a 500 Internal Server Error when trying to access PHP files in my Laravel project running in Docker, specifically occurring on GET /index.php. I am using Nginx and PHP-FPM with Docker.

Nginx Configuration (site.conf):

upstream backend {
    server web:9000;  # PHP-FPM service
}

server {
    listen 80;
    index index.php index.html;
    server_name domain_name;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;

    client_max_body_size 100M;
    fastcgi_read_timeout 1800;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_pass backend;  # Use the upstream backend
        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 Configuration (docker-compose.yml):

version: '3.8'

services:
  db:
    image: mysql:latest
    ports:
      - 3306:3306
    env_file:
      - ./.env
    volumes:
      - db-data:/var/lib/mysql

  nginx:
    image: nginx:stable-alpine
    ports:
      - 80:80
    volumes:
      - ./docker/nginx/site.conf:/etc/nginx/conf.d/default.conf
      - ./docker/logs/nginx:/var/log/nginx
    depends_on:
      - web

  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: ${APP_NAME}_php
    volumes:
      - ./:/var/www/html
    env_file:
      - ./.env
    ports:
      - 9001:9000
    depends_on:
      - db

volumes:
  db-data:

Dockerfile

FROM php:8.3-fpm

# Install system dependencies and PHP extensions
RUN apt-get update && apt-get install -y \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libonig-dev \
    libzip-dev \
    unzip \
    git \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd pdo pdo_mysql mbstring zip

# Set working directory
WORKDIR /var/www/html

# Copy application code
COPY . .

# Install Composer
COPY --from=composer:2.7 /usr/bin/composer /usr/bin/composer

# Install PHP dependencies
RUN composer install

When accessing PHP files, I receive a 500 Internal Server Error. HTML files are served correctly, but PHP files are not processed. These are the logs from the storage/logs directory:

tail -n 10 laravel.log
#25 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(490): Illuminate\\Foundation\\Exceptions\\Handler->renderHttpException(Object(Symfony\\Component\\HttpKernel\\Exception\\HttpException))
#26 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(356): Illuminate\\Foundation\\Exceptions\\Handler->prepareResponse(Object(Illuminate\\Http\\Request), Object(Symfony\\Component\\HttpKernel\\Exception\\HttpException))
#27 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(196): Illuminate\\Foundation\\Exceptions\\Handler->render(Object(Illuminate\\Http\\Request), Object(ArgumentCountError))
#28 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(173): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->renderHttpResponse(Object(ArgumentCountError))
#29 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleException(Object(ArgumentCountError))
#30 {main}
  thrown at /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Manager.php:94)
[stacktrace]
#0 {main}
"} 

Steps Taken:

Verified that the index.php file is present in the /var/www/html/public directory. Ensured that the Nginx and PHP-FPM containers are running. Checked file permissions and ownership. Questions:

Are there any misconfigurations in my Nginx setup or Docker setup that could be causing this issue? How can I further troubleshoot this 500 Internal Server Error? Thank you for your assistance!


Solution

  • Resolved the issue by running composer install to ensure all dependencies were correctly installed.