Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Deploy ASP.NET CORE Applications on Centos 8
Uthman Rahimi
Uthman Rahimi

Posted on

     

Deploy ASP.NET CORE Applications on Centos 8

A couple of weeks ago I needed to deploy anASP.NET Core application on acentos 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 useVirtualBox 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
Enter fullscreen modeExit fullscreen mode

To deploy theASP.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.0sudo dnf install aspnetcore-runtime-5.0
Enter fullscreen modeExit fullscreen mode

PackageManager forCentOs 8 isdnf andyum is forCentOs 7.

Install Nginx

I am usingNginx as a web server but you can chooseApache if you do not want to useNginx.

So use below command to installnginx

sudo dnf install nginx
Enter fullscreen modeExit fullscreen mode

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

sudo systemctl enable nginxsudo systemctm start nginx
Enter fullscreen modeExit fullscreen mode

This will enable and startNginx. 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
Enter fullscreen modeExit fullscreen mode
  • 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 meansNginx 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 configuringNginx to forward HTTP Requests to ourASP.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
Enter fullscreen modeExit fullscreen mode

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;}
Enter fullscreen modeExit fullscreen mode

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

If you open a browser and enterhttp:localhost you should see the default page ofNginx.

Create a new Service

Up until now we installed the required packages and configuredNginx, 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
Enter fullscreen modeExit fullscreen mode

And add following example to it, then save it byctrl+x:

[Unit]Description=Example .NET Web API App running on CentOs 8[Service]WorkingDirectory=/var/www/myappExecStart=/usr/bin/dotnet /var/www/myapp/myapp.dllRestart=always# Restart service after 10 seconds if the dotnet service crashes:RestartSec=10KillSignal=SIGINTSyslogIdentifier=dotnet-exampleUser=www-dataEnvironment=ASPNETCORE_ENVIRONMENT=ProductionEnvironment=DOTNET_PRINT_TELEMETRY_MESSAGE=false[Install]WantedBy=multi-user.target
Enter fullscreen modeExit fullscreen mode

And copy published Project tovar/www/myapp.

Note thatwww-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.servicesudo systemctl start myapp.service
Enter fullscreen modeExit fullscreen mode

After running it, make sure it's running by checking its statussudo systemctl satatus myapp.service , if you get a greenrunning result, it is working and you can access it by enteringhttp:localhost:5000.

Redirect Traffic to 5000

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

 "Urls": "http://0.0.0.0:5000",
Enter fullscreen modeExit fullscreen mode

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

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
demir profile image
Mehmet
  • Joined

hey thanks for informations.
When I apply this method for aspnet mvc applications, I get an error in static files (such as css, js, appsettings).

CollapseExpand
 
uthmanrahimi profile image
Uthman Rahimi
Software Engineer who does not use Resharper.
  • Location
    Tehran
  • Education
    MA of Computer Engineering
  • Joined

Which method you applied?
There is no code in your comment.

CollapseExpand
 
demir profile image
Mehmet
  • Joined

I tried your method. I am getting error on static files in mvc projects. Have you tried it as mvc project?
My project is an empty mvc project.

Thread Thread
 
uthmanrahimi profile image
Uthman Rahimi
Software Engineer who does not use Resharper.
  • Location
    Tehran
  • Education
    MA of Computer Engineering
  • Joined

Could you please share the error you are getting?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer who does not use Resharper.
  • Location
    Tehran
  • Education
    MA of Computer Engineering
  • Joined

More fromUthman Rahimi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp