Ever wondered, how can you set up a cost-effective Virtual Private Server for .Net Core hosting?
In this blog, I am discussing how to set up your own VPS for .Net Core self-hosting.
Here are the steps:
- Set up an Ubuntu Server
- Install Asp.Net Core runtime
- Install Nginx
- Upload your published files in a folder on the server
- Configure nginx to receive and forward requests to the Kestrel Server
- Reload nginx config so that nginx reads the latest config
- Are you getting 502?
- Run the dll
- yay!
1. Setup an Ubuntu Server
I have set up an AWS Lightsail Server. You can follow this video to do the same.
2. Install Asp.Net Core runtime
Since I have Ubuntu20.x, the only commands that I ran to install dotnet core runtime are:
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
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
To verify dotnet runtime run "dotnet --info"
3. Install Nginx
I have followed this link to install nginx and the only commands I ran were:
sudo apt-get update
sudo apt-get install nginx
4. Upload published files to the server
Ideally, I recommend using Azure Pipelines which should own the responsibility of building the package and publishing the files on the server. For this demo, you can follow video in step 1 to upload the files using FTP.
5. Configure nginx to receive and forward requests
- Run `sudo nano /etc/nginx/sites-available/default`
- Modify the contents of as below (Replace the text in <> with your own values):
server {
listen 80;
# listen [::]:80;
#
server_name ;
#
root /var/www/;
# index index.html;
#
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;
}
}
6. Reload nginx config so that nginx reads the latest config
Run `sudo nginx -s reload`
This command makes nginx read the latest configuration
7. Are you getting 502 Bad Gateway?
At this time, if you are getting bad gateway, then you should look in /var/log/nginx/error.log and you would get to know that you have not run the dll yet.
8. Run your dotnet application
Run the command in the directory where your files are available `dotnet <your dll>`
9. Check your website again. It should work fine.
If your application did not work fine, please comment the issue that you are facing. I'll try my best to help you out. You have hosted a .Net Core application on your own VPS.
Next Steps?
We'll talk in the next blogs about running the dotnet application as a service and lot more. So, stay connected. 😊