1. Overview

ASP.Net is a popular web application framework developed by Microsoft for building web applications and services. Traditionally, it has been used on Windows-based servers**.** With the increasing popularity of Linux-based servers, it has become necessary to run ASP.Net applications on Linux servers.

One of the main advantages of using a Linux server instead of Windows is containerization. Although Windows containers are easy to install and production-ready, Linux ones are even easier and much lighter on resources.

In this tutorial, we’ll discuss how to run ASP.Net on a Linux-based server.

2. Requirements to Run ASP.Net on a Linux Server

Before we start, here are some key requirements that must be met in order to support running ASP.Net on a Linux server:

  • Linux server distro – we must make sure we’re running a verified Linux server distribution. There are many versions, but some common ones are CentOS, Ubuntu, Debian, etc. For this tutorial, we’re using Ubuntu Server
  • A .Net runtime: there are two main options:
  • Web Server – we also need to install a web server on the Linux server. We can choose Apache or Nginx as our web server. In this article, we’ll be using Apache and FastCGI as our web servers
  • If the application requires database access, its .Net client packages are required. We’ll show how to install one (or any other dotnet package required)

3. .Net Runtime for Linux

In order to run .Net on Linux, we must choose the runtime we’ll use. We can use Microsoft .Net or a platform known as Mono.

Microsoft has made .Net available for Linux for some time, however, currently, it only supports 6.0 and 7.0 versions. They can run most modern .Net applications without modification, even pre-built binaries built on Windows, provided we install all dependencies.

On the other hand, Mono is an open-source implementation of the .Net framework that runs on various platforms, including Linux, macOS, and Windows. It enables developers to build and run .Net applications on non-Microsoft platforms.

Mono includes a compiler, runtime, and a set of libraries that are compatible with the .Net framework. This makes it possible to run ASP.Net applications on Linux-based servers using the Mono runtime.

4. Running Microsoft ASP.Net on a Linux-Based Server

Now that we’ve understood the prerequisites, let’s look at some of the steps we can use to be able to run ASP.Net on Linux.

We may download Mono and the .Net core from their respective websites. If we go that way, we can choose the newer or a specific version. However, this might give us more hassle whenever we need to upgrade.

Accordingly, in this tutorial, we shall try to use the distribution’s standard packages whenever possible. Thus, to install the first prerequisites on Ubuntu, we’ll use:

$ sudo apt update & sudo apt install dotnet7 apache2

For .Net 6.0, replace the dotnet7 to dotnet6.

This will install the .Net command line tool dotnet. This will allow us to create a new project. We can use Visual Studio Code or the command line like this:

$ mkdir my_project & cd my_project & dotnet new webapp

Now that we have a bare-bones .Net web application we can add the packages that we shall use, such as the Npgsql Postgress .Net client:

$ dotnet add package Npgsql --version 7.0.6

Once we’re done developing our application, we can publish it. To do so, we must package the application into a format that can be deployed to a web server with the command:

$ dotnet publish -c Release -o publish

This command creates a publish folder containing the published application.

We then configure Apache to serve the ASP.Net application by creating a new Apache configuration file named /etc/apache/sites-available/my_project.conf:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/my/public_html

    ProxyPass / http://localhost:5000/
    ProxyPassReverse / http://localhost:5000/
</VirtualHost>

This configuration file tells Apache to serve the application a virtual domain named example.com (to test create it on your DNS server or add on the client’s hosts file) on port 80 and proxy the requests to the ASP.Net application running on port 5000.

And, to finish, apache’s configuration enables the configuration and the required modules:

$ sudo a2enmod proxy proxy_http; sudo a2ensite my_project; systemctl reload apache2

The final step is to start the ASP.Net application. We navigate to the publish folder and start the application on port 5000:

$ dotnet myapp.dll

As long as the client can reach the server using the virtual domain on the conf file, it can see the applications’ front page:

5. Using Mono as a Runtime for ASP.Net

Alternatively, for legacy ASP.Net, for instance, we need to install Mono runtime on the server to be able to run ASP.Net applications on a Linux-based server.,

First, we must ensure that any dependencies or third-party libraries used in our application are compatible with Mono. We can use a tool known as the Mono Migration Analyzer(MoMA), to help us determine whether our application is compatible with Mono.

Second, we need to configure the webserver to use the Mono runtime. We can do this using the mod_mono module for Apache or the FastCGI module for Nginx. These modules enable the web server to forward requests to the Mono runtime, which then handles the execution of the ASP.Net application.

5.1. Configuring Apache With mod_mono

The mod_mono module enables Apache to host ASP.Net applications on Linux-based servers.

To configure Apache with mod_mono, we start by installing mod_mono and the Mono runtime with root permissions:

$ sudo apt-get install mono-complete libapache2-mod-mono

The command above helps us install the mono runtime and the necessary dependencies.

Next, we configure Apache to use mod_mono:

$ sudo a2enmod mod_mono

This command also requires elevated privileges and enables us to set up Apache to work with Mono.

For the changes to take effect, we need to restart Apache:

$ sudo systemctl restart apache2

Next, let’s deploy the ASP.Net application. Let’s say we call our application “testApp“:

$ cp <path-to-application>/testApp /var/www/html

This copies our ASP.Net application to the /var/www/html directory.

Lastly, let’s test the ASP.Net application by navigating to *http://localhost/**testApp* in a web browser to load the application.

5.2. Configuring Nginx With FastCGI

The FastCGI module enables Nginx to host ASP.Net applications on Linux-based servers.

To configure Nginx with FastCGI, we start by installing Nginx and the Mono runtime:

$ sudo apt-get install nginx mono-fastcgi-server4

The command above installs Nginx and FastCGI mono server, which is an ASP.Net runtime host.

Then, we configure Nginx to use FastCGI by adding the following lines to our Nginx configuration file /etc/nginx/sites-available/default:

location /testApp {
fastcgi_pass unix:/var/run/fastcgi-mono-server4/testApp.socket;
include /etc/nginx/fastcgi_params;
}

With the lines above, we set up the configuration parameters for Nginx and FastCGI.

Next, let’s restart Nginx for the changes to take effect:

$ sudo systemctl restart nginx

The next step is to deploy our ASP.Net application:

$ cp <path-to-application>/testApp /var/www/html

The command above copies the ASP.Net application to the /var/www/html directory.

Lastly, let’s test the ASP.Net application by navigating to *http://localhost/**testApp* in a web browser to load the application.

5.3. Installing NuGet Packages using Mono

Mono can run .Net applications built with the .Net SDK. For instance, 5.3. Installing Nuget Packages Using Monowe can download nuget.exe from the Microsoft .Net github page and run it directly using this to install Postgress .Net driver:

$ cd my_project; mono <path>/nuget.exe install Npgsql

However, the best way is to stick to our distribution preferred package manager:

$ sudo apt install nuget

Then we can use it to install packages:

$ cd my_project; nuget install Npgsql

6. Conclusion

In this article, we learned how to run ASP.Net on a Linux-based server. We saw that it’s possible to run ASP.Net on Linux by installing the .Net Core SDK and configuring Apache to serve the application.

In addition, we also looked at how we can use an open-source platform called Mono that supports most capabilities with *.*Net applications. Lastly, it’s important to make sure that the application libraries are supported by Mono before transitioning to Linux.