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.repo
Paste the following content into the file:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
Then you can install Nginx from the repo:
sudo yum install nginx -y
And finally start the service:
sudo systemctl enable nginx
sudo systemctl start nginx
Configure 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 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
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 /srv
sudo mkdir ProjectName
sudo chmod 777 /srv/ProjectName
Connect 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.service
For 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.target
Now we can start the service, check the status and test whether it returns data!
sudo systemctl enable projectname.service
sudo systemctl start projectname.service
We 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.service
then reload it by using
systemctl daemon-reload
Configure 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.conf
In 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 reload
Configure 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.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 apply the configuration.
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