Deploy ASP.NET CORE Applications on Centos 8
Tags: aspnetcore, linux, centos, netcore
Published: 2021-12-26 09:09:30

A couple of weeks ago I needed to deploy an `ASP.NET Core` application on a `centos 8` machine and that was the first time I wanted to use Linux because I had no experience working with Linux, it took me a whole day to finish this task.
In this article, I am going to share whatever you need to know about deploying the asp.net core application on a Linux machine.

First of all, if you are using Windows or Mac OS and you want to have a Linux OS you can use **VirtualBox** which is free and open-source to run a Linux OS as a gust on your Windows.




In order to install a package, you need to use its command which is like this:

```
sudo dnf install PackageName

```

To deploy the `ASP.NET Core` application we need to install some packages. These packages are required and we have to install them.


**Prerequisites**:

1. - SDK
2. - AspNetCoreRuntime

Open a terminal and run the following command to install them :

```
sudo dnf install dotnet-sdk-5.0
sudo dnf install aspnetcore-runtime-5.0

```

> PackageManager for `CentOs 8` is `dnf` and `yum` is for `CentOs 7`.

## Install Nginx

I am using `Nginx` as a web server but you can choose `Apache` if you do not want to use `Nginx`.


So use below command to install `nginx`

```
sudo dnf install nginx
```



After installation is finished, you need to enable and start `Nginx`. To enable and  run  `Nginx` (or any other services) you can use the below command :

```
sudo systemctl enable nginx

sudo systemctm start nginx
```

This will enable and start `Nginx`. But if you want to make sure that it is running, you can check its status by using the below command to see its status.


```
sudo systemctl status nginx
```
* bear in mind that whenever you want to check the status of a service you can use the above command and I think it's one of the most used commands at the beginning.

If you get a result like below, it means `Nginx` is working and it's ready to use.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9worl6fncrta96pnevvt.png)

Good job, we have installed all the things that are required to deploy our application, next we have to create a new service to run our application.

## Configure Nginx
The next step is configuring `Nginx`  to forward HTTP Requests to our `ASP.NET Core` application, to do this we should modify its default configuration which is located in `/etc/nginx/nginx.conf`
run following command to open it and modify it like below:

```
sudo nano /etc/nginx/nginx.conf

```
this will open the file and now replace its content with the following :

```
location / {
    proxy_pass http://0.0.0.0:5000;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

}
```

To verify if the change we applied is fine and there is no mistake in our `syntax`, run `sudo nginx -t`, if the test is successful then we need to reload `nginx`: `sudo nginx -s reload`.

If you open a browser and enter `http:localhost` you should see the default page of `Nginx`.


## Create a new Service

Up until now we installed the required packages and configured `Nginx`, now we should create a new service to run our application.

To create a new Service File, use the following command :

```
sudo nano /etc/systemd/system/myapp.service

```
And add following example to it, then save it by `ctrl+x`:

```
[Unit]
Description=Example .NET Web API App running on CentOs 8

[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=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
```
And copy published Project to `var/www/myapp`.


> Note that `www-data` user must exist, otherwise your service could not be run. 


If you have done all the things above, now we should enable and start our  service to run the application:

```
sudo systemctl enable myapp.service
sudo systemctl start myapp.service

```

After running it, make sure it's running by checking its status `sudo systemctl satatus myapp.service` , if you get a green `running` result, it is working and you can access it by entering `http:localhost:5000`.

## Redirect Traffic to 5000

If you want to access your project on `port 80` and also access to it from other computers, you will need to add below line in your `appSetting.json` file:

```
 "Urls": "http://0.0.0.0:5000",
```

in this way, we just need to enter Server IP Address in browser, `Nginx` will redirect to port 5000.