【问题标题】:Apache in Docker won't deliver sitesDocker 中的 Apache 不会提供站点
【发布时间】:2014-09-09 18:15:40
【问题描述】:

在 docker 容器中安装 apache 网络服务器后,我想显示一个示例页面以确保其正常工作。但是我总是得到 404 Not found。

这是 dockerfile

FROM ubuntu:14.04
MAINTAINER <mail@example.org>
RUN DEBIAN_FRONTEND=noninteractive

# Install required packages
RUN apt-get -y update
RUN apt-get install -y apache2 libapache2-mod-php5 php5-gd php5-json php5-mysql php5-curl php5-intl php5-imagick bzip2 wget

# Enable the php mod
RUN a2enmod php5

# Configuration for Apache
ADD ./apache.conf /etc/apache2/sites-available/
RUN ln -s /etc/apache2/sites-available/apache.conf /etc/apache2/sites-enabled/
RUN a2enmod rewrite

RUN mkdir /var/www/test && echo "<html><body><h1>Yo</h1></body></html>" >> /var/www/test/index.html

EXPOSE :80

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

apache.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www

    <Directory /var/www/test/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

容器已启动并正在运行。将 80 端口转发到主机上的 8080。

当我浏览到 localhost:8080 时,我可以看到 apache2 默认页面。但是,当我转到 localhost:8080/test/index.html 时,我只得到 404 Not found。

【问题讨论】:

  • 我在你的 .conf 文件中没有看到:ServerName "your.domain" 或 localhost

标签: php apache apache2 webserver docker


【解决方案1】:

这是因为您仍然拥有默认的 sites-enabled 配置。

要删除此文件,您应该添加一行来删除它(可能与添加新虚拟主机配置的位置大致相同):

RUN rm -rf /etc/apache2/sites-enabled/000-default.conf

【讨论】:

  • 这是正确的答案。我不知道为什么默认 conf 文件会导致这些问题,但在禁用或删除后一切正常。如果您不想删除它,只需使用以下命令禁用它: RUN a2dissite 000-default.conf
【解决方案2】:

我猜这是权限问题。请记住,从 dockerfile 创建 docker 容器时,一切都以 root 身份运行。所以你的/var/www/test/index.html 文件归根用户所有。我的猜测是它有一些受限的权限。

试试这个:

之后

RUN mkdir /var/www/test && echo "<html><body><h1>Yo</h1></body></html>" >> /var/www/test/index.html

添加

RUN chmod 0755 /var/www/test/index.html

这将重置该文件的权限,以便任何人都可以读取它。如果我是对的,那将解决您的问题。

【讨论】:

  • 谢谢,但我已经尝试过了。马库斯休斯的回答成功了。
【解决方案3】:

我刚刚遇到了类似的问题,并且能够解决它。

首先判断apache2是否在运行

/etc/init.d/apache2 status

如果它没有运行,你可以将它添加到你的 dockerfile:

CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    相关资源
    最近更新 更多