通过 www.example.com 在 localhost:8000 上为 wordpress 重定向 Apache 虚拟主机
Apache virtual host redirect for wordpress on localhost:8000 via www.example.com
我正在将 2 个 wordpress 网站(其中一个是子域)移动到 docker 开发容器下的本地计算机。
我想使用主机文件通过域名在我的开发机器中访问它们,但我正在努力让我的 apache 虚拟主机工作。
假设:
站点 1 是 'www.example.com'
站点 2 是 'sub.example.com'
每个都有自己的 MySQL 数据库和 wordpress 代码库,在不同的 docker-compose 容器上设置。
所以:
站点 1:http://localhost:8000
上的 wwww.example.com
站点 2:http://localhost:8001
上的 sub.example.com
通过 localhost:8000 和 localhost:8001
都可以正常工作
我将本地主机文件更改为:
127.0.0.1 localhost
127.0.0.1 example.com www.example.com sub.example.com
我正在努力为这种情况设置 apache 虚拟主机,
这是我试过的:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8000/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
ProxyPass / http://localhost:8001/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
直接转到 http://localhost:8000 和 http://localhost:8001 有效。
当我转到 http://www.example.com 时,出现以下错误:
Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.4.25 (Debian) Server at www.example.com Port 80
Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.4.25 (Debian) Server at sub.example.com Port 80
有人可以为我指明正确的方向,告诉我如何设置虚拟主机吗?
谢谢
您可以尝试检查位于 var/log/httpd
中的日志(您也可以尝试 sudo locate access.log
)以了解发生的实际错误。
您缺少的一件事是 ProxyPassReverse
如果您的网站正在重定向,它将重写 URL。有了它,您的配置将如下所示
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
ProxyPass / http://localhost:8001/
ProxyPassReverse / http://localhost:8001/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
我知道我的问题在这里可能是题外话,但既然我设法解决了我的问题,我希望你能允许我的答案保留在这里,以便它可以帮助其他人。
我没有提到 Apache 服务器 运行 在它自己的 Docker 容器中。
这是我解决问题的方法:
在主机中:
在主机文件中包含 www.example.com、example.com 和 sub.example.com:
127.0.0.1 localhost
127.0.0.1 example.com www.example.com sub.example.com
在 apache docker 容器中:
因为 apache 运行 在它自己的 docker 容器中,localhost:8000 指向 apache docker 容器的本地主机,这里是不正确的。
它需要指向主机的 ip 地址,在我的例子中是 192.168.1.100.
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://192.168.1.100:8000/
ProxyPassReverse / http://192.168.1.100:8000/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
ProxyPass / http://192.168.1.100:8001/
ProxyPassReverse / http://192.168.1.100:8001/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
重启apache
在每个 Worpdress 网站上:
最后要做的是更改 'site_url' 和 'home' 设置以指向主机的 IP 地址(此处为 192.168.1.100)。
这可以通过两种方式完成:
通过转到 wordpress 数据库,table wp_options 并修改:
site_url 和 home 到 http://192.168.1.100:8000 对于 www.example.com 和example.com;和
site_url and home to http://192.168.1.100:8001 for sub.example.com
或者登录您的 Wordpress 仪表板 (/wp-admin),然后转到 设置 并修改:
WordPress 地址 (URL) 和 站点地址 (URL) 至 http://192.168.1.100:8000 对于 www.example.com 和 example.com;和
WordPress 地址 (URL) 和 站点地址 (URL) 至 http://192.168.1.100:8001 子example.com
您不需要重新启动 Wordpress 容器,但如果您遇到任何错误,请重新启动它们并再次检查。
我正在将 2 个 wordpress 网站(其中一个是子域)移动到 docker 开发容器下的本地计算机。
我想使用主机文件通过域名在我的开发机器中访问它们,但我正在努力让我的 apache 虚拟主机工作。
假设:
站点 1 是 'www.example.com'
站点 2 是 'sub.example.com'
每个都有自己的 MySQL 数据库和 wordpress 代码库,在不同的 docker-compose 容器上设置。
所以:
站点 1:http://localhost:8000
上的 wwww.example.com
站点 2:http://localhost:8001
上的 sub.example.com
通过 localhost:8000 和 localhost:8001
我将本地主机文件更改为:
127.0.0.1 localhost
127.0.0.1 example.com www.example.com sub.example.com
我正在努力为这种情况设置 apache 虚拟主机, 这是我试过的:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8000/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
ProxyPass / http://localhost:8001/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
直接转到 http://localhost:8000 和 http://localhost:8001 有效。
当我转到 http://www.example.com 时,出现以下错误:
Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.4.25 (Debian) Server at www.example.com Port 80
Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.4.25 (Debian) Server at sub.example.com Port 80
有人可以为我指明正确的方向,告诉我如何设置虚拟主机吗?
谢谢
您可以尝试检查位于 var/log/httpd
中的日志(您也可以尝试 sudo locate access.log
)以了解发生的实际错误。
您缺少的一件事是 ProxyPassReverse
如果您的网站正在重定向,它将重写 URL。有了它,您的配置将如下所示
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
ProxyPass / http://localhost:8001/
ProxyPassReverse / http://localhost:8001/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
我知道我的问题在这里可能是题外话,但既然我设法解决了我的问题,我希望你能允许我的答案保留在这里,以便它可以帮助其他人。
我没有提到 Apache 服务器 运行 在它自己的 Docker 容器中。
这是我解决问题的方法:
在主机中:
在主机文件中包含 www.example.com、example.com 和 sub.example.com:
127.0.0.1 localhost
127.0.0.1 example.com www.example.com sub.example.com
在 apache docker 容器中:
因为 apache 运行 在它自己的 docker 容器中,localhost:8000 指向 apache docker 容器的本地主机,这里是不正确的。
它需要指向主机的 ip 地址,在我的例子中是 192.168.1.100.
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://192.168.1.100:8000/
ProxyPassReverse / http://192.168.1.100:8000/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
ProxyPass / http://192.168.1.100:8001/
ProxyPassReverse / http://192.168.1.100:8001/
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
重启apache
在每个 Worpdress 网站上:
最后要做的是更改 'site_url' 和 'home' 设置以指向主机的 IP 地址(此处为 192.168.1.100)。
这可以通过两种方式完成:
通过转到 wordpress 数据库,table wp_options 并修改:
site_url 和 home 到 http://192.168.1.100:8000 对于 www.example.com 和example.com;和
site_url and home to http://192.168.1.100:8001 for sub.example.com
或者登录您的 Wordpress 仪表板 (/wp-admin),然后转到 设置 并修改:
WordPress 地址 (URL) 和 站点地址 (URL) 至 http://192.168.1.100:8000 对于 www.example.com 和 example.com;和
WordPress 地址 (URL) 和 站点地址 (URL) 至 http://192.168.1.100:8001 子example.com
您不需要重新启动 Wordpress 容器,但如果您遇到任何错误,请重新启动它们并再次检查。