Fixing port issue in WSL 2 after PHP upgrade
Martijn | Developer
You may encounter a specific issue when upgrading PHP in WSL.
In this case, we will look at error 'ERR_CONNECTION_REFUSED'.
Tip
Want to upgrade PHP in WSL 2? Make sure to check out this article.
Step 1 - Verify if nginx is listening
You first need to verify if nginx is listening on port 80 and 443 (SSL).
sudo lsof -i :80
sudo lsof -i :443
Step 2 - Stop and disable Apache
If you see 'apache2' instead of 'nginx' under 'COMMAND', you need to make some changes. Stop and disable Apache by running the following commands.
sudo systemctl stop apache2
sudo systemctl disable apache2
If you want, you can remove Apache by running the following commands.
sudo apt remove apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.3
sudo apt purge apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.3
sudo apt autoremove
Step 3 - Check and test nginx configuration
Check the nginx configuration to see if the ports are correctly configured.
sudo nano /etc/nginx/sites-available/default
Test the configuration by running the following command.
sudo nginx -t
Step 4 - Restart nginx and verify it is running
Restart nginx by running the following command.
sudo systemctl restart nginx
Verify if nginx is running by running the following command.
sudo systemctl status nginx
Step 5 - Verify if nginx is listening
Finally, you should repeat step 1 and check if nginx is listening on the correct ports.
sudo lsof -i :80
sudo lsof -i :443
You should now see 'nginx' under 'COMMAND' and everything should run normal again.
After this, you can run valet restart
to ensure there are no open issues.