【问题标题】:Using Domain name instead of localhost in with https in xampp在xampp中使用域名而不是localhost
【发布时间】:2022-01-03 03:10:01
【问题描述】:

我的问题可能很愚蠢,但老实说,我搜索了很多并获得了成功但并不完整。

我在 Windows 8 中使用 xampp。

我的主机文件如下所示。

    127.0.0.1   localhost
    127.0.0.1   www.mysite.com

我的 httpd-vhosts.config 如下所示。

    NameVirtualHost 127.0.0.1
    <VirtualHost 127.0.0.1>
        DocumentRoot "C:/xampp/htdocs"
        ServerName localhost
    </VirtualHost>
    <VirtualHost 127.0.0.1>
        ServerName www.mysite.com
        ServerAlias mysite.com
        DocumentRoot "C:/xampp/htdocs/mysite"
    </VirtualHost>

这非常适合 http。 但我启用了 ssl。

当我输入http://localhosthttps://localhost 时,两者都可以正常工作。

当我输入http://mysite.com 时,它可以工作,

当我输入 https://mysite.com 时,它被重定向为 https://mysite.com/xampp/ 并显示 xampp 的默认欢迎页面。

我尝试了以下事情。

1) 我没有使用 127.0.0.1,而是尝试在 httpd-vhosts.conf 中使用 *:80 但结果是一样的。

2) 我没有使用 127.0.0.1,而是尝试在 httpd-vhosts.conf 中使用 *:443 但是在重新启动时 apache 无法再次启动。

请告诉我如何通过域名而不是使用 https 或 http 的 localhost 访问我的网站。

【问题讨论】:

  • 尝试在 php.ini 中取消注释 extension=php_openssl.dll 并重启 apache
  • 你提到的那一行已经被注释掉了,这就是为什么我可以通过https://localhost/othersite/在https中使用我的其他站点/我应该评论重启,然后再次取消注释并重新启动?跨度>
  • 可能也需要 apache 支持,取消注释 xampp/apache/conf/httpd.conf 中的#LoadModule ssl_module modules/mod_ssl.so
  • @shapeshifter 你提到的文件已经被取消注释了。
  • @JeetChaudhari 你有没有在 Windows 防火墙中为端口添加异常

标签: php apache xampp localhost domain-name


【解决方案1】:

我尝试了很多东西,但我想我错过了基本的编辑。

现在一切正常。

现在主机文件仍然与问题中提到的相同。我没有对其进行任何更改。

我在 httpd-vhosts.config 中更改了端口,如下所示。

NameVirtualHost *
    <VirtualHost *>
        DocumentRoot "C:/xampp/htdocs"
        ServerName localhost
    </VirtualHost>
    <VirtualHost *>
        ServerName www.mysite.com
        ServerAlias mysite.com
        DocumentRoot "C:/xampp/htdocs/mysite"
    </VirtualHost>

另外我错过的步骤是在 httpd-vhosts.config 的同一文件夹中编辑 httpd-ssl.config 文件。

我刚刚在 http-ssl.config 文件的最后一行之前添加了以下行,即

<VirtualHost _default_:443> 
    DocumentRoot "C:/xampp/htdocs/mysite" 
    ServerName www.mysite.com:443 
    ServerAlias mysite.com:443  
    SSLEngine on 
    SSLCertificateFile "conf/ssl.crt/server.crt" 
    SSLCertificateKeyFile "conf/ssl.key/server.key" 
</VirtualHost> 

感谢所有朋友在这方面帮助我,如果没有您的链接,我永远无法发现我需要再编辑一个文件。

