How to deploy ASP.NET Core 2.1 applications on Ubuntu
We just finished our first ASP.NET Core project in Linux for one of our clients. Though we had worked on ASP.NET projects earlier, however all were developed on Windows for Windows servers. As we were gearing up to develop our next project in ASP.NET Core (few more lined up), this time, we wanted to explore .NET on Linux platform. We are great fan of Linux and having written tons of APIs in Node.JS for mobile apps; we wanted to bring .NET on Ubuntu.
When we started looking for deployment the web app on Ubuntu 16.0, we ran into multiple issues ad we could not find information at one place. Hence we thought of sharing this to whole tech community and use this link as their default reference document.
When you have ASP.NET Core application ready to be deployed, it is important to keep few things in mind. We are going to use NGINX as our reverse proxy to the ASP.NET core application running on port 5000.
Make you aspnetcore_environment set to production, if it is for production environment.
Having set the environment to “Production”, it is good time to publish the app into the release mode.
Let’s go to the application directory where our .csproj file is and publish the application in the release mode.
dotnet publish — configuration Release
Let’s setup hosting environment with NGINX & .NET Core 2.1 installations (and DB server setup, if any)
sudo apt-get install nginx
sudo service nginx start
Above command installs NGINX and starts on your Ubuntu 16.04 (or greater). Let’s check, if NGINX is running and is accessible.
If you see above screen, your NGINX is running successfully. Half of the battle fought.
Great, let’s install .NET Core 2.1 on the server. Let’s follow below steps in the same order as they have been provided below.
wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.1
If everything goes fine, we should see .NET Core 2.1 installed on our Ubuntu (16.04 in this case). Let’s check by dotnet — -version command and it should confirm .NET Core 2.1 installed.
That’s great! Let’s run ASP.NET Core application to be accessed by our users. Just fire a command:
dotnet run
Hit localhost or public ip address or your domain address. Uh. Ah. It still shows NGINX running page. What’s happening here?
Well, our ASP.NET core is running on port 5000 using Kestrel (Kestrel is a web server like IIS on Windows). Ok, so we need to configure NGINX to route our requests to port 5000 for all incoming requests.
Find NGINX default configuration file on your remote server and edit the below configurations:
location / {
proxy_pass http://localhost:5000;
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;
}
Alternatively, go to this resource to deep dive into it. Please keep in mind, NGINX config changes might complain about access issue. Just sudo chmod -R 777 to the file and you should be able to make changes saved. Now let’s check, if config changes are correct:
sudo nginx -t
And now let’s restart NGINX again.
sudo nginx -s reload
Hit localhost or IP address or domain address and boom. You see your asp.net core app loading. Sweet.
One more challenge though. The moment you come out of the remote server, you don’t see your .NET Core running any more. Why is that?
Well, dotnet run command runs .NET Core on the shell. It needs to be running in the background.
nohup dotnet <yourapp.dll> — urls “http://<your ip address>:<your port no>” > /dev/null 2>&1 &
nohup is a POSIX command to ignore the HUP signal (Wikipedia). In a nutshell it is just windows service sort of and this makes sure that your .NET Core app runs continuously in the background.
There are situations, where we want .NET Core application to runs in background after an Ubuntu machine restarts. We can write scripts to automate this process.
Please feel free to get to us in case of any queries, you might have. We would love to answer.