
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
To deploy theASP.NET Core
application we need to install some packages. These packages are required and we have to install them.
Prerequisites:
- - SDK
- - 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
PackageManager for
CentOs 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
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
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
- 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.
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
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 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
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
And copy published Project tovar/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.servicesudo systemctl start myapp.service
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",
in this way, we just need to enter Server IP Address in browser,Nginx
will redirect to port 5000.
Top comments(4)

- LocationTehran
- EducationMA of Computer Engineering
- Joined
Which method you applied?
There is no code in your comment.

- LocationTehran
- EducationMA of Computer Engineering
- Joined
Could you please share the error you are getting?
For further actions, you may consider blocking this person and/orreporting abuse