Saturday, 26 June 2021

Deploy .net core app to Ubuntu

Step 1 : Create Azure VM 

Ubuntu Server 20.04 LTS - Gen1

Standard B1s (1 vcpu, 1 GiB memory)

0.0104 USD/hr

Step 2 : Register Microsoft key and repository feed

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb Jump -O packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb

Step 3 : Install Dot NET Core

-------.net sdk 3.1----------
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-3.1

-------.net sdk 5----------
sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-5.0

Step 4 : Install Nginx Web Server
sudo apt-get update
sudo apt-get install nginx

#Enable Nginx
sudo systemctl enable nginx

#Start Nginx
sudo systemctl start nginx

#check service status
sudo systemctl status nginx

#check service log
journalctl -u nginx.service

Step 5 : Deploying ASP.NET Core App

Create directory under nginx 
sudo mkdir /var/www/myapp

sudo chmod 777 /var/www/myapp

Step 6 : Clone git repository

git clone https://github.com/nageshajab/myapp.git

git clone will copy files into your home directory/ username
cd /home/nageshajab/myapp

Step 7 : Publish your code to server directory

run dotnet restore to get nuget libraries from server
dotnet restore

go to web directory to publish ur code to server directory
cd myApp.web
dotnet publish -c release --output /var/www/myapp

Step 8 : Configure a new website in nginx

Open nginx config file
sudo nano /etc/nginx/sites-available/default

add this block to opened file

server {
    listen 80;
    root /var/www/myapp;
    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 $http_host;
  proxy_cache_bypass $http_upgrade;
  }
}


Save file & exit
  • Save the modified default file press (Ctrl + O) and hit the enter
  • To exit the nano editor press (Ctrl + x) it will ask you to exit write Y and hit the enter on your keyboard.

We have replaced content of the default server block file on Nginx so we need to reload changes that we have made before we running the following command.

sudo nginx -s reload

Step 9 : Create ubuntu unit service to host .net core app


Create a service definition and put below code in it. For creating/ stopping / starting/ edition/ enable/ disable services refer - How To Use Systemctl to Manage Systemd Services and Units | DigitalOcean

[Unit]
Description=Asp.NET Web App now is running on Ubuntu

[Service]
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/dotnet /var/www/myapp/myApp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=myapponlinux-log
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target


Step  9 : Copy database backup file to server



No comments:

Post a Comment

Search This Blog

Creating your first "Alexa" Skill

Index What is Alexa What is Alexa Skill? Why is it required when Alexa already equipped with voice assistant? Dev...