How To Deploy ASP.NET Core App/Web API On Oracle Linux Server Using Nginx

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.

  1. Install Nginx
  2. Install DotNet.sdk
  3. Code in .Net Core Web App / API
  4. Build and Publish
  5. Create and register service to host Dot Net Core Web App / API
  6. Configure reverse proxy for single service
  7. Configure reverse proxy for multiple services

Install Nginx Server

Ng­inx is a web server. It can­not be in­stalled from of­fi­cial repos­i­to­ries pro­vided by Or­a­cle, so we will reg­is­ter Ng­inx repos­i­tory.

sudo nano /etc/yum.repos.d/nginx.repo

Paste the fol­low­ing con­tent into the file:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Then you can in­stall Ng­inx from the repo:

sudo yum install nginx -y

And fi­nally start the ser­vice:

sudo systemctl enable nginx
sudo systemctl start nginx

Configure firewall

Now let’s al­low the op­er­at­ing sys­tem to ac­cept in­com­ing traf­fic.

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 1

Install 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 /opt
sudo mkdir dotnet

In dotnet folder, create 5.0 folder to place the sdk file.

cd /dotnet
sudo mkdir 5.0

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

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

Save it and run a command to execute this .bash_profile.

source /home/your_folder_name/.bash_profile

Now check the dotnet version using this command:

dotnet --version

You will see the installed dotnet version.

If any error message show then type below command:

export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1

Code in .Net Core App

In­stall the Mi­crosoft.Ex­ten­sions.Host­ing.Sys­temd NuGet pack­age. This in­te­grates our ap­pli­ca­tion with the dae­mon host­ing en­vi­ron­ment in Oracle Linux.

Add the Us­eSys­temd call to the Pro­gram.cs file.

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).UseSystemd().ConfigureWebHostDefaults(webBuilder => {
    webBuilder.UseStartup < Startup > ();
});

Add the Use­For­ward­ed­Headers 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 pub­lish tar­get.
  • Set Target Framework to net5.0 or your defined framework
  • Set De­ploy­ment Mode to Self-Con­tained.
  • Set Tar­get Run­time to linux-x64.

Deploy

Now open command prompt in your Linux Server and go to /srv directory and cre­ate a new folder where the app will be later uploaded.

cd /srv
sudo mkdir ProjectName
sudo chmod 777 /srv/ProjectName

Con­nect to the Linux ma­chine us­ing 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 per­mis­sion to linux user to that di­rec­tory.

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 cre­ate a ser­vice con­fig­u­ra­tion file which will run the App in Server.

sudo nano /etc/systemd/system/projectname.service

For the pur­pose of this ar­ti­cle the con­fig­u­ra­tion 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.target

Now we can start the ser­vice, check the sta­tus and test whether it re­turns data!

sudo systemctl enable projectname.service
sudo systemctl start projectname.service

We en­able the ser­vice so it will run au­to­mat­i­cally af­ter start of the op­er­at­ing sys­tem.

If any changes occur in projectname.service by using

sudo nano /etc/systemd/system/projectname.service

then reload it by using

systemctl daemon-reload

Configure reverse proxy for single service

Ng­inx acts as a proxy server in front of .NET Core pro­cess which is us­ing Kestrel server in­ter­nally. The main rea­son for it is that Ng­inx can han­dle large work­loads smoothly by us­ing a re­quest queue (and the .NET Core pro­cess does not have to bother with hold­ing out­put buffers for a long time).

sudo nano /etc/nginx/conf.d/default.conf

In location sec­tion, replace the fol­low­ing 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 ap­ply the con­fig­u­ra­tion.

sudo nginx -s reload

Configure reverse proxy for multiple services

Ng­inx acts as a proxy server in front of .NET Core pro­cess which is us­ing Kestrel server in­ter­nally. The main rea­son for it is that Ng­inx can han­dle large work­loads smoothly by us­ing a re­quest queue (and the .NET Core pro­cess does not have to bother with hold­ing out­put buffers for a long time).

sudo nano /etc/nginx/conf.d/default.conf

In 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 ap­ply the con­fig­u­ra­tion.

sudo nginx -s reload

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

For checking existing port in Nginx

semanage port -l | grep http_port_t

For different port, Configure firewall

firewall-cmd --add-port=81/tcp
firewall-cmd --add-port=81/tcp –permanent

Restart 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.service

Stop the Services

If you need to stop your App/API, then type this command:

sudo systemctl stop projectname.service

If you need to stop nginx server, then type this command:

sudo systemctl stop nginx