Search code examples
nginxwindows-subsystem-for-linuxdevelopment-environment

Can't start nginx in WSL


I was using a dev environment with Windows and WSL with docker and nginx installed directly in my WSL machine to handle the proxy reverse, but now when I'm trying to start nginx, I'm getting the error "98: Unknown error" and using the command "sudo nginx -t", it shows me "bind() to 0.0.0.0:80 failed" and "0.0.0.0:443 failed", what can I do to fix this?


Solution

  • problem

    ubuntu wsl comes with apache web server pre-installed, its using the default web port (80), so when you try to start nginx with default setting (also on port 80) it will have bind error

    but of-course, there are multiple reason that port 80 is already used, in which you need to check what app that is using it

    how to check

    option 1:

    • open the ubuntu (linux) terminal
    • type sudo systemctl status apache2
    • if you see some message that tells its running, then it does using port 80, if not, then its something else

    option 2:

    • open a browser and type localhost in the address bar
    • if its showing default apache install page, then its truly the culprit

    solution

    this solution is for when apache2 is the culprit, for any other app that might use port 80, you need to find how to disable it yourself

    • stop it using sudo systemctl stop apache2
    • you need to disable it too, so that next time you restart your pc, it wont auto-start, type sudo systemctl disable apache2
    • after this, you can try starting the nginx again: sudo systemctl start nginx
    • and make it auto-start: sudo systemctl enable nginx

    of-course, you can just change the port used by nginx like here use other port on nginx

    if you decide to change nginx port, please note that any host listening on port that is used by other app will make nginx fail to start, so if you have more than 1 host on nginx config, you need to update them too