简体   繁体   中英

Docker php with apache on localhost: ERR_SSL_PROTOCOL_ERROR

I am trying to serve a hello_world.php file with apache and php in a docker container on Windows 10 with Docker Desktop.

Here's the Dockerfile:

FROM php:7.3-apache
# install git and zip, both needed for composer
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y git && \
    apt-get install zip unzip
# install composer
RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin --filename=composer && chmod +x /usr/bin/composer 

RUN a2enmod headers
RUN echo 'ServerName localhost' >> /etc/apache2/apache2.conf
COPY . /var/www/html
WORKDIR /var/www/html

#RUN composer install
EXPOSE 80

As you can see, some of the additional code for setting up composer etc. (so I can dockerize an existing PHP application) is even commented out but it does not work with even a basic hello world php file and no other files (no .htaccess for example).

Firstly, I build the image with docker build. . When I then execute docker run -it -p 8000:80 d7cd1255a20f , I get the console output

[Tue Jun 02 16:01:29.592455 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.3.18 configured -- resuming normal operations
[Tue Jun 02 16:01:29.592531 2020] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

Looks fine to me. I open http://localhost:8000 in my browser and get "Can't establish a secure connection. ERR_SSL_PROTOCOL_ERROR". The server log shows (two lines each time I load the page in my browser):

172.17.0.1 - - [02/Jun/2020:16:02:55 +0000] "\x16\x03\x01\x02" 400 0 "-" "-"
172.17.0.1 - - [02/Jun/2020:16:02:55 +0000] "\x16\x03\x01\x02" 400 0 "-" "-"

I have already researched this and it looks like it's the beginning of a TLS handshake.

How can I get this to work?

Do you have any .htaccess file inside /var/www/html ? It looks like Apache is performing an automated redirect to HTTPS. Building the image with an empty directory as /var/www/html and running docker run -it -p 8000:80 d7cd1255a20f correctly runs over HTTP.

okay Try this

Follow the folder structure

phpExample/
   php/
      index.php
   dockerfile

dockerfile

FROM php:7.3-apache
COPY php/ /var/www/html
EXPOSE 80

index.php

<?php
echo "Hello World from Docker container using PHP<br>";
echo '<img src="https://www.docker.com/sites/default/files/d8/2019-07/Moby-logo.png">';
?>

After this run following command

docker build -t helloWorld-php-docker .

When the build is completed.

docker run -p 80:80 helloWorld-php-docker

Please make sure port 80 is not in use, or what you can do change the port number

PS: directory structure is not mandatory. I added just to structure the example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM