Host ASP.NET Core with Apache Using Webmin

If you are using Webmin to administer your Linux box, here are the steps to run ASP.NET Core application with Apache.

Prerequisites

  • Ubuntu 18.04
  • Webmin

I assume that you have setup your server, Webmin and everything is up and running.

Install .Net Core

Here is a compilation of commands to install the .Net Core 3.1 on Ubuntu 18.04. Following is a list of commands without any clutter and specific for Webmin/Virtualmin setup.

Open a terminal with sudo user and execute following command to register Microsoft key

wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb 

Now install .Net Core 3.1 and ASP.NET Core 3.1

sudo add-apt-repository universe 
sudo apt-get install dotnet-runtime-3.1 
sudo apt-get install aspnetcore-runtime-3.1 

If you want to install other .Net Core version, follow the main tutorial to get the correct command.

Setup .Net Core Website

Using Webmin, create a new Virtual Server. It would create a new folder “/home/Your_Virtual_Server”. Here create a new folder called “netcore_ Your_Virtual_Server” and upload and configure your .Net Core application to this folder.

Using terminal, navigate to this folder and execute the following command to quickly verify if everything is working as expected or not.

cd "/home/Your_Virtual_Server/netcore_Your_Virtual_Server"
dotnet "YourApp.dll"

Above command runs the application on http://localhost:5000 address by default. You can press “Ctrl+C” to terminate this application but continue running the application.

Setup Apache

While the dotnet core application is running, we want to setup Apache.

Go to Webmin > Servers > Apache Webserver on left hand side menu.

Click on “Global Configuration” tab > Edit Config Files > Select your Virtual Server Config file from the list.

Under <VirtualHost> tab, add following settings to use proxy

RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
ProxyPreserveHost On
ProxyPass /.well-known !
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/

Above settings sets the special header, excludes Let’s Encrypt SSL request and forwards all other requests to the dotnet core application running on http://127.0.0.1:5000/

If you have SSL enabled for this virtual server, Webmin creates two <VirtualHost> tags – one for non secure and another one for secure site. Add above settings for both <VirtualHost> tags.

Save the changes and go back to the screen.

Top right corner shows a button to apply the changes. Click on the button and Apache will apply changes.

Now try to visit your website in a browser and you will get response from dotnet core application.

At this moment, your dotnet core application is working as expected so now in open terminal press “Ctrl+C” to terminate the dotnet core application that was running.

Monitor the application

In above portion, you have manually ran the application but now we want this application managed by Linux. For this, we need to create service.

Create a service file “/etc/systemd/system/kestrel-Your_Virtual_Server_APP.service” using Webmin. Change the file name “kestrel-Your_Virtual_Server_APP.service” as per your need.

Open the service file in Webmin and paste following text. Make change to the file as per your need

[Unit]
Description= Your_Virtual_Server_APP .Net Core 3.1 App

[Service]
WorkingDirectory=/home/Your_Virtual_Server/netcore_Your_Virtual_Server 
ExecStart=/usr/local/bin/dotnet /home/Your_Virtual_Server/netcore_Your_Virtual_Server/YourApp.dll
Restart=always
Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=kestrel-Your_Virtual_Server
User=Your_Virtual_Server

[Install]
WantedBy=multi-user.target

In above text, change Description, WorkingDirectory, ExecStart, SyslogIdentifier and User as per your configuration.

Enable service by executing following command using sudo user at terminal

sudo systemctl enable kestrel-Your_Virtual_Server_APP.service

At this moment, your service is enable. Now go to Webmin > System > Bootup and Shutdown.

Here you will see your service in a list. Select your service by clicking on a checkbox. Go all the way down and click on “Start on Boot” in order to start this service on bootup. Again select this service and click on “Start” button to start your service.

You are all set and now your .net core application is happily running behind the Apache proxy.

Share

0 comments

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