Deploying an ASP.NET MVC application on Ubuntu 20.04 on a VPS. Installing SSL

In this article, I will show you step by step how to:

  1. Deploy an ASP.NET Application

  2. Install and configure MS SQL

  3. Install SSL certificate

All this on a VPS with a clean Ubuntu 20.04 system.

MS SQL requires a minimum of 2GB of free RAM to install and run.

I will show on the example of a ready-made ASP.NET site for watching anime (Japanese animation). It works and runs without errors on localhost. The application uses MS SQL database which we will install and configure on step six.

ASP.NET Core 3.1 Application |  MS SQL
ASP.NET Core 3.1 Application | MS SQL

Step one: application publishing

In Solution Explorer, right click on the project -> Publish.

If you don’t have a profile, click “Add a publishing profile” -> Select a target folder -> Specify a convenient location where the files will be uploaded -> Done.

My profile settings:

  • Configuration: Release

  • Target platform: netcoreapp3.1

  • Deployment Mode: Platform dependent

  • Target runtime: Portable version

I click publish.

If you have a different target platform, then on step three you will need to download the version of the .NET-SDK you need.

Step two: send files via SFTP and establish SSH connection

The username and password were sent to my e-mail, specified during registration. Usually the username is root. The IP address is written in the VPS settings on the site.

To connect via SFTP (SSH File Transfer Protocol) To transfer files, I use the FileZilla program.

Launch -> File -> Site Manager -> New Site

The protocol must be selected SFTP – SSH File Transfer Protocol.
Host is the IP from the VPS.
I enter the login and password from the letter, click connect.

On the VPS, I created the /var/netcore/ directory and the files of the published application, transferred them to the created directory using Drag&Drop. FileZilla shows some errors in logs, but I just ignore them and everything works.

Establishing an SSH connection. Most sites provide the ability to open a console with SSH right in the browser with a single click, but it’s kind of laggy. It is much more convenient for me to do this via CMD on windows 10.

ssh <ip> -l <login>
ssh "134.0.118.152" -l root

Step three: install .NET Core 3.1 on Ubuntu

I didn’t have Microsoft’s .NET Core repositories installed by default, so I downloaded and installed them with these commands:

sudo 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

Now I install .NET Core 3.1 and the required libraries:

sudo apt-get update
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-3.1

Step four: install and configure Apache

Install Apache and connect the necessary modules:

sudo apt-get install apache2
sudo a2enmod proxy proxy_http proxy_html proxy_wstunnel
sudo a2enmod rewrite

The next thing to do is to create a configuration file that will forward requests on port 80 (and in the future 443 for SSL) to our ASP.NET application. The configuration file must be created along the path /etc/apache2/conf-enabled/netcore.conf

You can create a file on your OS and use FileZilla to transfer it to the path above, or create it using the nano/vim text editor. I’m using nano.

sudo nano /etc/apache2/conf-enabled/netcore.conf

File content:

<VirtualHost *:80>
   ProxyPreserveHost On
   ProxyPass / http://127.0.0.1:5000/
   ProxyPassReverse / http://127.0.0.1:5000/
</VirtualHost>

If your .NET application is running on a different port, use it instead of 5000.

Check the config for errors and restart apache to update the config:

sudo apachectl configtest
sudo service apache2 restart

Step five: create a service with ASP.NET application

In fact, this step is optional and can simply be skipped and replaced with the command dotnet YourApp.dll. Now you can write the IP address of the VPS in the browser line and see your site. I’m currently having a 500 error due to missing MS SQL on the VPS, fixing it to step six, site will work. But keeping in a thread (I’m not sure what it’s called in unix) your user’s always running application is not an option, so let’s set up a service that does this constantly in the background.

To end the application, press the keyboard shortcut Ctrl + C.

I have created a file along the path /etc/systemd/system/AspNetServer.service AspNetServer is the name of our service, later we will use it to start, stop, restart the application, read its logs, etc. You can specify any name, the main thing is to leave .service at the end.

