, ,

How to host a C# code on Nginx in CentOS

Posted by

C# is an OOP Language developed by Microsoft and is part of its .NET initiatives widely used for creating a wide range of applications for desktop applications, web applications, game development, and Mobile apps

C# can be deployed in a wide range of platforms which include Linux, CentOS, MacOS, and Windows. this is made possible by its ability to run on the .NET Framework/.NET Core. The tools and framework available for developing C# include Visual Studio, Xamarin, and .NET Core

host a C# code on Nginx in CentOS

to host a C# Project on Nginx hosted in CentOS here are the Steps to follow to achieve this.

  1. install the .NET Core Runtime on your CenOS using the command below;-
sudo yum install dotnet-runtime
  1. Install Nginx on your CentOS using the command
sudo yum install nginx
Nginx installation
  1. We Configure Nginx to work with .NET Core by creating a new configuration file for the application in the /etc/nginx/conf.d directory and adding below :
sudo vi configuration.conf
server {
    listen        80;
    server_name   example.com;
 
    location / {
        proxy_pass         http://127.0.0.1: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;
    }
}
  1. the next process is to Publish the C# code as a self-contained application:
dotnet publish --configuration Release --self-contained -r linux-x64
  1. once all is done now Copy the published files to your CentOS server:
cp -r /path/to/publish/folder user@server:/path/to/destination/folder
  1. When all files have been uploaded then we Start your application:
cd /path/to/destination/folder
./YourAppName
  1. Verify that your application is running by navigating to http://yourserverip or http://yourdomainname in a web browser.

Configure Nginx to use SSL/TLS

To configure Nginx to use SSL/TLS to secure our application we need to install the OpenSSL library .This is by running the command below;-

sudo yum install openssl
OpenSSL

Once OpenSSL installs what follows is to generate the certificate using the command below and proceed to update the Nginx configuration file to include in the SSL settings

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/cert.key -out /etc/nginx/cert.crt

including SSL configurations in the settings

server {
    listen        443 ssl;
    server_name   example.com;
 
    ssl_certificate      /etc/nginx/cert.crt;
    ssl_certificate_key  /etc/nginx/cert.key;
 
    location / {
        proxy_pass         http://127.0.0.1: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;
    }
}

How to Configure the firewall

When running the firewall on the personal server it is required to open ports for Nginx to allow incoming traffic, Nginx listens to ports 80 for HTTP and 443 for HTTPS by default hence we can open these ports using the below command

cssCopy codesudo firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo firewall-cmd --zone=public --add-port=443/tcp --permanent
sudo firewall-cmd --reload

Conclusion

Hosting C# application on CentOS is possible as explained in the above steps, Nginx works with web applications written in languages such as Python, and PHP and can as well be used with C# applications through the use of reverse proxies and FastCGI

Related Articles

NGInx

.

Leave a Reply

Your email address will not be published. Required fields are marked *