Host ASP.NET Core Website/Application on Ubuntu with Apache Webserver

ASP.NET Core is the latest web framework from Microsoft that supports development with C# and the .NET Framework. By using the .NET Core framework with ASP.NET Core, you can develop and publish your applications on non-Windows platforms like MacOS or Linux.

Before getting into the detail of how to set up such a website, let’s take a step back to go over the detail overview of the configuration. When a web application is created using the ASP .NET Core framework, it would inherently be served using Kestrel, the web server provided along with the framework. But if the hosting machine has already had a web server of its own or if it had been decided previously to use another web server -like our example- then we would need to turn that server into a proxy server.

Register the Microsoft Product key as trusted

To download install run this command in terminal.

wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb

Install .Net Core runtime

In this step install .NET Core Runtime allows you to run apps that were made with .NET Core.

sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y aspnetcore-runtime-3.1

Publishing Application

In this step, we are going to publish an application to the folder and copy that folder to VM.

We are going publish the application to the local folder we are going to choose Folder option.

After publishing, we need to copy files to Linux Virtual Machine for doing that we are going to use WinSCP tool.

Copying Application to Linux VM

In this step, we are going to copy that folder to Linux Virtual Machine for doing that we are going to use WinSCP.

URL to Download WinSCP: – https://winscp.net/eng/index.php

Install Apache

sudo apt install apache2

Next, we have installed Apache now we can check Apache web server status is it running.

sudo systemctl status apache2

To test in the browser, just access from Public IP. It will show you default Ubuntu 18.04 Apache web page.

Enable the required apache modules

sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod headers
sudo a2enmod ssl

After Enabling modules, Apache server needs to restart.

Restart Apache

sudo service apache2 restart

Creating folder in the www folder with name of application/website and copy all application files

sudo mkdir -p /var/www/myapp/publish/

Upload files to this new folder

Create a Virtual Host Configuration File

Apache Virtual Host configuration are located in /etc/apache2/sites-available

I am going to name Virtual Host Configuration file as myapp.conf many people give the domain name to the Configuration file for easy to recognize.

sudo nano /etc/apache2/sites-available/myapp.conf

Add below content to my-application.conf file

<VirtualHost *:80>  
   ServerName www.example.com  
   ProxyPreserveHost On  
   ProxyPass / http://localhost:5000/ 
   ProxyPassReverse / http://localhost:5000/  
   RewriteEngine on  
   RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]  
   RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]  
   RewriteRule /(.*) ws://127.0.0.1:5000/$1 [P]  
   ErrorLog ${APACHE_LOG_DIR}/error-myapp.com.log  
   CustomLog ${APACHE_LOG_DIR}/access-myapp.com.log combined  
</VirtualHost>

Disable the default site defined in 000-default.conf

sudo a2dissite 000-default.conf

Activate the myapp.conf

sudo a2ensite myapp.conf

Check Syntax of the Default Configuration file

sudo apachectl configtest

Restart Apache2 to make to take new changes

sudo systemctl restart apache2

Creating service

Creating a systemd Service file with name myappservicefile.service

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

Paste the following contents into the service file.

WorkingDirectory= defines on which directory the service will be launched.

This my application publish folder /var/www/publishapplication/publish

ExecStart= allows you to specify any command that you’d like to run when this service is started.

/usr/bin/dotnet /var/www/publishapplication/publish/myapp.dll

[Unit]
Description=Running ASP.NET Core on Ubuntu 18.04 Webserver APACHE

[Service]
WorkingDirectory=/var/www/myapp/publish
ExecStart=/usr/bin/dotnet /var/www/myapp/publish/myapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target

Enable the service

sudo systemctl enable myappservicefile.service

Output