How to Deploy ASP.NET web application to Linux VPS

  • Reading time:12 mins read

ASP.NET Core applications are versatile and can be hosted on a variety of platforms. While Windows servers are a common choice, deploying them on Linux servers offers advantages in terms of stability, security, and cost-effectiveness. In this guide, we’ll walk you through the process of deploying an ASP.NET Core application on a Linux server running Ubuntu 20.04 using the Apache web server. Please note that we’re using Ubuntu 20.04 instead of 22.+ due to compatibility issues with installing MsSql Server directly on the server without Docker.

Deploying ASP.NET Application

Publish your application from Visual studio.

  • Begin by publishing your ASP.NET Core application from Visual Studio. Ensure that your appsettings.json file includes the desired port for Kestrel to run on (e.g., "urls": "http://localhost:5001") if you don’t want it to run on the default port (5000). This port differs from the standard 80/443 ports used for external access.
  • Right click on the project and click Publish. Set up on publish profile. For this tutorial, we are publishing to a Folder with these configuration.
    • Configuration: Release
    • Deployment Mode: Framework-dependent
    • Target Runtime: Portable
  • Click Publish and the deployment build should be on the specified folder on your local system.

Linux Server Setup

For server setup and management, we’ll use two essential tools: PuTTY and WinSCP (though you can also consider FileZilla).

  • PuTTY: A free, open-source terminal emulator that supports various network protocols like SCP, SSH, Telnet, and more. Download it from here.
  • WinSCP: A popular SFTP and FTP client for Windows, perfect for managing files between your local machine and remote servers. Download it here.

Step 1 – Installing .NET Core and necessary dependencies

Open the installed PuTTY and enter the server IP Address(Host Name) and root password.

PuTTY interface

To host your ASP.NET Core application, you’ll need to install the .NET Core framework.

Run the following commands in PuTTY:

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
sudo apt-get update
sudo apt-get install -y apt-transport-https
sudo apt-get install -y aspnetcore-runtime-7.0

Confirm a successful installation with ‘dotnet --info‘ .

Step 2 – Installing Apache

Install Apache on your server with these commands:

sudo apt update
sudo apt install apache2

After installation, enable the necessary Apache modules and restart Apache:

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

Restart Apache to activate the new configuration. Use need to restart Apache any time you make changes to your application(like redeploy)

sudo systemctl restart apache2

Step 3 – Move the application files into the Linux Server

Move the deployed application files to the Ubuntu Server. To do this, we will use WinSCP/FileZilla.

  • Open WinSCP and connect to the server using the server root credential.
  • Navigate to /var/www/ folder on the server, and create a folder to house your application files.
  • Select all the application files from local system into the newly created folder on the server.
WinSCP interface

Configure Apache

The service file

To make your ASP.NET Core application accessible through Apache, create a .service file (e.g., hello.service) on your local system and move it to /etc/systemd/system/ on the server. Below is a sample .service file:

[Unit]
Description=Hello World Application

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

Ensure that the www-data user has ownership permission for the application folder. Move the .service file to /etc/systemd/system/ on the server and enable the service:

sudo systemctl enable hello.service
sudo systemctl start hello.service
sudo systemctl status hello.service

Add Web App to Apache

Edit the Apache configuration file located at /etc/apache2/sites-available/000-default.conf. Add the following configurations to proxy requests to your ASP.NET Core application:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5001/
    ProxyPassReverse / http://127.0.0.1:5001/
    ServerName www.helloworld.com
    ServerAlias *.helloworld.com
    ErrorLog ${APACHE_LOG_DIR}/helloworld-error.log
    CustomLog ${APACHE_LOG_DIR}/helloworld-access.log common
</VirtualHost>

If you want to enable HTTPS, configure it as follows:

<VirtualHost *:80>
ServerName www.helloworld.com
ServerAlias *.helloworld.com
Redirect permanent / https://helloworld.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
ServerName www.helloworld.com
ServerAlias *.helloworld.com
Protocols             h2 http/1.1
ProxyPreserveHost     On
ProxyPass             / http://127.0.0.1:5001/
ProxyPassReverse      / http://127.0.0.1:5001/
ErrorLog              ${APACHE_LOG_DIR}/helloworld-error.log
CustomLog             ${APACHE_LOG_DIR}/helloworld-access.log common
SSLEngine on
SSLCertificateFile /etc/ssl/helloworld.crt
SSLCertificateKeyFile /etc/ssl/private/helloworld.key
SSLCertificateChainFile /etc/ssl/helloworld.ca-bundle
</VirtualHost>
  • *:80 and *:443 are the listening ports for the incoming request. 80 is the default port for HTTP and 443 is the default for HTTPS. If you choose to use another port, you should add the port to ports.conf in /etc/apache2 folder
  • ServerName and ServerAlias are the source URL. This is the url the users typed in their browser. Apache uses this to identify the source of the request so that it can be properly redirected. You need to setup a DNS resolver like Cloudflare resolve your domain name to your VPS server IP address. This article covers how to point domain name to the VPS.
  • ProxyPass and ProxyPassReverse are the internet destination of the request. This is the port number we configure earlier for our Kestrel service to run.
  • SSLCertificateFile, SSLCertificateKeyFile and SSLCertificateChainFile are the SSL file. Check out this article on how to install SSL certificate, and this article on how to generate CSR for the SSL.

To host multiple application on this Linux server add VirtualHost to 000-default.conf for the new application and map the correct ServerName, ServerAlias, ProxyPass and ProxyPassReverse for the app.

Finally, test your Apache configuration for errors:

sudo apachectl configtest

Remember to restart Apache with the command below

sudo systemctl restart apache2

Securing Your Server

To enhance security, configure your server’s firewall:

sudo apt-get install firewalld -y
sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=443/tcp --permanent
sudo firewall-cmd --reload

Use the following command to check the firewall configuration:

sudo firewall-cmd --list-all

This setup will allow traffic over ports 80 (HTTP) and 443 (HTTPS).

Conclusion

In this guide, I’ve walked you through the process of deploying your ASP.NET Core application on a Linux server running Ubuntu 20.04 with the Apache web server. By leveraging the power of Linux, you’ve gained access to a stable, secure, and cost-effective hosting environment for your web applications.

Remember, successful deployment involves careful configuration and testing. Ensure that your ASP.NET Core application runs flawlessly on your Linux server before making it accessible to your users.

By following these steps, you’re well on your way to hosting your ASP.NET Core application on a robust Linux server.

Stay Informed and Share Your Thoughts

I hope this guide has been valuable to you. To stay updated on the latest technology trends, tips, and tricks, and to receive more insightful guides like this one, subscribe to our newsletter – The Light. Don’t hesitate to drop your comments or questions below – I will love to hear from you and assist with any inquiries you may have.

Leave a Reply