skip to Main Content

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…

2

Answers


  1. Chosen as BEST ANSWER

    The answer from @Isaac Souza it's correct but i want to add some details, my problem was in fact that in my server the project was in var/www/html/project/project-back/ but inside the container the project was in var/www/html and this caused the 404 file not found.

    To fix it i had to change the apache config for the subdomain and add this :

        # Remove the proxy, you need to use SetHandler "proxy:fcgi://127.0.0.1:9000"
        # ProxyPreserveHost On
        # ProxyPass / fcgi://localhost:9000/
        # ProxyPassReverse / fcgi://localhost:9000/
    
        # This need to be the symfony public path inside the container
        Define container_root "/var/www/html/public"
        <FilesMatch .php$>
            Require all granted
            SetHandler "proxy:fcgi://127.0.0.1:9000"
            ProxyFCGISetEnvIf "true" DOCUMENT_ROOT   "${container_root}"
            ProxyFCGISetEnvIf "true" SCRIPT_FILENAME "%{HANDLER}${container_root}%{reqenv:SCRIPT_NAME}"
        </FilesMatch>
        UnDefine container_root
    

    You can't use the ProxyPass in this case you need to use SetHandler "proxy:fcgi://127.0.0.1:9000" and define the DOCUMENT_ROOT with the path inside the container to the symfony public folder.

    I've found the solution here : https://serverfault.com/questions/1047449/apache-php-fpm-setup-results-in-file-not-found

    Sorry i didn't saw it before, so i guess the question can be marked as duplicate.


  2. 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!

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