How to Host ASP.Net Core 6 application on Linux?
Microsoft claims that Asp.Net Core is much faster than dot Net 4.8 Application. Developers are moving on Asp.Net Core because these applications can be hosted on Linux which is a cheap hosting solution. Students are taking advantage of that. Windows hosting is expensive and requires much high ram and processor to run windows and their applications smoothly. Asp.Net core Linux hosting does not require much hardware to run Linux and Asp.net core applications. However, Linux servers are difficult to configure because they don’t have any kind of graphical interface. Configuring the Asp.net core can be hectic because you can’t host it like a Php application. You need to create a new service for your application to run on Linux. Configuring service is another task but when you do it once you will find it very easy to configure and run Asp.Net Core application on Linux. You need to follow the following steps to configure and run your Asp.Net core application.
Installing Asp.Net Core Packages.
First of all you need to install asp.net core packages to host Asp.net core application packages are available on Microsoft Asp.Net Core download page. Here
Select your Linux version and follow the steps carefully until you successfully install all the packages for your Asp.Net Core application.
How to Install Nginx on Ubuntu:-
After installing all the packages you need to install your favorite server on Linux it depends on your need which one you prefer for your application Apache or Nginx. We would like to suggest that you may try nginx because it's very lightweight and easy to configure and it's a better option for Asp.Net Core. You can install Nginx by following instructions at Microsoft Website from Here.
How to Configure Nginx on Ubuntu:-
After installing Nginx you need to configure Nginx. Use the following command to locate the site enable directory of Ubuntu.
“Cd /etc/nginx/”
“Ls” will show all the directories
“Cd sites-available”
Create a new config file by the following command.
“Sudo nano mysite.conf” you can name it whatever you want.
Copy and paste code below into the file.
server {
listen 80;
server_name example.com *.example.com;
location / {
proxy_pass http://127.0.0.1:5000; //Port Number should be the same you used in your project while debugging
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
After that you need to create a service to run your application on Nginx. To create service you need to follow the steps below:-
-
Go to “cd /etc/systemd/system”
-
Create new file with any name you want “sudo nano myweb.service”
-
Copy and paste code below to make changes for your directories and website name.
[Unit]
Description=Todaybins Core applications running on Ubuntu
[Service]
WorkingDirectory=/var/www/todaybins/
ExecStart=/usr/bin/dotnet /var/www/todaybins/Todaybins.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=myfirstapp-identifier
User=www-data
Environment=ASPNETCORE\_ENVIRONMENT=Development
Environment=DOTNET\_PRINT\_TELEMETRY\_MESSAGE=false
Environment=ASPNETCORE_URLS=http://localhost:7057 //This url is from your project startup.json file
[Install]
WantedBy=multi-user.target
Save your file with any name like “mywebsite.service”.
Sudo systemctl enable mywebsite.service
Sudo systemctl start mywebsite.service
Sudo systemctl status mywebsite.service
If your service status is active then you are good to go to run your website.
For any queries please comments
This is some sample content.