How to deploy Asp.net Core app to Linux Droplet

Steps;

Connect to server

ssh root@<Your Server IP>

.Net sdk install on server,

wget https://packages.microsoft.com/config/ubuntu/18.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 dotnet-sdk-3.1

Let’s create the path;

sudo mkdir /var/www/
sudo mkdir /var/www/
sudo chown -R dries:www-data /var/www/

Let’s clone our project here

git clone <your repo git address.git>

Build and publish project

dotnet build
dotnet publish

Install nginx

sudo nano /etc/apt/sources.list

deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

sudo apt-get update
sudo apt-get install nginx

sudo service nginx start

Welcome Nginx message when enter url with Your server ip

Add an SSL certificate with Certbot

sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python-certbot-nginx
sudo certbot --nginx

Forward requests to our app

sudo nano /etc/nginx/sites-available/sites.com
server {
    listen        80;
    server_name   ;
    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;
    }
}

linked,

sudo ln -sf /etc/nginx/sites-available/sites.com /etc/nginx/sites-enabled/
sudo nano /etc/nginx/nginx.conf
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
http{
    ...
    include /etc/nginx/sites-available/sites.com;
}
sudo service nginx restart

Create service

sudo nano /etc/systemd/system/dotnet-project-name.service
[Unit]
Description=My application service

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

[Install]
WantedBy=multi-user.target

In my case,

[Unit]
Description=My application service

[Service]
WorkingDirectory=/var/www/oguzhanabali-dev-demo-web
ExecStart=/usr/bin/dotnet /var/www/driesdeboosere-dev-demo-web/Oguzhanabali.Dev.Demo.Web.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=oguzhanabali-dev-demo-web
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Permission access to folders,

 

sudo chown -R root:root /var/www/
sudo setfacl -R -d -m u:root:rwx,g:root:rwx,o::r /var/www/
sudo service oguzhanabali-dev-demo-web start
sudo systemctl | grep oguzhanabali-dev-demo-web

That’s it! Now your web server will keep running your ASP.NET Core app, even if you exit the server.

Terminal Environment on macos (Unix commands,Git,Fish,Autocomplete)

1- Bkz: https://github.com/jorgebucaran/awsm.fish

2-Fish-shell install https://github.com/fish-shell/fish-shell

3-Oh-my-fish install https://github.com/oh-my-fish/oh-my-fish

4-Fisher install https://github.com/jorgebucaran/fisher

omf install bobthefish (themeinstall) 

https://github.com/oh-my-fish/oh-my-fish/blob/master/docs/Themes.md

5-Z install -(folder manager)

6-Fish manuel config, insert alias.

Create custom config file for Docker and Dotnet cli run on ubuntu terminal

nano ~/.config/fish/config.fish in
alias gg="git push"
alias gm="git commit -m"
alias gma="git commit -ma"
alias gp="git pull"
alias gpr="git pull --rabase"
alias gcb="git checkout -b"
alias gs="git status"
alias gl="git log --n 5"

set fish_greeting Go, go go..!

Then active fish config:

source ~/.config/fish/config.fish

Customize vscode theme, material-theme active.

fisher install franciscolourenco/done

fisher install rodrigobdz/fish-apple-touchbar

Finished.

Hint! AdminLte – how to fix active sidebar

$(document).ready(function() {
  /** add active class and stay opened when selected */
  var url = window.location;

  // for sidebar menu entirely but not cover treeview
  $('ul.nav-sidebar a').filter(function() {
      return this.href == url;
  }).addClass('active');

  // for treeview
  $('ul.nav-treeview a').filter(function() {
      return this.href == url;
  }).parentsUntil(".nav-sidebar > .nav-treeview").addClass('menu-open').prev('a').addClass('active');
})

ScreenShot Download from Webbrowser with C#

This method;
It takes a screenshot of the page you are on.

private void DownloadScreenShotImage()
        {
            int width, height;
            width = webBrowser1.ClientRectangle.Width;
            height = webBrowser1.ClientRectangle.Height;
            using (Bitmap image = new Bitmap(width, height))
            {
                using (Graphics graphics = Graphics.FromImage(image))
                {
                    Point p, upperLeftSource, upperLeftDestination;
                    p = new Point(0, 0);
                    upperLeftSource = webBrowser1.PointToScreen(p);
                    upperLeftDestination = new Point(0, 0);
                    Size blockRegionSize = webBrowser1.ClientRectangle.Size;
                    graphics.CopyFromScreen(upperLeftSource, upperLeftDestination, blockRegionSize);
                }
                image.Save("d:\\Test.bmp");
            }
        }

Hell yeah!

Asp.net core https redirect ERR_TOO_MANY_REDIRECTS (on IIS)

Hi,

Your project after you deploy on IIS; when you want to do http to https redirect;
“ERR_TOO_MANY_REDIRECTS” brush up against.

For Solution add in startup.cs;

public void ConfigureServices(IServiceCollection services)
{
  services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders = ForwardedHeaders.XForwardedProto;
            });
  services.AddHttpsRedirection(opts => {
                opts.RedirectStatusCode = StatusCodes.Status301MovedPermanently;
                opts.HttpsPort = 443;
            });
}

and,

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
..
 app.UseHttpsRedirection();
..
}

Asp.Net Core Deploying ERR_TOO_MANY_REDIRECTS ERROR ON IIS

Hi everyone,

You made a asp.net core. Everyting is okey in local. But on IIS deploying there is a problem.

Solution:

 <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />

to

 <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />

Terminal Environment on windows (Unix comamands,Git,Fish,Autocomplete)

Bkz: https://github.com/jorgebucaran/awsm.fish

1- Install Windows terminal and Ubuntu 20.xx version download on windows store

2-Fish-shell install https://github.com/fish-shell/fish-shell

3-Oh-my-fish install https://github.com/oh-my-fish/oh-my-fish

4-Fisher install https://github.com/jorgebucaran/fisher

omf install bobthefish (themeinstall) 

https://github.com/oh-my-fish/oh-my-fish/blob/master/docs/Themes.md

5-Z install -(folder manager)

6-Fish manuel config, insert alias.

Create custom config file for Docker and Dotnet cli run on ubuntu terminal

nano ~/.config/fish/config.fish in
alias docker="docker.exe"
alias docker-compose="docker-compose.exe"
alias dotnet="dotnet.exe"
alias python="python.exe"
alias virtualenv="virtualenv.exe"
alias pip="pip.exe"
alias explorer="explorer.exe"

alias gg="git push"
alias gm="git commit -m"
alias gma="git commit -ma"
alias gp="git pull"
alias gpr="git pull --rabase"
alias gcb="git checkout -b"
alias gs="git status"
alias gl="git log --n 5"

set fish_greeting Go, go go..!

Then active fish config:

source ~/.config/fish/config.fish

Customize vscode theme, material-theme active.

How can set default open shell?

chsh -s /usr/bin/fish
If error; run "which fish"

open the default terminal for vscode below run:

wsl --list --all

wsl --setdefault Ubuntu-20.04

Finished.