top of page

Learning Center

  • Writer's pictureElegant Software Solutions

.NET Web Apps Served on a Linux Server

Updated: May 29, 2020


If you’re a .NET developer, you’re probably pretty familiar with IIS. But some of us are also big fans of Linux and all the flexibility that it offers, and it doesn’t hurt that using Linux can cut your hosting costs almost in half. Linux isn’t just for PHP and MySql anymore; you can host full .NET MVC applications on your Linux server using your choice of server software.

The two main HTTP servers for Linux are Apache and nginx. Between the two, they serve about half of the websites around the world. I’m going to show you how you can host your .NET applications on them.

MonoDevelop

First I’ll cover how to add the latest MonoDevelop to Linux. MonoDevelop is the generic version of Xamarin Studio, similar to Visual Studio but much less polished. I’m running Linux Mint 18.2, which is based on Ubuntu 16.04, so this should be very similar if you are using any Linux distribution based on Debian. You do not have to have MonoDevelop installed just to host a .NET application, but I’ll cover the steps to install the latest version anyway.

You can just open the Software Manager and search for MonoDevelop and install it directly from there, but you will not get the most recent version that way. They recently changed the way MonoDevelop will be distributed and the binary packages will no longer be made available for direct installation. Instead, they have switched to using FlatPak for distribution. Flatpak is basically a virtualization layer that puts the application in a sandboxed environment. To get the most recent version of MonoDevelop, you must first install FlatPak:

sudo add-apt-repository ppa:alexlarsson/flatpack sudo apt-get update sudo apt-get install flatpak

Now that you have FlatPak installed, you can use it to install the most recent version of MonoDevelop:

flatpack install --user --from https://download.mono-project.com/repo/monodevelop.flatpackref

And to run MonoDevelop, you use:

flatpack run com.xamarin.MonoDevelop

Flatpak also adds the application to your menu under “Programming” for those that prefer to use the GUI instead of the command line.

Apache2

Now let’s talk about Apache. Apache web server has been around since the mid 1990’s, it’s mature, robust, stable, and super configurable. Apache has all sorts of add-ons that are ready to handle any kind of content, including .NET. The basic install is simple and straight forward; you can just use the Software Manager to search for Apache2 and install it from there. You will also want to install the mono library named “libapache2-mod-mono” so your server can handle .NET applications. These are both available in the Software Manager, or you can install them via command line:

sudo apt-get install apache2 libapache2-mod-mono

Nginx

Nginx (pronounced “engine-X”) is growing in popularity at a record pace and recently surpassed Apache. It’s not as old as Apache, but it is lightweight, super-fast, efficient, and capable of handling thousands of requests per second. Nginx is designed for serving static web pages and it performs that task very well. For any other kind of file, it passes control to the appropriate interpreter which you will need to configure. This is a bit more complicated than the Apache instructions, but if you have a lot of static content or you expect heavy traffic it is worth the trouble.

Like MonoDevelop, nginx may already be included in your distribution repository so you might be able to install it via the Software Manager. But if you want the most recent version, you will need to add the official repository and install it from there.

sudo add-apt-repository ppa:nginx/stable sudo apt-get update sudo apt-get install nginx

Check the version just to make sure it is installed.

nginx -v

At this point we can open a web browser and browse to “localhost” and we should see an nginx welcome page which verifies it is up and running. Before we can serve up any .NET code, we will need to add an interpreter like we did with Apache. There are several to choose from, and I will show you one of the simplest to get up and running:

sudo apt-get install mono-fastcgi-server4

Then we’ll need to start the fastcgi server:

fastcgi-mono-server4 /applications=/:/var/www/html/ /socket=tcp:127.0.0.1:9000

This command assumes that the html root is in the default location of /var/www/html and that all of your web content is .NET. If you have a mix of static HTML web sites and .NET applications on your server, you will need to modify the fastcgi command so that it only applies to the appropriate hostname.

We also need to edit some configuration files to tell nginx where to send those aspx files to be processed. The fastcgi interpreter is listening on port 9000 if you followed my example above, so open /etc/nginx/sites-available/default and add these lines to the “location /” section:

fastcgi_index Default.aspx; fastcgi_pass 127.0.0.1:9000; include /etc/nginx/fastcgi_params;

Then edit the /etc/nginx/fastcgi_params file and add these lines:

fastcgi_param PATH_INFO “”; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Be sure to restart your nginx server for those changes to take effect:

sudo service nginx restart

Deploying .NET applications

Deploying your .NET application from MonoDevelop requires a little more involvement than what you might be used to in Visual Studio because there is no “publish” button. When you are ready to deploy your .NET web application, you will need to build it in Release mode then manually copy the appropriate files (*.aspx, *.config, etc) to your web directory and don’t forget to also copy the ./bin folder! With a little bit of searching you can find a few scripts that other Linux users have written to automate the process of deploying .NET applications.

This has been a quick and dirty introduction to getting a Linux server set up to serve .NET applications. There are a lot of options and a lot of additional configurations and customizations that you can add to your server depending on your needs. Microsoft is embracing the open source movement, so dive in and start experimenting!

Next time I’ll cover how to port an existing Visual Studio solution to a MonoDevelop solution!

Happy Coding!

Elegant Software Solutions

488 views0 comments
bottom of page