Hosting on Ubuntu Linux 20.04

Case Details

May 19, 2022

Publish .Net 6.0 Web App on Ubuntu Linux 20.04

A Step by Step guide to creating Web App and Publish on Linux

1. Create a new project and select the ASP.NET Core Web App

Publish-.Net-6.0-Web-App

2. Specify your Project name, location, and Solution name to Configure your project

Publish-.Net-6.0-Web-App

3. Specify framework to .Net 6.0 latest one

Publish-.Net-6.0-Web-App

4. Your project is almost ready and looks like this one.

Publish-.Net-6.0-Web-App

5. Configure a reverse proxy server in the application

After creating the project next, we are going to make changes in Configure method in the Startup class we are going to add UseForwardedHeaders middleware.

6. Do some additional configuration.

Mainly need to do app.UseForwardedHeaders(); app.UseHttpsRedirection(); and app.UseEndpoints(endpoints =>

Publish-.Net-6.0-Web-App

Supported distributions

The following table is a list of currently supported .NET releases and the versions of Ubuntu they're supported on. These versions remain supported until either the version of .NET reaches end-of-support or the version of Ubuntu reaches end-of-life.

  • A indicates that the version of Ubuntu or .NET is still supported.
  • A indicates that the version of Ubuntu or .NET isn't supported on that Ubuntu release.
  • When both a version of Ubuntu and a version of .NET have , that OS and .NET combination is supported.

Ubuntu .NET Core 3.1 .NET 5 .NET 6
21.10 3.1 5.0 6.0
21.04 3.1 5.0 6.0
20.10 3.1 5.0 6.0
20.04 (LTS) 3.1 5.0 6.0
19.10 3.1 5.0 6.0
19.04 3.1 5.0 6.0
18.10 3.1 5.0 6.0
18.04 (LTS) 3.1 5.0 6.0
17.10 3.1 5.0 6.0
17.04 3.1 5.0 6.0
16.10 3.1 5.0 6.0
16.04 (LTS) 3.1 5.0 6.0

The following versions of .NET are no longer supported. The downloads for these still remain published:

  • .NET Core 3.0
  • .NET Core 2.2
  • .NET Core 2.1
  • .NET Core 2.0

7. Connecting to Linux Server using PuTTY

For connecting to Linux Virtual Machine, we are going to use the PuTTY URL for downloading putty https://www.putty.org/.

To get a Public IP address to connect to your VM just click on Home Menu from the sidebar then you can Recent resources from that choose recently Create Virtual Machine you will see a similar screen as shown below.

Let’s start PuTTY for connecting to the Virtual Machine. Enter your public IP address in Host Name and Port will be 22 as shown in the above screenshot and click on the Open button to connect.

Publish-.Net-6.0-Web-App

Enter your username which you have set while creating the Virtual Machine.

Publish-.Net-6.0-Web-App

And enter a Password

Publish-.Net-6.0-Web-App

After connecting the terminal, we are going to run some installation in it.

8. Register the Microsoft Product key as trusted

We are going to run this command in the terminal.

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

9. Install .Net

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

Install the SDK

sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-6.0

Install the runtime

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

10. Publishing the Application

Publish application on local (windows) IIS

Publish-.Net-6.0-Web-App

Publish Files

Publish-.Net-6.0-Web-App <

Your web application is up and running on local IIS.

Publish-.Net-6.0-Web-App <

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

11. Copying Application to Linux Server

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

Publish-.Net-6.0-Web-App

12. Install Apache

In this step, we are going to install the Apache webserver.

sudo apt install apache2

13. Check Apache Server Status

Next, we have installed Apache now we can check the Apache webserver status is it running.

sudo systemctl status apache2

Publish-.Net-6.0-Web-App <

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

Publish-.Net-6.0-Web-App

14. Enable the required apache modules

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

Run each command one by one. After Enabling modules, the Apache server needs to restart.

15. Restart Apache

Here we are going to restart the Apache server after Enabling Modules in the last step.

sudo service apache2 restart

16. Create a Folder in the www folder and copy all application files

In step 11 we have copied files to /home/devadmin/publish/ folder now we are going copy those files to /var/www/publishapplication/ folder. Before copying, we are going to create a folder.

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

mkdir -p command
With the help of mkdir -p command, you can create subdirectories of a directory. It will create a parent directory first if it doesn’t exist. But if it already exists, then it will not print an error message and will move further to create sub-directories. https://www.javatpoint.com/linux-mkdir-p

Copy files to newly created folder /var/www/publishapplication/Publish folder.

sudo cp -r /home/devadmin/publish/ /var/www/publishapplication/

You can view this folder using WinSCP tool and navigate to /var/www/publishapplication/

cp -r command

Option ‘r’ with the copy command can be used to copy a directory including all its content from a source directory to the destination directory. https://www.javatpoint.com/linux-cp-r

17. 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 webapplicationfile.conf many people give the domain name to the Configuration file for easy to recognize.

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

18. Add the below content to webapplicationfile.conf

<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-webnix.com.log
    CustomLog ${APACHE_LOG_DIR}/access-webnix.com.log combined
<VirtualHost>

Publish-.Net-6.0-Web-App

After adding this content just save the file by pressing Crtl + S. Then press Ctrl + x to jump back from this window and back to a terminal window.

19. Disable the default site defined in 000-default.conf

sudo a2dissite 000-default.conf

After disabling the site next defined in 000-default.conf file next, we are going to activate current configured virtual host configuration file.

20. Activate the webapplicationfile.conf

Here we are going to enable webapplicationfile.conf file.

sudo a2ensite webapplicationfile.conf

21. Check the syntax of the Default Configuration file

sudo apachectl configtest

If the message of the terminal is Syntax OK then your configuration file is proper

Publish-.Net-6.0-Web-App

22. Restart Apache2 to make to take new changes

sudo systemctl restart apache2

23. Creating service

Creating a systemd Service file with the name webapplicationfile.service

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

Paste the following contents into the service file.

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

[Service]
WorkingDirectory=/var/www/publishapplication/publish
ExecStart=/usr/bin/dotnet /var/www/publishapplication/publish/DotNet6.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

WorkingDirectory= defines in 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.

Save this file by pressing Ctrl + s.

24. Enable the service

sudo systemctl enable dotnet6servicefile.service

25. Start the service

sudo systemctl start dotnet6servicefile.service

26. Output

Publish-.Net-6.0-Web-App

Your .Net 6.0 Web App is successfully hosted on Linux Ubuntu 20.04 and the web app is up & running.

All this setup and configuration is credited to this article https://tutexchange.com/how-to-host-asp-net-core-app-on-ubuntu-with-apache-webserver

Project Details
  • Technology: .Net 6.0
  • Database: MS SQL
  • Platform: Linux Ubuntu 20.04

Related Case Studies