How to host an Asp.NET core 3.1 application on Linux Ubuntu 20.04 with Apache as reverse proxy

Host an Asp.Net Core Website in Linux without Docker

Preliminary steps: Update the machine

In this step, I just update the machine to have the last packages and updates

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo reboot

Installing the necessary components

Reverse proxy schema
Reverse proxy schema
sudo wget -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
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
sudo apt-get install -y apache2
sudo apt-get install -y nano
sudo a2enmod ssl headers rewrite proxy proxy_http proxy_html

Opening the ports

sudo nano /etc/iptables/rules.v4
sudo reboot

Publishing the app

Publishing the web app
Publishing the web app
sudo mkdir /var/www/mywebsite
sudo chown -R ubuntu:www-data /var/www/mywebsite
sudo chmod --verbose -R 750 /var/www/mywebsite

Adding Kestrel service to run our application

sudo nano /etc/systemd/system/kestrel-mywebsite.service
Description=My Web App
ExecStart=/usr/bin/dotnet /var/www/mywebsite/The.App.Entrypoint.dll --urls "http://localhost:5100"
# Restart service after 10 seconds if the dotnet service crashes:
sudo systemctl enable kestrel-mywebsite.service
sudo systemctl start kestrel-mywebsite.service
sudo systemctl status kestrel-mywebsite.service
sudo journalctl -fu kestrel-mywebsite.service #Checks the events of your servicesudo systemctl stop kestrel-mywebsite.service #Stop the service
sudo systemctl disable kestrel-mywebsite.service #Disable it
# You can even launch the website manually to see if it works with the same command that the service is launching and see if you had runtime errors#Go to your website folder
cd /var/www/mywebsite
#Launch the website as the service does with the dotnet command
/usr/bin/dotnet /var/www/mywebsite/The.App.Entrypoint.dll --urls "http://localhost:5100"

Configure Apache to call Kestrel when a request is done

sudo nano /etc/apache2/sites-available/mywebsite.conf
<VirtualHost *:*>
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass /
ProxyPassReverse /
ServerAlias *
ErrorLog ${APACHE_LOG_DIR}mywebsite-error.log
CustomLog ${APACHE_LOG_DIR}mywebsite-access.log common
sudo apachectl configtest
sudo a2ensite mywebsite.conf #enable the website
sudo systemctl reload apache2
sudo systemctl restart apache2
The website Working!
The website Working!

Project Manager and former Architect & Developer specialized in Microsoft technologies since 2004. Living in Madrid, Spain. Techie photographer, travels, wines.