docker-library / php

Docker Official Image packaging for PHP
https://php.net
MIT License
3.79k stars 2k forks source link

.htaccess error #1373

Closed cdchn1989 closed 1 year ago

cdchn1989 commented 1 year ago

Hello guys, firt off all is a pleasure for my to put an issue in this repository, So lets go to the issue, recently I'm trying to migrate my old PHP apps deployed on Cpanel the platform which has been choosen to do this migration was Railway app, actually railway app is pretty cool and easy to use. So What is the problem? , In some services in that case I'm using PHP Slim Micro framework, So I've updated my framework fix some stuff (line codes) and test in my local enviroment, of course I tested my API with (PHP 8.2) php -S locahost:8001, in that case works fine, To work with railway app I have to options: 1. Deploy native php (Nix container) or 2 deploy docker, So I've decided to use docker, but I have an issue PHP Slim needs a .htcaccess file to work fine over apache, for that reason to run my container I use php:8.2-apache, So whats is my issue when I put the .htaccess file all my API routes doesn't work.

My docker file is this :

FROM php:8.2-apache

COPY /src /var/www/html

EXPOSE 80

My .htaccess have this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]`

My index.php have this:

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Routing\RouteCollectorProxy;

require __DIR__ . '/vendor/autoload.php';
$app = AppFactory::create();
$app->setBasePath("");

$app->get('/test', function (Request $request, Response $response) {
  $response->getBody()->write(json_encode(array('getted succesfully')));
  return $response
    ->withHeader('Content-Type', 'application/json')
    ->withStatus(200);
});
$app->post('/test', function (Request $request, Response $response) {
  $body = $request->getParsedBody();
  $response->getBody()->write(json_encode($body));
  return $response
    ->withHeader('Content-Type', 'application/json')
    ->withStatus(200);
});

$app->run();

My scafolding is this:

/src
----/vendor
----composer.json
----composer.lock
----.htaccess
----index.php

Yes the index.php file and .htaccess must to be in the same folder.

So when I run my container with the htaccess file I allways get the error 404 in any single route.

I have attached two images of localhost:8000 (docker instance running) and localhost:8001 (php "native" run), My cuestion is There is any rigth way to put an own .htaccess file into this container.

port 8000 docker instance runnign docker-php

port 8001 native instance running native-php

sbuerk commented 1 year ago

Should be the content of the .htaccess file not be like this (!!! linebreaks !!!:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^ index.php [QSA,L]

Not sure about that file, never used "that" construct - personally I use more something like following (with non-docker instances):

RewriteEngine On

# Store the current location in an environment variable CWD to use
# mod_rewrite in .htaccess files without knowing the RewriteBase
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=CWD:%2]

# ... additional rules, like block source controle folders etc.
#     for security reason

# Final rewrite to the /index.php script (with proper subfolder handling)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ %{ENV:CWD}index.php [QSA,L]

I would guess you recieve the "Internal Server Error" because of the malformed .htaccess. Your "php native" does not use the .htaccess, thus not a compare here.

Personally never used the one of the images with a webserver - if it is not the malformed .htaccess, you should check the server logs what they tell you.

What happens if you call the "/index.php" file directly ? is php executed at all ?

I would say, you are hitting casual apache / fpm errors - thus out of the scope of this issue tracker (however I'm not a official, so don't take that for granted).

cdchn1989 commented 1 year ago

Should be the content of the .htaccess file not be like this (!!! linebreaks !!!:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^ index.php [QSA,L]

Not sure about that file, never used "that" construct - personally I use more something like following (with non-docker instances):

RewriteEngine On

# Store the current location in an environment variable CWD to use
# mod_rewrite in .htaccess files without knowing the RewriteBase
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=CWD:%2]

# ... additional rules, like block source controle folders etc.
#     for security reason

# Final rewrite to the /index.php script (with proper subfolder handling)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ %{ENV:CWD}index.php [QSA,L]

I would guess you recieve the "Internal Server Error" because of the malformed .htaccess. Your "php native" does not use the .htaccess, thus not a compare here.

Personally never used the one of the images with a webserver - if it is not the malformed .htaccess, you should check the server logs what they tell you.

What happens if you call the "/index.php" file directly ? is php executed at all ?

I would say, you are hitting casual apache / fpm errors - thus out of the scope of this issue tracker (however I'm not a official, so don't take that for granted).

Hello sbuerk thank you so much for your reply, Actually this line breaks was github mistake, I will try storing the current location ans unsing this in .htaccess, when a run index.php everything works pretty well and fine. My issue is just whit these "sub routes" and I guess you are rigth, Let me try and I gonna tell was happened. Is like instead of get route is specting a folder or something like that (Actually I will try just to see what happend).

Regards.

cdchn1989 commented 1 year ago

@sbuerk I putted your recomendations on .htaccess and this is what I'm getting is an error with some apache commands. With mime I got the same error. I will try to fix this.

logs-docker

Apparently I need to install rewrite module.

sbuerk commented 1 year ago

@cdchn1989 Reading the log, it indicates that the rewrite module is not loaded, yes. That error should have been in the log with your htaccess too.

I guess, albit not tested, that you need a Dockerfile e.g. like following:

FROM php:8.1-apache

# activate needed apache modules
RUN a2enmod rewrite

or at least add the RUN in your Dockerfile. the "FROM" is just example, use the image you are using right now instead.

cdchn1989 commented 1 year ago

@cdchn1989 Reading the log, it indicates that the rewrite module is not loaded, yes. That error should have been in the log with your htaccess too.

I guess, albit not tested, that you need a Dockerfile e.g. like following:

FROM php:8.1-apache

# activate needed apache modules
RUN a2enmod rewrite

or at least add the RUN in your Dockerfile. the "FROM" is just example, use the image you are using right now instead.

Hello thank you so much for your reply and this is what I did and works fine, my issue now is with railway docker no running properly but this is another topic, so again thank you so much.

Regards.