Deploying a .Net Core web app to a Ubuntu Linux VPS

Deploying a .Net Core web app to a Ubuntu Linux VPS

This tutorial assumes a fresh Ubuntu 20.04 (at the time of writing) VPS on GCP, e2-micro (2 vCPUs, 1 GB memory) (Free tier), and also assumes working from a windows machine, hence the below prerequisites.

Prerequisites

  • PuTTY (https://www.putty.org/) (For connecting to remote VPS over SSH)
  • WinSCP (https://winscp.net/eng/download.php) (For transferring files to remote VPS over SSH)
  • VS 2019
  • DotNetCore 3.1 SDK

Configuring the VPS: (Assume using sudo whenever needed)

  • Connect to VPS via SSH using PuTTY. (Use PuTTYgen to generate public and private keys to enable and secure connection, check [this](https://cloud.google.com/compute/docs/instances/connecting-advanced#windows-putty) article, and connect using the private key you saved, many articles illustrate how to do so with PuTTY)
  • Once connected, create a user that will be used to run your aspnetcore app and give them sudo permissions: (Assume this username is going to be vima91)
$ adduser vima91

$ usermod -aG sudo vima91
  • (I like to use vim for editing text files, but be free to use whatever text editing software you like) I will be installing vim at this point. Also, this is an awesome tutorial to get you started using vim:


$ sudo apt-get update
$ sudo apt-get install vim
  • To ensure there would be not problems next time you connect via ssh with the new user, let's edit the /etc/ssh/ssdh_config file:
$ sudo vim /etc/ssh/ssdh_config

And add these two lines:

PermitRootLogin no
AllowUsers vima91

Now save and restart ssh service:

$ sudo systemctl restart sshd
  • Now switch to this user, as we are going to use this user from here on:
$ su - vima91
  • Now we are ready to install .net core SDK. At the time of writing, the latest LTS is 3.1, let's first install package sources:
$ wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
  • And then install:
$ sudo apt-get install apt-transport-https
$ sudo apt-get update
$ sudo apt-get install dotnet-sdk-3.1
  • Then after it is finished, make sure it is installed by typing
$ dotnet --info

and checking version to be 3.1, should display something like this:

No alt text provided for this image
  • To create the folder that will have our app on the server, and give our user full ownership: (Assuming our app name is Mvc.Test)
$ sudo mkdir -p /var/www/Mvc.Test
$ sudo chown -R vima91:www-data /var/www/Mvc.Test
  • Now it is time to publish the app to the server.

Assuming you have the project on your windows machine, open and ready to launch, make sure you create a new public profile with the following settings:

- Type: FileFolder publish

- Configuration: Release

- Target Framework: netcoreapp3.1

- Deployment mode: Framework-dependent

- Target runtime: linux-x64

  • Now you are ready to publish, hit publish.
  • After publish is done, check the folder from which you need to copy the files:
<repo path>\bin\Release\netcoreapp3.1\publish
  • Open WinSCP and connect to your server using the same ways you used to connect via PuTTY, using the private key you generated and saved.
  • Copy the files from the above path to where we need our app to be on the server:
<repo path>\bin\Release\netcoreapp3.1\publish => /var/www/Mvc.Test
  • After copying is done, now we need to setup our reverse proxy, nginx.

First, let's install it:

$ sudo apt update
$ sudo apt install nginx
  • And let's run it:
$ sudo service nginx start

...and then make sure the nginx service is running:

$ systemctl status nginx

...and check that it is running. To double check, now if you navigate to your server's url/ip address, you should see the default nginx page, which would display something like this: 

No alt text provided for this image
  • Now the fun part, let's configure nginx, open the config file:
$ sudo vim /etc/nginx/sites-available/default

You can clear all the file contents and paste this:

server {
    listen        80;
    server_name   localhost;
    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;
  } 

}

This means:

- server_name is localhost, meaning this will work only with the ip address of the server, with no domain name.

- listen on 80 means that the server will listen on port 80 (<your ip address to the server>:80) and reverse it to http://localhost:5000 (proxy_pass).

  • Now restart nginx:
$ sudo systemctl restart nginx

Now try to run the app with:

$ dotnet /var/www/Mvc.Test/Mvc.Test.dll

...should display something like this:

No alt text provided for this image

...and check if it is being served on your server ip address, should be served on port 80.

  • Now we need to run it as a service, so stop the current process (Ctrl + C) and type in:
$ sudo vim /etc/systemd/system/Mvc.Test.service

...and paste in this text:

[Unit]
Description=My application service


[Service]
WorkingDirectory=/var/www/Mvc.Test
ExecStart=/usr/bin/dotnet /var/www/Mvc.Test/Mvc.Test.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=Mvc.Test
User=vima91
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

...notice the user is set to `vima91` as it was decided this is the user that we will use to run the app, and this is also the user with full permissions on the app folder.

  • Now save the file and start the service that was just created:
$ sudo service Mvc.Test start

...notice the service name is `Mvc.Test`, this is taken from the file name `Mvc.Test.service`.

  • Now again try to navigate to your server ip address, your website should be up and running.

* Finally, to secure your website and install a certificate, visit Certbot (https://certbot.eff.org/lets-encrypt/ubuntufocal-nginx) to install a certificate, just follow the instructions and you are ready!

If you have a domain name, your nginx configration will slightly differ, and same with installing the certificate, but the articles and links provided will help you get up and running

Credits:

A combination of articles were used to achieve this working state and to compile this mini tutorial, please find these articles here:

and here:

---------------------------------------------------------------------------------------------------------------

THANK YOU!! Now .NET Core page is running in my Raspberry server with Ubuntu - thanks for you!! 😊

Can I host more than one asp.net core website? If yes, how this will be done?

Like
Reply

To view or add a comment, sign in

More articles by Evram Ehab

  • Working with SSDT and GIT: Best practices

    This article is intended to help people new to SSDT and relatively new to git, comprised of two separate yet somewhat…

Explore content categories