Deploy ASP.NET CORE Applications on Centos 8

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 …


This content originally appeared on DEV Community and was authored by uthman

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

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://127.0.0.1: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.


This content originally appeared on DEV Community and was authored by uthman


Print Share Comment Cite Upload Translate Updates
APA

uthman | Sciencx (2021-12-26T09:09:30+00:00) Deploy ASP.NET CORE Applications on Centos 8. Retrieved from https://www.scien.cx/2021/12/26/deploy-asp-net-core-applications-on-centos-8/

MLA
" » Deploy ASP.NET CORE Applications on Centos 8." uthman | Sciencx - Sunday December 26, 2021, https://www.scien.cx/2021/12/26/deploy-asp-net-core-applications-on-centos-8/
HARVARD
uthman | Sciencx Sunday December 26, 2021 » Deploy ASP.NET CORE Applications on Centos 8., viewed ,<https://www.scien.cx/2021/12/26/deploy-asp-net-core-applications-on-centos-8/>
VANCOUVER
uthman | Sciencx - » Deploy ASP.NET CORE Applications on Centos 8. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/26/deploy-asp-net-core-applications-on-centos-8/
CHICAGO
" » Deploy ASP.NET CORE Applications on Centos 8." uthman | Sciencx - Accessed . https://www.scien.cx/2021/12/26/deploy-asp-net-core-applications-on-centos-8/
IEEE
" » Deploy ASP.NET CORE Applications on Centos 8." uthman | Sciencx [Online]. Available: https://www.scien.cx/2021/12/26/deploy-asp-net-core-applications-on-centos-8/. [Accessed: ]
rf:citation
» Deploy ASP.NET CORE Applications on Centos 8 | uthman | Sciencx | https://www.scien.cx/2021/12/26/deploy-asp-net-core-applications-on-centos-8/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.