我创建了一个非常简单的 Dockerfile:
FROM php:7.0-apache
COPY src/ /var/www/html
EXPOSE 80
但是当我启动它时,我收到此错误:
AH00558:apache2:无法可靠地确定服务器的完全限定域名(使用 172.17.0.2)。全局设置“ServerName”指令以抑制此消息
所以,我不知道如何解决。有什么想法吗?
我想提供另一种(也许更类似于 Docker 的)方法来解决警告消息。但首先,在盲目设置 ServerName
之前,了解 Apache 到底在做什么来弄清楚域名是有意义的。
http://ratfactor.com/apache-fqdn.html有一篇很好的文章解释了这个过程。 一旦我们确认 Apache 使用
getnameinfo
来查找名称,并且它使用
/etc/nsswitch.conf
来确定首先到哪里查找,我们就可以找出要修改的内容。如果我们检查容器内的
nsswitch.conf
,我们可以看到它在外部 DNS 之前先在
/etc/hosts
文件中查找主机:# cat /etc/nsswitch.conf | grep hosts
hosts: files dns
了解了这一点,也许我们可以在 Docker 运行时影响该文件,而不是将其添加到我们的配置文件中?
如果我们查看 Docker 运行参考文档,就会发现有一个关于
/etc/hosts
文件的部分,位于
https://docs.docker.com/engine/reference/run/#managing-etchosts。它提到: 您的容器将在 /etc/hosts 中包含一些行,这些行定义容器本身的主机名以及 localhost 和一些其他常见内容。
因此,如果我们只设置容器主机名,它将被添加到
/etc/hosts
,这将解决 Apache 警告。幸运的是,使用
--hostname
或 -h
选项可以很容易地做到这一点。如果我们将其设置为完全限定的域名,Docker 会将其设置在我们的 /etc/hosts
文件中,Apache 会选择它。尝试这个非常容易。带有警告的示例(无主机名):
$ docker run --rm php:7.0-apache
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Sun Sep 17 19:00:32.919074 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/7.0.23 configured -- resuming normal operations
[Sun Sep 17 19:00:32.919122 2017] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
现在将
-h
选项设置为完全限定域名:
$ docker run --rm -h myapp.mydomain.com php:7.0-apache
[Sun Sep 17 19:01:27.968915 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/7.0.23 configured -- resuming normal operations
[Sun Sep 17 19:01:27.968968 2017] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
希望这有助于回答问题并提供一些有关 Apache 和完全限定域名的学习。
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
RUN service apache2 restart
这应该消除警告。
您也可以从终端执行此操作
echo "ServerName localhost" >> /usr/local/etc/apache2/apache2.conf
并且不要忘记重新启动服务器
sudo systemctl restart apache2
服务器重新启动后错误应该消失。
ServerName
未设置,因此它将假设机器 IP 相同。
如果您查看
/etc/apache2/sites-available
中存在的默认配置,您会发现
000-default.conf
和 default-ssl.conf
root@d591ab6febff:/etc/apache2/sites-available# cat 000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
ServerName 指令在配置中被注释
#ServerName www.example.com
因此,如果您担心警告,您应该将其更改为您想要的值,如下所示
ServerName www.tarunlalwani.com
ServerAlias tarunlalwani.com
您需要在文件夹中创建一个配置,然后使用 Dockerfile 覆盖默认配置中的文件。然后警告就会消失。
运行 docker 文件时出现以下错误:
转到root
转到
etc
然后转到
apache2
然后是 nano
apache2.conf
然后在
# The directory where shm and other runtime files will be stored.
ServerName localhost
保存文件并退出root,再次运行
docker run
命令,就可以了。
同一问题的 YouTube 视频参考链接:请参阅此示例:
tagplus5/docker-php/7-apache/Dockerfile
apt-get install -y --no-install-recommends iproute2 apache2 php7.0 libapache2-mod-php7.0 \
php7.0-mysql php7.0-sqlite php7.0-bcmath php7.0-curl ca-certificates && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/* && \
echo "ServerName $(ip route get 8.8.8.8 | awk '{print $NF; exit}')" >> /etc/apache2/apache2.conf && \
(尽管 iproute 在 Mac 上的工作方式可能有所不同:这取决于您的主机。请参阅iproute2mac 问题 8
sed -i -e 's/Listen 80/Listen 80\nServerName localhost/' /etc/apache2/ports.conf
Dockerfile 或入口点文件(如果使用)。
FROM php:7.2-apache
COPY my_script.php /var/www/html/index.php
从这个 dockerfile 启动容器时,出现错误:
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.22.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.22.0.2. Set the 'ServerName' directive globally to suppress this message
[Fri Aug 30 14:22:04.845355 2024] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.2.34 configured -- resuming normal operations
[Fri Aug 30 14:22:04.845673 2024] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
我通过像这样编辑我的 Dockerfile 解决了这个错误(添加了
RUN
命令):
FROM php:7.2-apache
RUN test -w /etc/apache2/apache2.conf && echo "ServerName docker.localhost" >> /etc/apache2/apache2.conf
COPY my_script.php /var/www/html/index.php
从这个更新的 dockerfile 启动容器时,没有错误:
[Thu Sep 05 12:11:45.659749 2024] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.2.34 configured -- resuming normal operations
[Thu Sep 05 12:11:45.659809 2024] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'