Score:0

Ubuntu Failed to bind to address http://127.0.0.1:5000

ua flag

Deploying an aspnetcore 6 web app to Ubuntu 20.04. Here is an article of what I am doing https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-6.0#publish-and-copy-over-the-app

Apache is now set up to forward requests made to http://localhost:80 to the ASP.NET Core app running at http://127.0.0.1:5000. It worked before but now its giving me this error.

 dotnet helloapp.dll
Unhandled exception. System.IO.IOException: Failed to bind to address http://127.0.0.1:5000: address already in use.
 ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use
 ---> System.Net.Sockets.SocketException (98): Address already in use

My service file looks something like
[Unit]
Description=Example .NET Web API App running on Ubuntu

[Service]
WorkingDirectory=/var/www/helloapp
ExecStart=/usr/local/bin/dotnet /var/www/helloapp/helloapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=apache
Environment=ASPNETCORE_ENVIRONMENT=Production 

[Install]
WantedBy=multi-user.target

and config like

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ServerName www.example.com
    ServerAlias *.example.com
    ErrorLog ${APACHE_LOG_DIR}helloapp-error.log
    CustomLog ${APACHE_LOG_DIR}helloapp-access.log common
</VirtualHost>
John Ronald avatar
ca flag
Check what is using the port with command "netstat -tulpn | grep 5000"
DavidJS avatar
ua flag
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 4587/dotnet tcp6 0 0 ::1:5000 :::* LISTEN 4587/dotnet
DavidJS avatar
ua flag
Would it not be the app specified in the configuration file? dotnet/4587. It seems like its configured to run but for some reason not being able to be viewed online.
John Ronald avatar
ca flag
Try to kill that process and start the app again.
DavidJS avatar
ua flag
Yes, I was able to kill the process and run dotnet webapp.dll. What I am trying to figure out now, is why my http://website.com is not working, its showing ERR_CONNECTION_REFUSED on chrome when. I thought my config/service file would have handled that.
DavidJS avatar
ua flag
After I killed the process and ran the app, I got info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[14] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0]
John Ronald avatar
ca flag
Are you trying that from localhost ?
DavidJS avatar
ua flag
No. I do my development on a Windows 11 Home machine, and I upload to Ubuntu 20.04 after I click on publish website.
John Ronald avatar
ca flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/134077/discussion-between-john-ronald-and-davidjs).
Score:0
nl flag

i solved the AddressInUseException (:5001 in my case) doing the steps listed here.

Let me know if the issue persists after that.

DavidJS avatar
ua flag
Thanks but it didn't help. I developed the aspnet on my windows machine, then upload the files to Ubuntu as published. I have added a configuration file to forward request to o http://localhost:80 to the ASP.NET Core app running at http://127.0.0.1:5000 in Ubuntu
Matthias Lenmher avatar
nl flag
Did you check if that assigned port is in use ?
DavidJS avatar
ua flag
I did its my dotnet service I created. I still cant view my url online, trying to figure that one out. I developed my aspnet 6 web mvc app on my dev machine, published it and upload it to Ubuntu. This error issue is not on my dev machine
Matthias Lenmher avatar
nl flag
you are binding the 5000 port twice, check how to redirect the request correctly.
DavidJS avatar
ua flag
Ok, i will look into it. Thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.