Guide to hosting .NET 8 ASP.NET Core and Blazor Web Assembly on a Linux VPS

KristiadhyKristiadhy
5 min read

To deploy your Asp .Net Core or Blazor Web Assembly 8.0 application to production, you have several options. You might be familiar with AWS and Azure, but a VPS could be an appealing choice if you're looking for a more budget-friendly solution.

💡
If you've already got a VPS service set up for your .net app, you can skip this article and check out the article on deploying your .net app. It's at Deploying your .net app to Linux VPS.

I previously used Azure to host Asp .Net Core or Blazor Web Assembly 8.0 application and it was easy. You simply select and set up the service in the Azure portal, link your GitHub account, publish your application, and point to your Azure account, and your website app will go online. Now let's explore another method using a VPS, which is also pretty simple if we're willing to understand the details.

Before you can host your Asp .Net Core or Blazor Web Assembly 8.0 application on a Linux VPS, you need to acquire and set it up.

Get a VPS service.

First, pick any provider that suits you. VPS prices are usually pretty affordable. Just remember, before you start installing Linux OS on your VPS server, make sure the OS version is compatible with the .Net version you want to install, in this case, .Net 8. You can check the supported .NET releases and the versions of Ubuntu they're supported here: .NET and Ubuntu overview - .NET | Microsoft Learn. In my case, I use Ubuntu 20.04, just make sure to select the right OS during the initial VPS registration. A web server is typically pre-installed when you register for a VPS. If it’s not, you can choose from popular options like Nginx or Apache. This time, I’m going with Apache.

Install the .NET 8.0 package.

After the Apache is installed on your server, now it's time to install the .Net 8.0 package. You can choose between the SDK and runtime options, depending on what you need. Check out this link for details: https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu-install?tabs=dotnet8&pivots=os-linux-ubuntu-2004.

Next, make sure you add the Microsoft package signing key to your trusted keys and include the package repository.

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

To install the .NET runtime, just run the following commands:

sudo apt-get update && sudo apt-get install -y aspnetcore-runtime-8.0

Create a directory for your website.

You can do the following:

mkdir -p /var/www/yourdomainname.com

Set the ownership of the directory. If you have set up a separate user, you can configure it for that user. But I haven't set up a specific user in this guide, so I'm still using root.

sudo chown -R $USER:$USER /var/www/yourdomainname.com

Next, make sure the permissions on the directory allow you to read, write and execute files. And only give read and execute permissions to groups and others.

You can run the command below.

sudo chmod -R 755 /var/www/yourdomainname.com

Change the default virtual host configuration.

Go to /etc/apache2/sites-available/, and you will find a file like this 000-default.conf. This is the default virtual host configuration, you can copy its contents and create a new virtual host file for your site, you can name it yourdomainname.com.conf. Edit the contents as shown in the example below:

<VirtualHost *:80>
ServerAdmin admin@yourdomainname.com
ServerName yourdomainname.com
ServerAlias www.yourdomainname.com
DocumentRoot /var/www/yourdomainname.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Replace yourdomainname.com with your domain name and adjust the document root to the folder path that will be used to store website data. The next step is to enable the virtual host configuration you have created.

Run the following command:

a2ensite yourdomainname.com

Disable the default configuration and check that the virtual host configuration is correct by using the command below:

sudo a2dissite 000-default.conf
sudo apache2ctl configtest

If it is correct, the 'syntax OK' message will appear. Now restart the Apache service.

service apache2 restart

Now you can test by accessing the domain name via your web browser.

Install and upload the SSL to your server.

First, you need to buy the SSL and choose a provider that suits you. In my case, I use Jagoan hosting service https://www.jagoanhosting.com/. You can find out how to configure it from the provider you bought the SSL from. For me, I use the following (You can change the website's language to English):

  1. SSL Activation Tutorial via Member Area from Jagoan Hosting

  2. How to Install SSL on Apache Ubuntu - Knowledge Base Jagoan Hosting Indonesia

To simplify it I will summarize it briefly as follows:

  1. Generate the CSR.

  2. Copy the private key to your storage and keep it.

  3. Validate your SSL. There are some options to do this: By DNS name, by email, or by using HTTP/HTTPS.

  4. Download your SSL certificate, it contains 3 CA files and 1 CRT file.

  5. Make a CA bundle file by combining the 3 CA files you have downloaded.

  6. Now you have 3 files: private key, CA bundle, and the domain certificate.

  7. Enable SSL module in Apache: a2enmod ssl and then restart the Apache service apache2 service restart .

  8. Create a folder to store your SSL mkdir -p /etc/ssl/private and set the role to it chmod 700 /etc/ssl/private.

  9. Upload your SSL to the server by using FTP client, in my case I use WinSCP.

  10. Edit your virtual host configuration by adding these lines:

    SSLEngine on
    SSLCertificateFile /etc/ssl/private/certificate.crt
    SSLCertificateKeyFile /etc/ssl/private/private.key
    SSLCertificateChainFile /etc/ssl/private/ca-bundle.crt
    …
    

    Then restart apache with the command:

    apache2 service restart
    

    Now your Linux VPS should be ready. It's time to deploy your ASP .NET Core or Blazor Web Assembly 8.0 application!

0
Subscribe to my newsletter

Read articles from Kristiadhy directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kristiadhy
Kristiadhy

Experienced Full Stack .NET developer with a proven track record of designing and implementing robust business applications. Proficient in using ASP.NET Core Web API, Blazor, and WinForms to deliver high-quality, efficient code and scalable solutions. Strong focus on implementing industry best practices for cleaner and scalable outcomes.