title: Localhost refused to connect on WSL2 when accessed via https://localhost:8000/ but works when using internal WSL IP adress
body: <h5>What I'm Trying to Achieve</h5>
  <p>To access localhost from my local machine during the development of a Symfony web app.</p>
  <h5>My Environment</h5>
  <ul>
  <li>WSL2 running on Windows 10</li>
  <li>Linux, Apache2, MySQL, PHP-7.4 stack (with Xdebug3 intalled)</li>
  <li>Debian 10</li>
  <li>Symfony 5.4 (although not sure on if relevant to this problem)</li>
  </ul>
  <h5>Steps I've Taken</h5>
  <ul>
  <li>Set up WSL2 according to this <a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10#manual-installation-steps" rel="nofollow noreferrer">Microsoft WSL2 tutorial</a></li>
  <li>Set up LAMP stack according to this <a href="https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mariadb-php-lamp-stack-on-debian-10" rel="nofollow noreferrer">Digital Ocean tutorial</a></li>
  <li>Set up Symfony according to this <a href="https://symfony.com/doc/current/setup.html" rel="nofollow noreferrer">Symfony tutorial</a></li>
  <li>Run the following bash script on startup to start my services and set the host to the virtual WSL IP in my xdebug.ini file</li>
  </ul>
  <pre><code>#!/bin/sh
  REMOTEIP=`cat /etc/resolv.conf | grep nameserver | sed 's/nameserver\s//'` 
  sed -i -E &quot;s/client_host=[0-9\.]+/client_host=$REMOTEIP/g&quot; /etc/php/7.4/mods-available/xdebug.ini
  service php7.4-fpm start
  service apache2 start
  service mysql start  
  </code></pre>
  <ul>
  <li>Run my Symfony project on the development server using <code>symfony serve -d</code> (Symfony then tells me &quot;The Web server is using PHP FPM 7.4.23 <a href="https://127.0.0.1:8000%22" rel="nofollow noreferrer">https://127.0.0.1:8000&quot;</a>)</li>
  <li>Go to https://localhost:8000/ in Chrome where the app is running</li>
  </ul>
  <h5>What I Expect to Happen</h5>
  <ul>
  <li>My Symfony web app to be running on https://localhost:8000/ when I visit the URL in my Chrome browser</li>
  </ul>
  <h5>What Actually Happens</h5>
  <ul>
  <li>I get &quot;This site can't be reached localhost refused to connect.&quot; in the Chrome browser</li>
  </ul>
  <h5>What I've Tried</h5>
  <ul>
  <li>This used to happen less frequently and I would give my laptop a restart, repeat the process above, and I could connect via https://localhost:8000/. However, it refuses to connect more regularly now (like 8/10 times I start up for the day)</li>
  <li>Connecting to <a href="https://127.0.0.1:8000" rel="nofollow noreferrer">https://127.0.0.1:8000</a> yields the same result.</li>
  <li>Connecting to the site using the internal WSL IP address, found using <code>hostname -I</code> and replacing localhost with this IP (still on port 8000). This is an adequate workaround to use my app, however I am unable to interact with my database via MySQL Workbench without having to set up a new connection, therefore a fix where I can use localhost would be very helpful!</li>
  <li>(Based off comments) Only ran <code>symfony serve -d</code> without starting apache and PHP services separately - still sometimes allows connections to localhost but sometimes doesn't work.</li>
  </ul>
  <h5>Conclusion</h5>
  <p>The behaviour is odd as it works sometimes but other times it doesn't when the exact same steps are carried out. I am unsure where else to look for answers and I can't seem to find anything online with this same problem. Please let me know if any config files, etc would be helpful. Thank you so much for your help! :)</p>