php - 如何为开发环境配置 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
解决方案
您应该*:80
在 VirtualHost 指令中使用,只需更改 ServerName 和 ServerAliases 以匹配您请求中的备用主机名。
有关调试,请参阅apachectl -S
推荐阅读
- python - 如何减少numpy数组中的行重复次数
- java - 将多个项目和依赖项构建到一个 jar 文件中
- java - 使用 Java 进行极慢的内置 AES 加密
- javascript - 使用 jQuery 修改 classList
- azure-functions - 重放编排时是否应该调用 SetCustomStatus()?
- java - 弹簧数据 jpa 中的 `set @variable = 0` 等价物
- c# - Azure DevOps 发布管道,从 C# 代码中读取环境变量
- c++-winrt - cppwinrt Panel 的子类(或其他类),需要哪些构造函数?
- http - Apache2 自定义标头 - 在标头上添加 Remort 端口
- python - 一个csv python pandas中的几个图表