Tutorial: How to host ASP.NET on Apache (Linux)

Step by step tutorial about how to host an ASP.NET website on Linux running Apache.

Prerequisites

  • You installed apache

Install dotnet

Lets install dotnet following the official microsoft page. We start downloading the installer for ASP.NET Core Runtime.

wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update

With these 4 commands, we’ll be able to use apt to install the packages we need.

Lets install the SDK and runtime for .NET 7.0.

sudo apt-get install -y dotnet-sdk-7.0
sudo apt-get install -y aspnetcore-runtime-7.0

Create a new project

Create a new dotnet project inside of an user folder, for exampe /home/timothy/myNewApp

cd /home/timothy
mkdir myNewApp
cd myNewApp

Inside this folder we can create our new project:

dotnet new webapp

With this simple command we created a new empty website. You can check the content using dir:

Now you can enable your website with the command dotnet run. You can also specify the url from which you’ll reach the website. On this tutorial I’ll use a custom ip address instead of localhost:

dotnet run --urls=http://192.168.1.68:5000/

Now, open the browser and check localhost or the address you specified. You’ll see your new website.

Publish the website

Now we’re ready to publish our website:

dotnet publish --configuration Release

You’ll find the published files in /home/timothy/myNewApp/bin/Release/net7.0/publish

Configure Apache

Now we need to configure an Apache virtual host using a proxy. Start enabling the necessary modules:

sudo a2enmod headers
sudo a2enmod proxy
sudo a2enmod proxy_http

Modify your virtual host configuration file in /etc/apache2/sites-available/000-default.con and set content:

<VirtualHost *:*>
        RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
</VirtualHost>

<VirtualHost *:80>
        ProxyPreserveHost On
        ProxyPass / http://127.0.0.1:5000/
        ProxyPassReverse / http://127.0.0.1:5000/
        ServerName 192.168.1.68
        ErrorLog /var/log/apache2/error.log
        CustomLog /var/log/apache2/access.log combined
</VirtualHost>

Make the folders to move the website in (/var/www/myNewApp/public_html) and restart apache.

sudo systemctl restart apache2

Connecting to your web server from a browser you should now receive an error 503 Service Unavailable. This is because Apache is trying to comunicate with dotnet service on port 5000, but we still have not enabled the service.

Lets copy the content of /home/timothy/myNewApp/bin/Release/net7.0/publish inside of /var/www/myNewApp/public_html

sudo rsync -r /home/timothy/myNewApp/bin/Release/net7.0/publish/* /var/www/myNewApp/public_html

Configure dotnet service

Now we need to create our dotnet service. To do so, create a new file in /etc/systemd/system and execute it.

cd /etc/systemd/system
sudo nano kestrel-myNewApp.service

Where kestrel-myNewApp will be the name of the dotnet service for this website.

In the file, write the following (bold what you need to change):

[Unit]
Description=.NET Core App running on ${distribution}
[Service]
WorkingDirectory=/var/www/myNewApp/public_html
ExecStart=/usr/bin/dotnet /var/www/myNewApp/public_html/myNewApp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-myNewApp-app
User=timothy
Environment=ASPNETCORE_ENVIRONMENT=Production 
[Install]
WantedBy=multi-user.target

And lets check what we’ve done using sudo systemctl status kestrel-myNewApp which should be Loaded and Inactive.

Now enable and start the service:

sudo systemctl enable kestrel-myNewApp
sudo systemctl restart kestrel-myNewApp

Now use againg sudo systemctl status kestrel-myNewApp to check the service status. It may take a couple of minutes to start.

Done

Now your Apache server running on Debian 11 is hosting an asp.net website.

Have a look on this youtube tutorial: https://youtu.be/mBhT_MRwGYE.

Leave a Reply

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