phpdockerapachedocker-composesymfony6

Deploing symfony with php-fpm and docker result in a 404 File not found


I've been trying to deploy my application since yesterday, but without success. I'm working with a personal server, so I have access to everything, but my knowledge of Linux is not that great.

I'm trying to map my port 8002 so that it gets forwarded to port 9000 of my container. I think this part works because when I look at the docker logs container, I see the requests, but the problem is that all requests return a 404 error, no matter which endpoint I call.

here's the subdomain.conf:

<VirtualHost *:80>
    ServerName mysubdomain.domain.com
    ServerAlias www.mysubdomain.domain.com

    # Redirect to HTTPS
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =mysubdomain.domain.com [OR]
    RewriteCond %{SERVER_NAME} =www.mysubdomain.domain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>
    ServerName mysubdomain.domain.com
    ServerAlias www.mysubdomain.domain.com

    # Proxy settings
    ProxyPreserveHost On
    ProxyPass / fcgi://localhost:8002/
    ProxyPassReverse / fcgi://localhost:8002/

    DocumentRoot /var/www/html/project/project-back/public
    DirectoryIndex /index.php

    # Allow .htaccess overrides in the public directory
    <Directory /var/www/html/project/project-back/public>
        AllowOverride All
        Options FollowSymLinks
        Require all granted
    </Directory>


    ErrorLog ${APACHE_LOG_DIR}/mysubdomain.domain.com-error.log
    CustomLog ${APACHE_LOG_DIR}/mysubdomain.domain.com-access.log combined

    # Enable SSL
    SSLEngine on
    SSLCertificateFile /some/path
    SSLCertificateKeyFile /some/path
    Include /some/path

    # Optionally enable HSTS to force browsers to use HTTPS
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</VirtualHost>

Dockerfile:

# Use the official PHP 8.1 FPM image as the base
FROM php:8.1-fpm

# Set the working directory inside the container
WORKDIR /var/www/html

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    unzip \
    nano \
    net-tools \
    lsof \
    libpq-dev \
    libzip-dev \
    && docker-php-ext-install pdo pdo_mysql zip

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Copy project files into the container
COPY . .

# Set permissions for cache and log directories
RUN chmod -R 777 /var/www/html/var/cache /var/www/html/var/log

# Set permissions for cache and log directories
RUN chmod -R 777 /var/www/html/public

# Install Symfony dependencies
RUN composer install --no-dev --optimize-autoloader

# Expose PHP-FPM port
EXPOSE 9000

# Start PHP-FPM
CMD ["php-fpm", "-F"]

docker-compose.yaml:

services:
  php:
    build:
      context: .
      dockerfile: ./Dockerfile
    container_name: mycontainer
    volumes:
      - .:/var/www/html
      - ./public:/var/www/html/public
    working_dir: /var/www/html
    ports:
      - "8002:9000"
    networks:
      - database


networks:
  database:
    external: true

The database network it's my mysql and phpmyadmin network, and that works fine i've already used it in other projects.

The only thing in the mysubdomain.domain.com-error.log it's this :

[proxy:warn] AH01144: No protocol handler was valid for the URL / (scheme 'fcgi'). If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.

But i've fixed it using sudo a2enmod proxy_fcgi and since then when i go to my subdomain.domain.com i get a 404 File not found.

Here's the output of some commands (from my server)

$ curl https://mysubdomain.domain.com/
File not found.
$ curl -I https://mysubdomain.domain.com/
HTTP/1.1 404 Not Found
Date: Sat, 30 Nov 2024 14:48:08 GMT
Server: Apache/2.4.62 (Debian)
X-Powered-By: PHP/8.1.31
Content-Type: text/html; charset=UTF-8
$ curl -I http://localhost:8002
curl: (52) Empty reply from server
$ sudo docker logs mycontainer
NOTICE: fpm is running, pid 1
NOTICE: ready to handle connections
"GET /" 404
"GET /" 404
"GET /" 404
"GET /" 404

From inside the container:

# curl -I http://localhost:9000
curl: (56) Recv failure: Connection reset by peer
netstat -tlnp | grep 9000
tcp6       0      0 :::9000                 :::*                    LISTEN      1/php-fpm

I have no idea how to fix the File not found. problem now ...

Edit: Just to be clear, what I get is not a '404 page not found,' but a '404 file not found.' it's the first time that i see this error here's what it looks like This is the only thing I get on all my pages. No error logs just this...


Solution

  • I had seen a similar issue acouple years ago when I was trying to setup my own infrastructure from scratch (load balancer using Nginx, two application servers running PHP-FPM, one database server running MySQL, and one redis server).

    Now I don't quite remember if I was seeing page not found or file not found, but it was one of the two.

    In my case, I found out that I needed to have my app deployed in both the application server AND the load balancer servers, in the EXACT same folder path, they needed to match exactly.

    So if in my load balancer server, my main php file was located at /var/www/html/public/index.php and in my app server the same was located at /var/www/app/public/index.php it would not work, and obviously, if the files were not present at all in my load balancer server, it would also not work.

    But maybe that is an Nginx thing (?), hope it helps!