PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 12, 2022

[FIXED] Localhost refused to connect on WSL2 when accessed via https://localhost:8000/ but works when using internal WSL IP adress

 March 12, 2022     apache, localhost, symfony, windows, wsl-2     No comments   

Issue

What I'm Trying to Achieve

To access localhost from my local machine during the development of a Symfony web app.

My Environment
  • WSL2 running on Windows 10
  • Linux, Apache2, MySQL, PHP-7.4 stack (with Xdebug3 intalled)
  • Debian 10
  • Symfony 5.4 (although not sure on if relevant to this problem)
Steps I've Taken
  • Set up WSL2 according to this Microsoft WSL2 tutorial
  • Set up LAMP stack according to this Digital Ocean tutorial
  • Set up Symfony according to this Symfony tutorial
  • 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
#!/bin/sh
REMOTEIP=`cat /etc/resolv.conf | grep nameserver | sed 's/nameserver\s//'` 
sed -i -E "s/client_host=[0-9\.]+/client_host=$REMOTEIP/g" /etc/php/7.4/mods-available/xdebug.ini
service php7.4-fpm start
service apache2 start
service mysql start  
  • Run my Symfony project on the development server using symfony serve -d (Symfony then tells me "The Web server is using PHP FPM 7.4.23 https://127.0.0.1:8000")
  • Go to https://localhost:8000/ in Chrome where the app is running
What I Expect to Happen
  • My Symfony web app to be running on https://localhost:8000/ when I visit the URL in my Chrome browser
What Actually Happens
  • I get "This site can't be reached localhost refused to connect." in the Chrome browser
What I've Tried
  • 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)
  • Connecting to https://127.0.0.1:8000 yields the same result.
  • Connecting to the site using the internal WSL IP address, found using hostname -I 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!
  • (Based off comments) Only ran symfony serve -d without starting apache and PHP services separately - still sometimes allows connections to localhost but sometimes doesn't work.
Conclusion

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! :)


Solution

When it's working normally, as you are clearly aware, the "localhost forwarding" feature of WSL2 means that you can access services running inside WSL2 using the "localhost" address of the Windows host.

Sometimes, however, that feature breaks down. This is known to happen when you either:

  • Hibernate
  • Have the Windows "Fast Startup" feature enabled (and it is the default). Fast Startup is a pseudo-hibernation which triggers the same problem.

Typically the best solution is to disable Hibernation and Fast Startup. However, if you do need these features, you can reset the WSL localhost feature by:

  • Exiting any WSL instances
  • Issuing wsl --shutdown
  • Restarting your instance

It's my experience that localhost forwarding will work after that.



Answered By - NotTheDr01ds
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing