Skip to main content
All CollectionsVPSVPS Management
How to Launch an ASP.NET Application
How to Launch an ASP.NET Application

Launching an ASP.NET application at Hostinger

Updated over 9 months ago

ASP.NET is a popular web development framework created by Microsoft. It allows developers to build dynamic and robust web applications, websites, and services using various programming languages like C# and Visual Basic .NET.

With its powerful features and seamless integration with other Microsoft technologies, ASP.NET simplifies the process of creating and deploying web applications.

The ASP.NET supported on Ubuntu is ASP.NET core. If you use ASP.NET MVC with .NET Framework or Web API, you might need to convert your project to ASP.NET Core MVC before you can deploy it in our Linux VPS.

To launch the ASP.NET application at Hostinger, you need a VPS with Ubuntu 22.04 64bit with ASP.NET template installed.

Visit our ASP.NET hosting page to learn more about the VPS plans available 🚀

Configure the ASP.NET Core Application

Once you log into your VPS via SSH, you can check installed .NET versions by running this command:

dotnet --version

To start off, we will create a simple web application from preinstalled templates and call it NewApp:

dotnet new webapp -o NewApp

This creates a new folder for your application: /root/NewApp/. Let’s change the default port of your application to 5000 in launchSettings.json:

nano /root/NewApp/Properties/launchSettings.json

It should look like this:

  "profiles": {
"NewApp": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}

Also, we have to remove the UseHttpsRedirection() middleware because we'll use NGINX to handle the HTTPS redirection. Open the Program.cs file and make sure there are no app.UseHttpsRedirection():

 nano /root/NewApp/Program.cs

We can add app.UseForwardedHeaders() middleware as well since we are using NGINX as a reverse proxy. It should look like this:

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseStaticFiles();
app.UseForwardedHeaders();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();

Publish and Run the Application

To publish the application using the Release configuration, make sure you're in the project directory, and then run the command below:

cd /root/NewApp/
dotnet publish --configuration Release

The project has been published. Now, we must create a service to ensure the application starts and runs. First, let's create a service file for our project inside the /etc/systemd/system directory:

cd /etc/systemd/system
sudo nano NewApp.service

In the new file, put the following configuration:

[Unit]
Description=NewApp

[Service]
WorkingDirectory=/root/NewApp/bin/Release/net6.0/publish/
ExecStart=/usr/bin/dotnet /root/NewApp/bin/Release/net6.0/publish/NewApp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-NewApp
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Now, let's enable and start the service using the commands below:

sudo systemctl enable NewApp.service
sudo systemctl start NewApp.service

Install and Configure the NGINX

Start with installing the NGINX server. It is a popular open-source web server software known for its high performance, scalability, and robustness. It is widely used as a reverse proxy server, load balancer, and HTTP cache.

sudo apt-get -y install nginx certbot python3-certbot-nginx

After the installation is completed, let's configure the NGINX. To do that, you can edit the configuration file using the command below:

sudo nano /etc/nginx/sites-available/default 

Let's make two changes to this file. First, set your domain name in server_name:

server_name domain.tld; 

Then, set the location configuration as follows:

location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

Install an SSL and Launch the NGINX Proxy

Once the DNS propagation of your domain is completed, proceed with generating the SSL certificate for your ASP.NET application. Run the command below to generate the SSL certificate automatically:

sudo certbot --nginx -d domain.tld

After that, restart the NGINX to apply the configuration:

nginx -t && nginx -s reload

Then, run the crontab command to auto-renew the SSL certificate:

crontab -e

Finally, add the command below to auto-renew the SSL every day at 12:00 (noon):

0 12 * * * /usr/bin/certbot renew --quiet

And that is it! You can now access your application via the domain you configured in this tutorial 🎉

Did this answer your question?