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 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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit 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 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

Enter fullscreen mode Exit 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 mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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",
Enter fullscreen mode Exit fullscreen mode

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

Discussion (4)

Collapse
demir profile image
Mehmet

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).

Collapse
uthmanrahimi profile image
Uthman Rahimi Author

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

Collapse
demir profile image
Mehmet

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 Author

Could you please share the error you are getting?