Introduction
.Net Core is a free and open-source framework from Microsoft. The beauty of .Net Core is that it is cross-platform and you can run it on Windows, Linux, or MAC. So you can develop your web API on your favorite OS like Windows or MAC and host it on a Linux machine.
In this tutorial, I will show how to deploy your .Net Core web API on a clean Ubuntu virtual machine with Nginx.
Setting up Linux VM
You will need SSH access to the virtual machine and root permissions. First of all, let’s update the VM and install the necessary minimum software.
sudo apt update
The sudo apt-get update command is used to download package information from all configured sources.
sudo apt -y install vim bash-completion wget
This command will install vim, bash-completion and wget packages.
- vim – is a text editor
- bash-completion is a collection of command line command completions for the Bash shell, collection of helper functions to assist in creating new completions, and set of facilities for loading completions automatically on demand, as well as installing them.
- wget – package for retrieving files using HTTP, HTTPS, FTP and FTPS, the most widely used Internet protocols.
sudo apt -y upgrade
upgrade is used to install the newest versions of all packages currently installed on the system
sudo reboot
After upgrade command is run we need to reboot a VM to make sure upgrade is completed.
Download and Install runtime
To run a .Net Core applications on any OS we need to install .Net Core Runtime. The current latest version of .Net Core is 3.1 and you can install it with the following command:
sudo apt-get update;
sudo apt-get install -y apt-transport-https &&
sudo apt-get update &&
sudo apt-get install -y aspnetcore-runtime-3.1
Detail instructions and troubleshooting guide is here.
Install Nginx
NginX, is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.
You can install it with a following command:
sudo apt-get install nginx
or the full instruction is located on an official web site.
Start Nginx with a command:
sudo service nginx start
Configure Nginx
To configure Nginx as a reverse proxy to forward requests to your ASP.NET Core app, modify /etc/nginx/sites-available/default. Open it in a text editor, and replace the contents with the following:
If you want to get more details on what is reverse proxy and why do we need to use it read this article.
nano /etc/nginx/sites-available/default
Add the following code:
server {
listen 80;
server_name YOUR_DOMAIN;
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;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Save the file and verify syntaxis with the following command:
sudo nginx -t
If everything is OK rerun the Ngnix to apply new settings:
sudo nginx -s reload
Configure .Net Core web api
Firstly you will need to build a web api in release mode on your machine. To do this run the command in root location of your application:
dotnet publish --configuration Release
Create a folder in Ubuntu /var/www/your_app_name
Copy all files to that folder. You can use FTP (if it is available on your VM) or SSH File Transfer Protocol (I am using CyberDuck for that).
In terminal navigate to your application folder and run the application. This is just to verify that it works:
dotnet <app_assembly.dll>
If app started without any issues press CTRL + C to stop it.
We want our application to be run from services. In this way we can instruct Ubuntu to restart our application automatically if it crashes or after VM restart.
Create a service file:
sudo nano /etc/systemd/system/kestrel-<your_app>.service
Paste the follwing code:
[Unit]
Description=<App Name>
[Service]
WorkingDirectory=/var/www/<app_folder>
ExecStart=/usr/bin/dotnet /var/www/<app_folder>/<app_name>.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
Save the file, than enable and start the service:
sudo systemctl enable kestrel-<app_name>.service
sudo systemctl start kestrel-<app_name>.service
You can check service status with the following command:
sudo systemctl status kestrel-alfalab.service
Thank you. This was really helpful
One question. How to deploy multiple APIs on the same server.
1. Upload several .Net Core app in separate folders
2. Start each api service under different port (e.g. by default .Net Core starts app under 5000 port, if it is busy .Net Core chooses any free port)
3. Create a service file for each .Net Core app
Maybe I need to write a complete post about it 🙂
I can’t deploy .NetCore in Linux with Apache.
hi, every step i follow already…but when go to the website see, it come out 502 bad gateway?what the issue?
Hi Lee, it is hard to say what is going on just from an error code, but usually it means that Nginx was not able to get a response from your .Net Core application. Please check if the application is up and running.
User=www-data
user must be a ssh-user ? or I should leave as is
I noticed one thing if I make changes in code and upload the updated dlls, changes do not relfect.
Would you tell me what I can do to fix it?
Thanks for the a new challenge you have unveiled in your short article. One thing I would really like to reply to is that FSBO interactions are built with time. By releasing yourself to the owners the first weekend break their FSBO will be announced, ahead of the masses start out calling on Mon, you develop a good association. By mailing them resources, educational products, free accounts, and forms, you become a great ally. By subtracting a personal affinity for them along with their scenario, you develop a solid interconnection that, in many cases, pays off once the owners decide to go with a representative they know along with trust — preferably you actually.
Follow the way for restarting the service, however when I run sudo systemctl status kestrel-.service it will show (code=exited,status=1/Failure)