Hosting on Ubuntu Linux 20.04
A Step by Step guide to creating Web App and Publish on Linux
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.
Mainly need to do app.UseForwardedHeaders(); app.UseHttpsRedirection(); and app.UseEndpoints(endpoints =>
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.
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:
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.
Enter your username which you have set while creating the Virtual Machine.
And enter a Password
After connecting the terminal, we are going to run some installation in it.
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
In this step we are going to install .NET Core Runtime allows you to run apps that were made with .NET 6.0
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
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
Publish application on local (windows) IIS
Publish Files
<Your web application is up and running on local IIS.
<After publishing, we need to copy files to Linux Machine for doing that we are going to use WinSCP tool.
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
In this step, we are going to install the Apache webserver.
sudo apt install apache2
Next, we have installed Apache now we can check the Apache webserver status is it running.
sudo systemctl status apache2
<To test in the browser, just access from Public IP. It will show you the default Apache web page as shown below.
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.
Here we are going to restart the Apache server after Enabling Modules in the last step.
sudo service apache2 restart
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
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
<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>
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.
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.
Here we are going to enable webapplicationfile.conf file.
sudo a2ensite webapplicationfile.conf
sudo apachectl configtest
If the message of the terminal is Syntax OK then your configuration file is proper
sudo systemctl restart apache2
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.
sudo systemctl enable dotnet6servicefile.service
sudo systemctl start dotnet6servicefile.service
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