My developer life has been primarily focused on the Microsoft Platform. However, I have recently stepped into the world of cross-platform development by exploring the deployment of ASP.NET web applications on Ubuntu Server with NGINX. This transition allows me to break free from the limitations of a single platform and harness the power of .NET Core for platform-independent development. In this article, I will share my experiences and insights gained during this journey. Thank you for joining me!
When considering Ubuntu Server as the deployment platform, I had to select a web server suitable for hosting .NET Core web applications. The two popular open-source options available for Ubuntu are Apache and NGINX. Apache follows a process-driven architecture, while NGINX utilizes a non-blocking event-driven architecture. Although Microsoft supports both deployment methods, I opted to test my application with NGINX.
Regardless of the web server chosen, certain prerequisites must be met for setting up .NET on Linux. Before installing anything, I first checked if dotnet was already installed by running the following command:
Check .NET SDK and Runtimes
dotnet --list-sdks
dotnet --list-runtimes
Install .NET Runtimes
To clarify, my intention is not to develop any new .NET applications on the Ubuntu server. Therefore, I have made the decision to solely install the .NET runtimes. Specifically, I have chosen to install the ASP.NET runtimes version 7.0.
sudo apt-get update && \
sudo apt-get install -y aspnetcore-runtime-7.0
Now ASP.NET Core 7.0.5 installation in Ubuntu Server 23.04 is finished.
Next step is installing NGINX.
Install NGINX on Ubuntu Server 23.04
sudo apt-get install nginx
After finish NGINX installation, start the service and test the status.
sudo service nginx start
sudo service nginx status
NGINX server is started in Ubuntu Server 23.04. Check from browser with Ubuntu server address
http://spot-ubuntu/
Configuring NGINX Server for SignalR or Blazor
The next step involves configuring the NGINX server to support ASP.NET SignalR or Blazor server applications, as SignalR utilizes WebSockets.
To accomplish this, we need to make changes to two configuration files in Ubuntu Server 23.04: the “http” portion in “/etc/nginx/nginx.conf” and the “server” portion in “/etc/nginx/sites-available/default.”
sudo nano /etc/nginx/nginx.conf
And update below code snippet after starting of http section, and save.
http {
map $http_connection $connection_upgrade {
"~*Upgrade" $http_connection;
default keep-alive;
}
##
# Basic Settings
##
Second is opening default configuration file in text editor.
sudo nano /etc/nginx/sites-available/default
Update existing configuration as below in location / { … } section.
server {
listen 80 default_server;
listen [::]:80 default_server;
# Configure the SignalR Endpoint
location / {
# App server url
proxy_pass http://localhost:4561;
# Configuration for WebSockets
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache off;
# WebSockets were implemented after http/1.0
proxy_http_version 1.1;
# Configuration for ServerSentEvents
proxy_buffering off;
# Configuration for LongPolling or if your KeepAliveInterval is longer than 60 seconds
proxy_read_timeout 100s;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
After update http and server configuration in two NGINX configurations files, test your changes.
sudo nginx -t
Test result well be success.
Let NGINX server to reload with new changes.
sudo nginx -s reload
Create a directory for web app in NGINX server
Now we need to create a directory for ASP.NET application for NGINX server. Make a directory under /var/www/ and set read/write permission.
sudo mkdir /var/www/spot_ubuntu
sudo chmod -R 777 /var/www/spot_ubuntu
Copy ASP.NET Web APP deployment files to /var/www/spot_ubuntu with scp command via SSH from windows command.
scp -r D:\InfoWaddy\Spot.NetSDK\Spot.NETSDK.WebApp\bin\Release\net7.0\publish\* zinmin@spot-ubuntu:/var/www/spot_ubuntu/
Run your ASP.NET dll file
cd /var/www/spot_ubuntu/
dotnet Spot.NETSDK.WebApp.dll
Note: An important aspect to note is that if you do not change the location to /var/www/spot_ubuntu/ where the appsettings.json file is located, dotnet will start the application from the default port 5000. This is because dotnet cannot find the specific endpoint configuration file you have set up.
You can access the application by entering the appropriate URL in a web browser. It should now be up and running ASP.NET Core Blazor App on Ubuntu Server 23.04 with NGINX.
Create a service to run on start-up
sudo nano /etc/systemd/system/kestrel-spotnetsdk.service
[Unit]
Description=Spot.NET SDK Web App running on Ubuntu Server
[Service]
WorkingDirectory=/var/www/spot_ubuntu
ExecStart=/usr/bin/dotnet /var/www/spot_ubuntu/Spot.NETSDK.WebApp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=spot_ubuntu
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
sudo systemctl enable kestrel-spotnetsdk.service
sudo systemctl start kestrel-spotnetsdk.service
Congratulations! Your ASP.NET web application is now successfully running on Ubuntu Server. Even if the server restarts, the NGINX service will automatically restart as well, ensuring that your application remains accessible and ready for use. Enjoy the benefits of deploying your application on Ubuntu with NGINX, and continue to leverage the power of ASP.NET in a Linux environment.
In this journey, we explored deploying ASP.NET web applications on Ubuntu Server with NGINX, leveraging the platform-independent capabilities of .NET Core. By choosing NGINX as the web server and following the necessary configuration steps, we were able to host the application successfully. The combination of Ubuntu Server, NGINX, and .NET Core provides a robust environment for running ASP.NET applications and ensures seamless availability even after server restarts. Embrace the possibilities of cross-platform development and enjoy the flexibility and stability of this setup for your future projects.
Thank you for reading.
Ref:
- NGINX vs Apache — Choosing the Best Web Server in 2023 (hostinger.com)
- Host ASP.NET Core on Linux with Apache | Microsoft Learn
- Host ASP.NET Core on Linux with Nginx | Microsoft Learn
- Install .NET on Linux distributions — .NET | Microsoft Learn
- ASP.NET Core SignalR production hosting and scaling | Microsoft Learn
- Host ASP.NET Core on Linux with Nginx | Microsoft Learn