Docker 安装 Apache

httpd 镜像的配置路径、静态站点、虚拟主机与反向代理。

Beginner
Key Points官方镜像叫 httpd,配置在 conf/httpd.conf 与 conf.d/*.conf,站点根目录 /usr/local/apache2/htdocs。

启动

Bash
docker run -d --name httpd -p 8080:80 httpd:2.4-alpine
curl -I http://127.0.0.1:8080

挂静态站点

Bash
docker run -d --name site -p 8080:80 \
  -v "$PWD/dist:/usr/local/apache2/htdocs:ro" \
  httpd:2.4-alpine

虚拟主机

APACHE
<VirtualHost *:80>
    ServerName app.example.com
    DocumentRoot /usr/local/apache2/htdocs

    ProxyPreserveHost On
    ProxyPass        /api/ http://api:8080/
    ProxyPassReverse /api/ http://api:8080/

    ErrorLog  /proc/self/fd/2
    CustomLog /proc/self/fd/1 combined
</VirtualHost>
Bash
docker run -d --name httpd -p 80:80 \
  -v "$PWD/vhost.conf:/usr/local/apache2/conf.d/vhost.conf:ro" \
  --network app-net httpd:2.4-alpine

docker exec httpd httpd -t

日志与模块

  • 日志写 /proc/self/fd/1 与 /2 才能进 docker logs
  • 反代模块 mod_proxy_http 官方镜像已编译,只需 LoadModule
  • httpd -M 列出已加载模块,排查「模块没开」很快
The official image's DocumentRoot is /usr/local/apache2/htdocs, not /var/www/html (that is the Debian package path). Copying the wrong one yields 403.

Friends & Partners