【讨论】:

    【解决方案2】:

    让我也为其他人一步一步解释。

    1.将您的自定义域名映射到 HOSTS 文件中的 localhost。

    打开主机文件并添加以下行。

    127.0.0.1 www.example.com
    

    2. 告诉 XAMPP 您的自定义域。

    将以下内容添加到 httpd-vhosts.conf

    <VirtualHost *>
        ServerName www.example.com
        ServerAlias example.com
        DocumentRoot "C:/xampp/htdocs/example"
    </VirtualHost>
    

    如果您的 localhost 有端口,请将其添加为 &lt;VirtualHost *:80&gt;

    重启apache,现在你可以在浏览器中访问http://example.com了。

    3.如果你想访问https://example.com

    将下面一行添加到 httpd-vhosts.conf

    <VirtualHost *:443>
        DocumentRoot "C:/xampp/htdocs/example"
        ServerName www.example.com
        SSLEngine on
        SSLCertificateFile "conf/ssl.crt/server.crt"
        SSLCertificateKeyFile "conf/ssl.key/server.key"
        <Directory "C:/xampp/htdocs/example">
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    

    【讨论】:

    • 如果我想要同样的解决方案但我无法修改 hosts 文件,我该怎么办?
    • 你必须编辑hosts文件,否则会搜索外面的域。如果您无法编辑 hosts 文件,请尝试管理员权限。
    • 使用 Apache 的本地代理服务器功能怎么样?,也许我可以使用此功能来启用为我的域添加特定规则的代理,我不知道如何使用代理模块,但可能它可以解决我的问题
    • cont 文件夹到底在哪里?
    • 在 Windows 中它在 "C:\Windows\System32\Drivers\etc\hosts" 在 ubuntu udpate "\etc\hosts"
    【解决方案3】:

    我对 apache 不太熟悉,但可能没有指定端口默认为 :80,添加它会神奇地解决所有问题?

    <VirtualHost 127.0.0.1:443>
        ServerName www.mysite.com
        ServerAlias mysite.com
        DocumentRoot "C:/xampp/htdocs/mysite"
    </VirtualHost>
    

    【讨论】:

    • 这导致apache服务器无法启动。错误日志显示。 [Fri Apr 26 10:06:38.323550 2013] [ssl:warn] [pid 7892:tid 324] AH01873:初始化:未配置会话缓存 [提示:SSLSessionCache]
    【解决方案4】:

    我已经在谷歌上搜索了几个小时,试图弄清楚为什么最新的 XAMPP 版本会在页面生成时间上设置 1200 毫秒...我认为这可能是我的代码与一些非常复杂的类系统一起工作......这个线程指出了整个本地主机 127.0.0.1

    我在 Windows 7 上,我没想过使用 CMD 来“ping localhost”

    结果是 "::1:" 而不是 127.0.0.1

    在快速编辑 windows/system32/drivers/etc/host 文件后取消注释掉该行

    127.0.0.0 本地主机

    我的页面时间恢复正常。可能是其他人最近遇到了这个问题,并且看到这个帖子在 Google 中排名第一,那么祝你好运!

    【讨论】:

      【解决方案5】:

      我从多个自定义域开始。请参阅下面的新代码:

      注意:WordPress 去掉了反斜杠,所以下面我用正斜杠替换了它们。无论哪种方式,我都相信工作。

      <VirtualHost *:80>
          DocumentRoot "C:/xampp/htdocs"
          ServerName localhost
      </VirtualHost>
      
      <VirtualHost *:80>
          DocumentRoot "C:/Users/Austin Passy/Documents/InMotion Hosting/frostywebdev.com/html"
          ServerName frostyweb.dev
          <Directory "C:/Users/Austin Passy/Documents/InMotion Hosting/frostywebdev.com/html">
          Options Indexes FollowSymLinks ExecCGI Includes
              Order allow,deny
              Allow from all
          </Directory>
      </VirtualHost>
      
      <VirtualHost *:80>
          DocumentRoot "C:/xampp/htdocs/eateryengine"
          ServerName eateryengine.dev
          <Directory "C:/xampp/htdocs/eateryengine">
          Options Indexes FollowSymLinks ExecCGI Includes
              Order allow,deny
              Allow from all
          </Directory>
      </VirtualHost>
      

      【讨论】:

      • 您需要编辑hosts 文件以使用自定义本地域...
      【解决方案6】:

      我使用自己的域(以 .lc 结尾)在 localhost 上开发 Web 应用程序。我将描述动态 .lc 域和开发环境的简单解决方案,它可以在不依赖互联网连接的情况下工作。

      我也在我的博客上写过:http://www.michalseidler.com/development/localhost-development-enviromet-for-php/

      对于此示例,我尝试使用 Wamp Server 描述本地动态域 *.lc 的配置。我将我的项目存储在 C:\wamp\www\projects\projectname\ 中,并且我使用动态映射 projectname.lc。这意味着我可以使用域 [项目目录名称].lc 访问每个项目目录

      第 1 步 - 本地 WAMP 服务器的配置

      首先你需要将 *.lc 域的配置放到 httpd.conf 中:

      <VirtualHost 127.0.0.1>
      ServerName lc
      ServerAlias *.lc
      DocumentRoot "C:\wamp\www\projects"
      </VirtualHost>;
      

      您需要将 .htaccess 文件插入项目目录(在我的示例中为:C:\wamp\www\projects),此配置将 *.ls 域映射到项目目录。例如:如果您在目录“myapp”中有资源,您可以使用 www.myapp.lc 在浏览器中打开它。

      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^([^\.]*)\.([^\.]*)$
      RewriteRule (.*) http://www.%1.%2/$1 [L,R=301]
      
      RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.([^.]+)\.([^\.]*)$ [NC]
      RewriteRule ^(.*)$ http://%1.%2.%3/$1 [L,R=301]
      
      RewriteCond %{REQUEST_URI} !^projects/
      RewriteCond %{REQUEST_URI} !^/projects/
      RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
      RewriteRule (.*) %3/$1 [DPI] 
      

      此更改后重新启动 Wamp 服务器

      第 2 步 - 本地 DNS 服务器的配置

      因为我们不能在 Windows 主机文件中使用 *.lc,所以我们需要安装本地 DNS 服务器。我选择 Acrylic DNS Server 是因为它的配置非常简单。

      安装后找到 AcrylicHosts 文件 (C:\Program Files (x86)\Acrylic DNS Proxy) 并插入新行:

      127.0.0.1 *.lc
      

      这只是我们需要的 DNS 配置,所以重启 Acrylic DNS 服务。

      第 3 步 – 配置网络适配器

      最后一步是安装新的假网络适配器并分配 DNS 服务器: 1.单击开始菜单。 2.搜索“cmd”。 3.右键单击“cmd”并选择“以管理员身份运行” 4.输入“hdwwiz.exe” 5.在“欢迎使用添加硬件向导”中,单击下一步。 6.选择“安装我从列表中手动选择的硬件(高级)”,然后单击下一步。 7.向下滚动并选择“网络适配器”,然后单击下一步。 8.在制造商“Microsoft”下选择,然后在网络适配器“Microsoft Loopback Adapter”下选择,然后单击下一步。

      在下一步中,您必须更改新创建的适配器的 TCP/IP 设置: 1.使用管理员帐户登录计算机。 2. 单击开始,指向控制面板,然后单击网络连接。 3.右键单击环回连接,然后单击属性。 4.在此连接使用以下项目框中,单击 Internet 协议 (TCP/IP),然后单击属性。出现 Internet 协议 (TCP/IP) 属性对话框。

      IP addess: 192.168.1.1
      Subnet mask: 255.255.255.0
      Default Gateway: empty
      
      Prefered DNS server: 127.0.0.1
      

      现在关闭所有对话框并完成!您可以尝试打开[您的项目名称].lc

      【讨论】:

        猜你喜欢
        • 2021-11-07
        • 2017-08-02
        • 2019-01-15
        • 2020-01-08
        • 2013-11-20
        • 2016-03-07
        • 2012-06-24
        • 2021-02-27
        相关资源
        最近更新 更多