如何为开发环境配置 vhosts/apache 子域?
Posted
技术标签:
【中文标题】如何为开发环境配置 vhosts/apache 子域?【英文标题】:How to configure vhosts/apache subdomains for dev environment? 【发布时间】:2020-05-05 08:52:56 【问题描述】:我在为 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>
如果我在 vhost 中将 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
docker-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
【问题讨论】:
【参考方案1】:您应该在 VirtualHost 指令中使用 *:80
,并更改 ServerName 和 ServerAliases 以匹配您请求中的备用主机名。
调试见apachectl -S
【讨论】:
无法通过自定义域(“网站”)从主机访问,甚至无法访问主虚拟主机。在主机上的 /etc/hosts 中添加了 0.0.0.0 网站。仍然只能通过 localhost:8080 访问以上是关于如何为开发环境配置 vhosts/apache 子域?的主要内容,如果未能解决你的问题,请参考以下文章