By Yuvaraj Shanmugam

Hosting ASP.NET core application in Ubuntu Linux with Apache

The key feature introduced in .NET Core framework is platform independence, which means apart from Windows we can host ASP.NET Core application in Linux and Mac operating system.

But hosting is little bit tricky in hosting Linux or Mac. When we host in IIS in Windows operating, there is not much configuration needed to host the application. In this post I am going to explain how to host and ASP.NET Core in Ubuntu Linux with Apache server.

Install Dependencies

Ubuntu does not come with .NET Core framework. We first need to download and install required dependencies before installing .NET Core framework. Copy and paste the following commands to install dependencies

wget -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

Install ASP.NET Core runtime

Now we can download and install ASP.Net core with below commands. This will download and install ASP.NET core and .NET Core runtimes.

sudo apt-get update
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install aspnetcore-runtime-3.1
sudo apt-get install dotnet-runtime-3.1

Install Apache server

After we installed all the required packages, we need to install Apache server. Apache server will serve as a reverse proxy which handles the traffic from the internet and passes the request to the Kestrel server.

sudo apt-get install apache2
sudo a2enmod proxy proxy_http proxy_html proxy_wstunnel
sudo a2enmod rewrite

Setup Apache as reverse proxy

Now we need to setup Apache server as reverse proxy. Open a new file by by using the below command.

sudo nano /etc/apache2/conf-enabled/netcore.conf

Add the following the config

    <VirtualHost *:80>  
       ServerName www.DOMAIN.COM  
       ProxyPreserveHost On  
       ProxyPass /  
       ProxyPassReverse /  
       RewriteEngine on  
       RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]  
       RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]  
       RewriteRule /(.*) ws://$1 [P]  
       ErrorLog /var/log/apache2/netcore-error.log  
       CustomLog /var/log/apache2/netcore-access.log common  

In the above config the tag VirtualHost *:80 indicates the web site is hosted in port 80. For server name you need to provide the domain url. In case you are using locally, you can provide localhost. For ProxyPass and ProxyPassReverse we are referring to Kestrel server running locally in its default port 5000.

Restart Apache

We need to restart Apache in order to apply the configuration. Use the below command.

sudo service apache2 restart
sudo apachectl configtest

Now we have to publish web application and move the files to /var/{YourAppFolderName}/

Create a Service to run the application

We need to have service that runs the .NET Core Application. Create a service file with the below command. You can give a name that you prefer for the service. Here I have named myService.service.

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

Copy and paste the below configuration into the file

    Description=ASP .NET Web Application  
    ExecStart=/usr/bin/dotnet /var/netcore/Application.dll  

In the above config replace Application.dll to the dll of your application you want to run.

Start the service

Now you can enable and start the service with the below commands

sudo systemctl enable myService.service
sudo systemctl start myService.service

I hope you found this post useful. Please leave you comments below.

Leave a Reply

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