Ad

Docker Nginx Php-fpm Not Serving Php

- 1 answer

I'm trying to setup nginx and php-fpm images to work together for a Laravel application. This is my set up:

docker-compose.yml

version: "3.8"

services:
    server:
        container_name: nginx
        build:
            context: .
            dockerfile: docker/nginx/Dockerfile
        volumes:
            - ./src/public:/var/www/html/public
        ports:
            - "8888:80"
        depends_on:
            - php
        networks:
            - laravel_net
    php:
        container_name: php
        build:
            context: .
            dockerfile: docker/php/Dockerfile
        ports:
            - "9000:9000"
        volumes:
            - ./src:/var/www/html
        networks:
            - laravel_net
networks:
    laravel_net:
        driver: 'bridge'

Nginx

FROM nginx:stable-alpine

# puts app.conf into the container as /etc/nginx/nginx.conf 
# has by default this: include /etc/nginx/conf.d/*.conf;
COPY docker/nginx/conf.d/*.conf /etc/nginx/conf.d/

WORKDIR /var/www/html
COPY ./src/public /var/www/html/public

docker/nginx/conf.d/app.conf

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

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

    location ~ \.php {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_pass php:9000;
    }
}

PHP

FROM php:8.1-fpm-alpine

WORKDIR /var/www/html
COPY ./src /var/www/html

EXPOSE 9000

Inside src folder is where the laravel app should live so for now I'm just outputting phpinfo() on the public dir just to check that everything is working as expected src/public/index.php

<?php

phpinfo();

If I run docker-compose up --build and docker ps I can see the containers running

% docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                    NAMES
abc123   laravel_server   "/docker-entrypoint.…"   8 seconds ago   Up 6 seconds   0.0.0.0:8888->80/tcp     nginx
abc456   laravel_php      "docker-php-entrypoi…"   8 seconds ago   Up 7 seconds   0.0.0.0:9000->9000/tcp   php

also on both docker-compose exec server /bin/sh and docker-compose exec php /bin/shI can see the index.php file inside the container

docker-compose exec server sh
/var/www/html # cat public/index.php 
<?php

phpinfo();
/var/www/html # ls -la public 
total 12
drwxr-xr-x    2 root     root          4096 Feb 12 13:41 .
drwxr-xr-x    1 root     root          4096 Feb 12 13:41 ..
-rw-r--r--    1 root     root            18 Feb 12 12:51 index.php

docker-compose exec php sh
/var/www/html # ls -lah public/
total 4K     
drwxr-xr-x    3 root     root          96 Feb 12 13:42 .
drwxr-xr-x    3 root     root          96 Feb 10 21:28 ..
-rw-r--r--    1 root     root          18 Feb 12 13:42 index.php
/var/www/html # cat public/index.php 
<?php

phpinfo();

However when I try to access http://localhost:8888 from the browser I get the default Welcome to nginx! page. Any ideas what I'm doing wrong?

Ad

Answer

I think the problem is nginx config. You are copying app.conf to /etc/nginx/conf.d/ but there already exists default config /etc/nginx/conf.d/default.conf. Because you are seeing the default nginx page I would assume the default one is used.

Try to rename app.conf to default.conf or change copy command to

COPY docker/nginx/conf.d/app.conf /etc/nginx/conf.d/default.conf
Ad
source: stackoverflow.com
Ad