Introduction
In this article, we will learn about the deployment of Asp.Net Core Web Application / API on Oracle Linux Server using Nginx as reverse proxy server.
Following steps need to be executed for successful deployment.
- Install Nginx
- Install DotNet.sdk
- Code in .Net Core Web App / API
- Build and Publish
- Create and register service to host Dot Net Core Web App / API
- Configure reverse proxy for single service
- Configure reverse proxy for multiple services
Install Nginx Server
Nginx is a web server. It cannot be installed from official repositories provided by Oracle, so we will register Nginx repository.
sudo nano /etc/yum.repos.d/nginx.repoPaste the following content into the file:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1Then you can install Nginx from the repo:
sudo yum install nginx -yAnd finally start the service:
sudo systemctl enable nginx
sudo systemctl start nginxConfigure firewall
Now let’s allow the operating system to accept incoming traffic.
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
sudo setsebool -P httpd_can_network_connect 1Install DotNet.sdk on Linux Server
Go to https://dotnet.microsoft.com/download/dotnet/5.0 and download SDK 5.0.404 of Linux x64.
Go to /opt directory and create dotnet folder.
cd /optsudo mkdir dotnetIn dotnet folder, create 5.0 folder to place the sdk file.
cd /dotnetsudo mkdir 5.0Now go to the downloaded sdk file location and extract it to /opt/dotnet/5.0 location.
After it, go to /home/your_folder_name then create a .bash_profile file to set dotnet sdk.
sudo nano /home/your_folder_name/.bash_profileAnd configure it like this(add ':/opt/dotnet/5.0' in PATH):
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/opt/dotnet/5.0
export PATHSave it and run a command to execute this .bash_profile.
source /home/your_folder_name/.bash_profileNow check the dotnet version using this command:
dotnet --versionYou will see the installed dotnet version.
If any error message show then type below command:
export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1Code in .Net Core App
Install the Microsoft.Extensions.Hosting.Systemd NuGet package. This integrates our application with the daemon hosting environment in Oracle Linux.
Add the UseSystemd call to the Program.cs file.
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).UseSystemd().ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup < Startup > ();
});Add the UseForwardedHeaders call to the Startup.cs file.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
app.UseForwardedHeaders(new ForwardedHeadersOptions {
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
}Build and publish
- Choose a Folder as a publish target.
- Set Target Framework to net5.0 or your defined framework
- Set Deployment Mode to Self-Contained.
- Set Target Runtime to linux-x64.
Deploy
Now open command prompt in your Linux Server and go to /srv directory and create a new folder where the app will be later uploaded.
cd /srvsudo mkdir ProjectNamesudo chmod 777 /srv/ProjectNameConnect to the Linux machine using MobaXterm SFTP. Go to the project directory. Click right button and select “Upload to current folder”. Then select all files from the publish folder and upload it to the new publish directory /srv/ProjectNamein Linux Server.
Grant write permission to linux user to that directory.
find /srv/ProjectName -type d -exec chmod 777 {} \;find /srv/ProjectName -type f -exec chmod 777 {} \;Create and register service to host Dot Net Core Application/API
We can create a service configuration file which will run the App in Server.
sudo nano /etc/systemd/system/projectname.serviceFor the purpose of this article the configuration can look like this:
[Unit]
Description=ASP.NET Core App
[Service]
Type=notify
WorkingDirectory=/srv/ProjectName
ExecStart=/opt/dotnet/5.0/dotnet /srv/ProjectName/ProjectName.dll
Restart=always
RestartSec=10
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.targetNow we can start the service, check the status and test whether it returns data!
sudo systemctl enable projectname.service
sudo systemctl start projectname.serviceWe enable the service so it will run automatically after start of the operating system.
If any changes occur in projectname.service by using
sudo nano /etc/systemd/system/projectname.servicethen reload it by using
systemctl daemon-reloadConfigure reverse proxy for single service
Nginx acts as a proxy server in front of .NET Core process which is using Kestrel server internally. The main reason for it is that Nginx can handle large workloads smoothly by using a request queue (and the .NET Core process does not have to bother with holding output buffers for a long time).
sudo nano /etc/nginx/conf.d/default.confIn location section, replace the following line:
root /usr/share/nginx/html;
index index.html index.htm;with:
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass "http://localhost:defined_kestrel_port_in_program.cs_file";Save the file and apply the configuration.
sudo nginx -s reloadConfigure reverse proxy for multiple services
Nginx acts as a proxy server in front of .NET Core process which is using Kestrel server internally. The main reason for it is that Nginx can handle large workloads smoothly by using a request queue (and the .NET Core process does not have to bother with holding output buffers for a long time).
sudo nano /etc/nginx/conf.d/default.confIn default.conf file, create server for each service like:
server {
listen 80;
server_name localhost;
location / {
proxy_set_header Host $host;
proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;
proxy_set_header X - Forwarded - Proto $scheme;
proxy_pass "http://localhost: defined_kestrel_port_in_program.cs_file";
#root / usr / share / nginx / html;
#index index.html index.htm;
}
error_page 500 502 503 504 / 50 x.html;
location = /50x.html {
root / usr / share / nginx / html;
}
}
server {
listen 81;
server_name localhost;
location / {
proxy_pass "http://localhost: defined_kestrel_port_in_program.cs_file";
}
}Save the file and apply the configuration.
sudo nginx -s reloadNow open any browser and hit the server with IP and PORT number.
Congratulations! You have successfully deployed ASP.NET CORE App / Web API.
Additional command you may need to host Asp.Net Core Application / Web API
For adding new port in Nginx
semanage port -a -t http_port_t -p tcp 82For checking existing port in Nginx
semanage port -l | grep http_port_tFor different port, Configure firewall
firewall-cmd --add-port=81/tcp
firewall-cmd --add-port=81/tcp –permanentRestart and Check Status of Services
To restart/check status of your services, just execute these commands individually.
sudo systemctl restart nginx
sudo systemctl restart projectname.service
sudo systemctl status nginx
sudo systemctl status projectname.serviceStop the Services
If you need to stop your App/API, then type this command:
sudo systemctl stop projectname.serviceIf you need to stop nginx server, then type this command:
sudo systemctl stop nginx

Showne PrPosted Jun 14, 2022, 11:34 PM EDT
The way of explanation step by step is really great To Deploy ASP.NET Core App/Web API On Oracle Linux Server Using Nginx. Keep it up so that programmer can get some easy process.
Menashe UngarPosted May 12, 2022, 4:05 AM EDT
Thanks for this excellent tutorial. I am trying to add 443 sll support in the default.conf file and I keep receiving 404 Not Found. This is my default.conf file. server { server_name my_domain.name; listen 443 ssl; ssl_certificate /home/myapp/certificate/myapp.net_chain.crt; ssl_certificate_key /home/myapp/certificate/star.pk.pem; } If I check the certificate with an SSL Checker everything is fine. If I try to access the server via port 80, everything is fine too.
Billal HossainPosted Feb 10, 2022, 1:06 AM EST
Helpful content. Thank you very much for sharing such a good content.
Ali ImamPosted Feb 10, 2022, 12:37 AM EST
Very useful guideline for deploy ASP.NET Core App/Web API on Linux Server Using Nginx. Thank you for share with this community.