How to host asp.net core application into Linux Environment?

Zakir Hossain
2 min readJul 11, 2024

--

The asp.net core language is becoming more and more popular, thus many developers are beginning to adopt it. However, because the majority of asp.net core developers are not accustomed to working in a Linux environment, they are having trouble hosting asp.net applications in Linux environments. Therefore today i will share a simple method to host asp.net application in Linux environment.

Architecture:

dot.net application hosting method

Prerequisite:

  • nginx reverse proxy service
  • kastrel web server
  • systemd script

Step 01: Installed dot.net required packages

# dnf install aspnetcore-runtime-6.0 dotnet-sdk-6.0 nginx -y

Step 02: Host dot.net application under nginx

Create require directory

# mkdir -p /usr/share/nginx/dotnet-app
Upload your application into your desire location: /usr/share/nginx/dotnet-app/

After Upload the content set proper permission
# chown nginx. -R /usr/share/nginx/dotnet-app

Note: Its good to compile dot.net core application as a singe file.

Step 03: Install kestrel web service to run the dot.net application

# cd /etc/systemd/system
# vim kestrel-dotnet-app.service

[Unit]
Description=Example .NET Web API App running on Linux

[Service]
User=nginx
Environment=ASPNETCORE_ENVIRONMENT=Development
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

WorkingDirectory=/usr/share/nginx/dotnet-app #dotnet application home directory
ExecStart=/usr/share/nginx/dotnet-app/service #dll file name of the service

#Restart=always
#RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-service

[Install]
WantedBy=multi-user.target

Step 03: Configure nginx as a proxy

# cd /etc/nginx/conf.d

# vim dotnet-app.conf

server {
listen 80;
#listen 443 ssl;

server_name dotnet-service.example.com;
access_log /var/log/nginx/dotnet-service_access.log;
error_log /var/log/nginx/dotnet-service_error.log;

#ssl_certificate "/etc/nginx/.ssl/bundle.crt";
#ssl_certificate_key "/etc/nginx/.ssl/example.com.key";

location / {
#proxy_pass http://localhost:5000/swagger/;
proxy_pass http://localhost:5000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Step 04: Start all the service and check.

# systemctl start nginx kestrel-dotnet-app.service

# systemctl enable nginx kestrel-dotnet-app.service

# systemctl status nginx kestrel-dotnet-app.service

Now check your application using any browser.

--

--