# Ubuntu (version 16.04 and later) .Net Core WebApi Deployment

Also you can see this document on: <https://docs.tugaycivgin.com/>

## Introduction:

1. Install Microsoft .net and .net core packages
2. Install Nginx
3. Build and run project

#### Register Microsoft key and feed:

This only needs to be done once per machine. Open a terminal and run the following commands.

```bash
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
```

### Install the .NET Core SDK <a href="#install-the-net-core-sdk" id="install-the-net-core-sdk"></a>

Update the products available for installation, then install the .NET Core SDK. In your terminal, run the following commands.

```bash
sudo add-apt-repository universe
sudo apt-get update
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-3.1
```

### Install the ASP.NET Core runtime <a href="#install-the-aspnet-core-runtime" id="install-the-aspnet-core-runtime"></a>

Update the products available for installation, then install the ASP.NET Core runtime. In your terminal, run the following commands.

```bash
sudo add-apt-repository universe
sudo apt-get update
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install aspnetcore-runtime-3.1
```

### Install the .NET Core runtime <a href="#install-the-net-core-runtime" id="install-the-net-core-runtime"></a>

Update the products available for installation, then install the .NET Core runtime. In your terminal, run the following commands.

```bash
sudo add-apt-repository universe
sudo apt-get update
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-runtime-3.1
```

### Troubleshoot the package manager <a href="#troubleshoot-the-package-manager" id="troubleshoot-the-package-manager"></a>

If you receive an error message similar to **Unable to locate package {the .NET Core package}**, run the following commands.

```bash
sudo dpkg --purge packages-microsoft-prod && sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install {the .NET Core package}
```

If that doesn't work, you can run a manual install with the following commands.

```bash
sudo apt-get install -y gpg
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o microsoft.asc.gpg
sudo mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/
wget -q https://packages.microsoft.com/config/ubuntu/18.04/prod.list
sudo mv prod.list /etc/apt/sources.list.d/microsoft-prod.list
sudo chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg
sudo chown root:root /etc/apt/sources.list.d/microsoft-prod.list
sudo apt-get install -y apt-transport-https
sudo apt-get update
sudo apt-get install {the .NET Core package}
```

#### Install Nginx

**NGINX** is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability.

**Install Nginx**

```bash
sudo apt-get install nginx
```

**Start Nginx Server**

```bash
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:

```bash
server {
    listen        80;
    server_name   example.com *.example.com;
    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;
    }
}
```

#### \*GVim - The Text Editor

Default file is read-only. You have to use a text editor for this. Just go to the directory and use these methods:

![GVim - Default File](https://694159573-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzHZ6Sv6hYKOa7k7f_P%2F-LzHwlX13XubRHzhdys0%2F-LzI4D43c1eYAUSPsi_J%2F86cb49b7-b023-4e26-bd09-821f379d0b57.jpg?alt=media\&token=f973bc6c-1f92-4a97-a086-01c3495bb4f1)

```bash
cd /etc/nginx/sites-available/
sudo gvim default
```

**Server Status: Check the server status if running**

```bash
sudo service nginx status
```

You can see that the server is active

![Nginx Status](https://694159573-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzHZ6Sv6hYKOa7k7f_P%2F-LzHwlX13XubRHzhdys0%2F-LzI2cTt9oFYTrSchP7b%2F300646e6-c388-4e2f-9411-7fa6e53b2ad9.jpg?alt=media\&token=63ba5402-1b90-4a88-88a7-e079be6977bf)

#### Run Project

Go to directory of your project and use these commands below for the " .*dll* " file. For example of mine:

```bash
cd /Desktop/erste
dotnet erste.dll
```

#### On Browser

The server is setup to forward requests made to `http://<serveraddress>:80` on to the ASP.NET Core app running on Kestrel at `http://127.0.0.1:5000 or http://localhost:5000`.However, Nginx isn't set up to manage the Kestrel process. *systemd* can be used to create a service file to start and monitor the underlying web app. *systemd* is an init system that provides many powerful features for starting, stopping, and managing processes.

![When you let the browser it will open http://localhost:5001](https://694159573-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzHZ6Sv6hYKOa7k7f_P%2F-LzHwlX13XubRHzhdys0%2F-LzI90UXRhKzPo2bXReh%2Feaecfdbd-9d12-4bb4-9d00-127a7f7624ed.jpg?alt=media\&token=06ce3569-4fbd-4d27-9655-384db7537552)

#### Probable Errors :

#### "E: Could not get lock /var/lib/dpkg/lock" Error in Ubuntu

Here a similar error you may see:

```bash
E: Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not get lock /var/lib/dpkg/lock – open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
```

The first thing you should do is to check if some other program could be running system update or installing a program.

If you are using command line, check if an application like Software Center, Software Updater, Synaptic, Gdebi is running any update/installation. If that’s the case, please wait for the program to finish the running process.

If there is no such application running, please check all the open terminal windows and see if you are running an update or installing a program. If yes, wait for it to finish.

If none of the above is happening, check which other process is running the apt command (package manager for handling software). Use this command:

```bash
ps aux | grep -i apt
```

This will show you the id of the process running apt or apt-get. In the example below, the process id is 7343. You can ignore the last line containing ‘grep –color=auto’.

![](https://694159573-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzHZ6Sv6hYKOa7k7f_P%2F-LzLAmNr25xBBkCY7Mm1%2F-LzLCZdPRbJ8xIzN0XFU%2Fps%20aux.png?alt=media\&token=9abcfef6-a5a1-4381-a739-c030bb469ebc)

You can use the process id to terminate it by sending the [SIGTERM signal](https://linuxhandbook.com/sigterm-vs-sigkill/). Replace the \<process\_id> with the number you got in the output of previous command.

```bash
sudo kill <process_id>
```

#### “dpkg: error: dpkg frontend is locked by another process”

If you see the error “dpkg frontend is locked by another process” while running the steps in method 2, you need to one additional step.

First, find out the id of the process that is holding the lock file.

```bash
sudo lsof /var/lib/dpkg/lock-frontend
```

The above command will give you the details of the processes using the lock files. Use the process ID to kill this program:

```bash
sudo kill -9 PID
```

Now you can remove the lock and reconfigure dpkg:

```bash
sudo rm /var/lib/dpkg/lock-frontend
sudo dpkg --configure -a
```

#### Uncomplicated Firewall (ufw)

**Note: Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)**

```bash
$ sudo ufw allow 'Nginx Full'
```

We enable the `ufw`:

```bash
$ sudo ufw enable
```

We can verify the change with:

```bash
$ sudo ufw status
```

```bash
output
  Status: active

  To                         Action      From
  --                         ------      ----
  Nginx Full                 ALLOW       Anywhere
  Nginx Full (v6)            ALLOW       Anywhere (v6)
```

Now we can open [http://server\_domain\_or\_IP](http://server_domain_or_ip) in our browser, and we should be able to see the default Nginx landing page after config the default file in /etc/nginx/sites-available/ directory. You can see it by scrolling up on the page.

#### Extra:

<http://devnot.com/2019/linux-centos-uzerinde-nginx-ile-net-core-uygulamalarini-calistirmak/>

### Thank You List

* <https://itsfoss.com/could-not-get-lock-error/>
* <https://medium.com/faun/ubuntu-servers-and-asp-net-core-project-deployment-using-nginx-d9a3a1f6ac82>
* <https://medium.com/@sketchmycloud/how-to-deploy-asp-net-core-2-1-applications-on-ubuntu-474062f8db73>
* <https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1&tabs=aspnetcore2x>
* <https://linoxide.com/linux-how-to/configure-nginx-server-blocks-ubuntu/>