Who is easier to create a file on their OS and send it through FileZilla, do it, I’ll just use the command sudo nano /etc/systemd/system/AspNetServer.service and paste the following code:

[Unit]
Description=ASP .NET Web Application
[Service]
WorkingDirectory=/var/netcore
ExecStart=/usr/bin/dotnet /var/netcore/AnimeSite.dll
Restart=always
RestartSec=10
SyslogIdentifier=netcore-demo
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target

On line 5, instead of AnimeSite.dll, specify the dll file of your application.

Now let’s start the server:

sudo systemctl enable AspNetServer
sudo systemctl start AspNetServer

You can check the service status with the command:

sudo systemctl status AspNetServer

To exit the status view, press Ctrl + C.

If your application does not use databases, then everything is ready, enter the IP from VDS into the browser line and check. If the application throws an error or you just want to read the output of the application, use the command:

journalctl -u AspNetServer

By entering numbers, you can choose which line to jump to. I write 9999 to jump to the end and see what kind of exception comes up.

Step six: installation and configuration of MS SQL

Install the required MS SQL library and repositories:

sudo apt-get install software-properties-common
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2022.list)"

Then install the MS SQL loader:

sudo apt-get update
sudo apt-get install -y mssql-server

And with the help of this command, let’s move on to installing MS SQL:

sudo /opt/mssql/bin/mssql-conf setup

We are prompted to choose which version to install, I will choose Express at number 3. I agree to the terms of use, set my super complex password and you’re done. Let’s check if MS SQL works with the command systemctl status mssql-server --no-pager

I change the connection string in appsettings.json with my super complex password:

Server=localhost;Database=animesitedb;User Id=sa;Password=<MyStrongPassword>;

And I restart the AspNetServer service with the command:

sudo systemctl restart AspNetServer

Ready! The site is working. The next step will be dedicated to setting up an SSL certificate.

Step seven: SSL certificate setup

When buying a domain, they gave me an SSL certificate as a gift. All information has been sent to the mail. To install it on the site, I created 3 files:

  • yourdomain.crt – the certificate itself

  • privatekey.key – private key

  • intermediate.crt – intermediate certificate

Important! The data in these files must be inserted along with

—–BEGIN CERTIFICATE—–

—–END CERTIFICATE—–

In the Asp.Net application directory, I created the ssl-certificates directory.
I moved all certificate files to /var/netcore/ssl-certificates/

Let’s add the SSL module to our Apache server:

sudo a2enmod ssl

Now I need to change the Apache configuration file, I open the nano editor with the command sudo nano /etc/apache2/conf-enabled/netcore.conf and change the code:

<VirtualHost *:80>
   ProxyPreserveHost On
   ProxyPass / http://127.0.0.1:5000/
   ProxyPassReverse / http://127.0.0.1:5000/
</VirtualHost>

<VirtualHost *:443>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    SSLEngine on
    SSLCertificateFile /var/netcore/ssl-certificates/yourdomain.crt
    SSLCertificateKeyFile /var/netcore/ssl-certificates/privatekey.key
    SSLCertificateChainFile /var/netcore/ssl-certificates/intermediate.crt
</VirtualHost>

If you want HTTP to automatically redirect to HTTPS, change the content of to this:

<VirtualHost *:80>
   Redirect permanent / https://example.com/
</VirtualHost>

example.com – replace with your domain.
Check the correctness of the config and restart Apache:

sudo apachectl configtest
sudo service apache2 restart

Outcome

That’s all, thanks to everyone who read, I hope it helped you.

Most of the information is taken from here – https://www.c-sharpcorner.com/article/how-to-deploy-net-core-application-on-linux/

I learned about configuring SSL on Apache from this instruction – https://support.globalsign.com/ssl/ssl-certificates-installation/apache-http-server-ssl-certificate-installation

Similar Posts

Leave a Reply

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