通过www.example.com在localhost:8000上为wordpress重定向Apache虚拟主机

通过www.example.com在localhost:8000上为wordpress重定向Apache虚拟主机,apache,localhost,port,virtualhost,Apache,Localhost,Port,Virtualhost,我正在将两个wordpress网站(其中一个是子域)移动到我的本地机器docker容器下进行开发 我想使用hosts文件在我的开发机器中通过它们的域名访问它们,但是我很难让我的apache虚拟主机正常工作 让我们说: 网站1是“www.example.com” 站点2是“sub.example.com” 每个都有自己的MySQL数据库和wordpress代码库,安装在不同的docker compose容器上 所以: 网站1:ww.example.com on 站点2:sub.example.co

我正在将两个wordpress网站(其中一个是子域)移动到我的本地机器docker容器下进行开发

我想使用hosts文件在我的开发机器中通过它们的域名访问它们,但是我很难让我的apache虚拟主机正常工作

让我们说:
网站1是“www.example.com”
站点2是“sub.example.com”
每个都有自己的MySQL数据库和wordpress代码库,安装在不同的docker compose容器上

所以:
网站1:ww.example.com on
站点2: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>

服务器名www.example.com
ProxyPass/http://localhost:8000/
命令拒绝,允许
通融
ServerName sub.example.com
ProxyPass/http://localhost:8001/
命令拒绝,允许
通融
直接去上班

当我转到时,会出现以下错误:

服务不可用
由于维护停机或容量问题,服务器暂时无法为您的请求提供服务。请稍后再试。
Apache/2.4.25(Debian)服务器,位于www.example.com端口80

服务不可用
由于维护停机或容量问题,服务器暂时无法为您的请求提供服务。请稍后再试。
位于sub.example.com端口80的Apache/2.4.25(Debian)服务器

有人能告诉我如何设置虚拟主机吗?


谢谢

您可以尝试检查
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>

服务器名www.example.com
ProxyPass/http://localhost:8000/
ProxyPassReverse/http://localhost:8000/
命令拒绝,允许
通融
ServerName sub.example.com
ProxyPass/http://localhost:8001/
ProxyPassReverse/http://localhost:8001/
命令拒绝,允许
通融

我知道我的问题可能离题了,但既然我设法解决了我的问题,我希望您能允许我的答案留在这里,以便对其他人有所帮助。

我没有提到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容器的localhost,这在这里是不正确的。
它需要指向主机的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>

服务器名www.example.com
ProxyPass/http://192.168.1.100:8000/
ProxyPassReverse/http://192.168.1.100:8000/
命令拒绝,允许
通融
ServerName sub.example.com
ProxyPass/http://192.168.1.100:8001/
ProxyPassReverse/http://192.168.1.100:8001/
命令拒绝,允许
通融
重新启动apache

在每个Worpdress站点上: 最后要做的是更改“site_url”和“home”设置,以指向主机的ip地址(此处为192.168.1.100)。 这可以通过两种方式完成:
通过转到wordpress数据库、表格wp_选项并修改:
www.example.com和example.com的网站url主页;和
site\u urlhome到sub.example.com

或者登录Wordpress仪表板(/wp admin),然后转到设置并修改:
WordPress地址(URL)网站地址(URL)到www.example.com和example.com;和
WordPress地址(URL)站点地址(URL)to for sub.example.com


您不需要重新启动Wordpress容器,但如果出现任何错误,请重新启动它们并再次检查。

我添加了ProxyPassReverse,重新启动了apache,同样的错误,检查了日志并意识到我的apache也在docker容器中,因此localhost定义了apache docker localhost,而不是主机的localhost。。。我将localhost:8001更改为host\u machine\u ip:8001,然后它似乎可以工作。我需要做更多的测试,因为出现了一个新问题,,,,谢谢你的帮助。问题比我原来想的更严重,我忘了提到apache正在自己的docker容器中运行。要解决这个问题,需要了解docker公开的端口、apache虚拟主机、主机文件和正确的wordpress设置,例如表wp_选项:home和siteurl。一个重新措辞的问题在这里还会偏离主题吗?