【问题标题】:Docker PHP + Apache deployment on Heroku crashesHeroku 上的 Docker PHP + Apache 部署崩溃
【发布时间】:2019-05-21 22:01:48
【问题描述】:

我正在尝试部署一个 docker 容器,虽然我尝试了几个选项,但它总是崩溃。在本地工作正常,在端口 8080 上。

现在,我正在使用 PHP + Apache。

我的文件夹层次结构如下所示:

docker-compose.yml
Dockerfile 
www
   .htaccess
   index.php

我的 Dockerfile 是这个:

FROM php:7.1-apache
COPY www /var/www/html
RUN a2enmod rewrite
RUN a2enmod lbmethod_byrequests
RUN service apache2 restart
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

我的 docker-composer.yml:

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: myproject
    ports:
      - 8080:80

还有.htaccess:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

当我检查 Heroku 日志 (heroku logs --tail) 时,我看到的是:

Starting process with command `/usr/sbin/apache2ctl -D FOREGROUND`
State changed from starting to crashed
Process exited with status 1
(13)Permission denied: AH00072: make_sock: could not bind to address [::]:80
(13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
Action '-D FOREGROUND' failed.

【问题讨论】:

    标签: php apache docker heroku deployment


    【解决方案1】:

    我最近自己解决了这个问题here,以防你想再次尝试 Heroku :)

    本质上,我使用 Docker CMD 语句在运行时更改 apache 端口配置。

    CMD sed -i "s/80/$PORT/g" /etc/apache2/sites-enabled/000-default.conf /etc/apache2/ports.conf && docker-php-entrypoint apache2-foreground
    

    【讨论】:

    • 您也可以在000-default.conf 和ports.conf 文件中使用${PORT}。例如Listen ${PORT}
    • 你还在使用这个设置吗?看起来它停止工作了。我得到:apache2: Syntax error on line 150 of /etc/apache2/apache2.conf: Could not open configuration file /etc/apache2/ports.conf: Permission denied
    • 也停止为我工作。在@DamianPaszkowski 下方查看我的新答案
    【解决方案2】:

    @caleb-gray 接受的答案于 2019 年 9 月停止为我工作,但我想出了一个替代解决方案:

    我没有替换 Dockerfile 中的端口值,而是用 env 变量替换了原始 apache2 .conf 文件中的端口值,然后将它们复制到 Docker 映像中。这也意味着 .conf 文件现在是我的存储库的一部分(我从正在运行的 Docker 容器中复制了它们)。

    我的文件夹层次结构:

    Dockerfile
    apache-config/
      ports.conf
      000-default.conf
    

    例如在我的 ports.conf 中

    Listen 80
    

    改为

    Listen ${PORT}
    

    在我的 Dockerfile 中:

    COPY ./apache-config/ports.conf /etc/apache2/ports.conf                                
    COPY ./apache-config/000-default.conf /etc/apache2/sites-available/000-default.conf    
    CMD docker-php-entrypoint apache2-foreground
    

    目前工作正常

    【讨论】:

      【解决方案3】:

      答案很简单,你不能绑定到一个特定的端口,你应该使用 $PORT 环境变量。有关此线程的更多信息here。

      【讨论】:

      • 嗨!感谢您的回答。我一直在尝试不同的选择,但没有运气。我删除了:EXPOSE 80 并添加了:ENV PORT "$PORT" EXPOSE ${PORT} 但 heroku 仍然告诉我同样的错误。 :(
      • 我最终选择了使用 Google Cloud。一切都像魅力一样。
      猜你喜欢
      • 2012-10-02
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2022-10-07
      • 2016-12-14
      • 1970-01-01
      相关资源
      最近更新 更多