首页 > 解决方案 > 如何为开发环境配置 vhosts/apache 子域?

问题描述

我在为 PHP 项目配置对子域的访问时遇到困难。仅适用于本地主机。虚拟主机(localhost.conf、backoffice.website.conf 等)从 docker-compose 行复制到 Docker 机器:./dev/conf/default/sites/:/etc/apache2/from-host并附加到 Dockerfile 中的 apache2.conf。怎么了 ?

子域:

localhost => public_html/www/index.php (this works)
backoffice.website => public_html/backoffice/index.php
api.website => public_html/api/index.php
health.website => public_html/health/index.php

工作localhost.conf

<VirtualHost *:80>
        ServerAdmin admin@website.ee
        DocumentRoot /var/www/dev/public_html/www
        ServerName localhost
        ServerAlias localhost

        <Directory /var/www/dev/public_html/www>
                        Options FollowSymLinks MultiViews
                        AllowOverride All
                        Order allow,deny
                        allow from all
        </Directory>
        ErrorLog /var/log/apache2/website.error.log
        LogLevel warn
        CustomLog /var/log/apache2/website.access.log combined

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png|svg|map)$ [NC]
        RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
</VirtualHost>

其他虚拟主机似乎无法正常工作,例如backoffice.website.conf

<VirtualHost backoffice.website:80>
        ServerAdmin admin@website.ee
        DocumentRoot /var/www/dev/public_html/backoffice
        ServerName backoffice.website


        <Directory /var/www/dev/public_html/backoffice>
                        Options FollowSymLinks MultiViews
                        AllowOverride All
                        Order allow,deny
                        allow from all
        </Directory>

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
</VirtualHost>

如果我在虚拟主机中将 localhost 更改为“网站”,我也无法访问网站。也许我没有启用

我的Dockerfile

FROM php:7.2-apache

RUN pecl install redis-5.1.1 \
    && pecl install xdebug-2.9.0 \
    && docker-php-ext-enable redis xdebug \
    && rm -rf /tmp/* \
    && mkdir /var/www/dev \
    && chmod -R 755 /var/www

RUN mkdir -p  /etc/apache2/from-host

RUN echo "" >> /etc/apache2/apache2.conf \
    && echo "# Include the configurations from the host machine" >> /etc/apache2/apache2.conf \
    && echo "IncludeOptional from-host/*.conf" >> /etc/apache2/apache2.conf

RUN a2enmod rewrite \
    && service apache2 restart

WORKDIR /var/www/dev

码头工人-compose.yml

version: '3.1'

services:
  web:
    build: .
    depends_on:
      - db
    restart: always
    volumes:
      - ./dev:/var/www/dev/
      - ./dev/conf/default/sites/:/etc/apache2/from-host
    environment:
      XDEBUG_CONFIG: remote_host=10.10.10.219
    ports:
      - 8080:80

标签: phpapachedockerdockerfilevhosts

解决方案


您应该*:80在 VirtualHost 指令中使用,只需更改 ServerName 和 ServerAliases 以匹配您请求中的备用主机名。

有关调试,请参阅apachectl -S


推荐阅读