nano \/root\/NewApp\/Properties\/launchSettings.json<\/code><\/pre>It should look like this:<\/p>
\"profiles\": {\r\n \"NewApp\": {\r\n \"commandName\": \"Project\",\r\n \"dotnetRunMessages\": true,\r\n \"launchBrowser\": true,\r\n \"applicationUrl\": \"http:\/\/localhost:5000\",\r\n \"environmentVariables\": {\r\n \"ASPNETCORE_ENVIRONMENT\": \"Development\"\r\n }\r\n }<\/code><\/pre>Also, we have to remove the UseHttpsRedirection()<\/b> middleware because we’ll use NGINX to handle the HTTPS redirection. Open the Program.cs<\/b> file and make sure there are no app.UseHttpsRedirection()<\/b>:<\/p> nano \/root\/NewApp\/Program.cs<\/code><\/pre>We can add app.UseForwardedHeaders()<\/b> middleware as well since we are using NGINX as a reverse proxy. It should look like this:<\/p>var builder = WebApplication.CreateBuilder(args);\r\n\/\/ Add services to the container.\r\nbuilder.Services.AddRazorPages();\r\nvar app = builder.Build();\r\n\/\/ Configure the HTTP request pipeline.\r\nif (!app.Environment.IsDevelopment())\r\n{\r\n app.UseExceptionHandler(\"\/Error\");\r\n \/\/ The default HSTS value is 30 days. You may want to change this for production scenarios, see https:\/\/aka.ms\/aspnetcore-hsts.\r\n app.UseHsts();\r\n}\r\napp.UseStaticFiles();\r\napp.UseForwardedHeaders();\r\napp.UseRouting();\r\napp.UseAuthorization();\r\napp.MapRazorPages();\r\napp.Run();<\/code><\/pre>Publish and run the application<\/h2>
To publish the application using the Release<\/b> configuration, make sure you’re in the project directory<\/b>, and then run the command below:<\/p>cd \/root\/NewApp\/\r\ndotnet publish --configuration Release <\/code><\/pre>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<\/b> our project inside the \/etc\/systemd\/system <\/b>directory:<\/p>cd \/etc\/systemd\/system\r\nsudo nano NewApp.service<\/code><\/pre>In the new file, put the following configuration:<\/p>
[Unit]\r\nDescription=NewApp\r\n \r\n[Service]\r\nWorkingDirectory=\/root\/NewApp\/bin\/Release\/net6.0\/publish\/\r\nExecStart=\/usr\/bin\/dotnet \/root\/NewApp\/bin\/Release\/net6.0\/publish\/NewApp.dll\r\nRestart=always\r\n# Restart service after 10 seconds if the dotnet service crashes:\r\nRestartSec=10\r\nKillSignal=SIGINT\r\nSyslogIdentifier=dotnet-NewApp\r\nUser=root\r\nEnvironment=ASPNETCORE_ENVIRONMENT=Production\r\nEnvironment=DOTNET_PRINT_TELEMETRY_MESSAGE=false\r\n \r\n[Install]\r\nWantedBy=multi-user.target<\/code><\/pre>Now, let’s enable and start the service<\/b> using the commands below:<\/p>sudo systemctl enable NewApp.service\r\nsudo systemctl start NewApp.service<\/code><\/pre>Install and configure the NGINX<\/h2>
Start with installing the NGINX server<\/b>. 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.<\/p>sudo apt-get -y install nginx certbot python3-certbot-nginx<\/code><\/pre>After the installation is completed, let’s configure the NGINX<\/b>. To do that, you can edit the configuration file<\/b> using the command below:<\/p>sudo nano \/etc\/nginx\/sites-available\/default <\/code><\/pre>Let’s make two changes to this file. First, set your domain name in server_name<\/b>:<\/p>server_name domain.tld; <\/code><\/pre>Then, set the location<\/b> configuration as follows:<\/p>location \/ {\r\n proxy_pass http:\/\/localhost:5000;\r\n proxy_http_version 1.1;\r\n proxy_set_header Upgrade $http_upgrade;\r\n proxy_set_header Connection keep-alive;\r\n proxy_set_header Host $host;\r\n proxy_cache_bypass $http_upgrade;\r\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\n proxy_set_header X-Forwarded-Proto $scheme;\r\n}<\/code><\/pre>Install an SSL and launch the NGINX proxy<\/h2>
Once the DNS propagation of your domain is completed, proceed with generating the SSL certificate <\/b>for your ASP.NET application. Run the command below to generate the SSL certificate automatically:<\/p>sudo certbot --nginx -d domain.tld<\/code><\/pre>After that, restart the NGINX<\/b> to apply the configuration:<\/p>nginx -t && nginx -s reload<\/code><\/pre>Then, run the crontab <\/b>command to auto-renew the SSL certificate<\/b>:<\/p>crontab -e<\/code><\/pre>Finally, add the command below to auto-renew the SSL every day at 12:00 (noon)<\/b>:<\/p>0 12 * * * \/usr\/bin\/certbot renew --quiet<\/code><\/pre>And that is it! You can now access your application via the domain you configured in this tutorial 🎉<\/p>\n","protected":false},"excerpt":{"rendered":"
Launching an ASP.NET application at Hostinger<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"include_on_kodee":true,"footnotes":""},"categories":[199],"tags":[],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-vps-management"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/support\/8184984-how-to-launch-an-asp-net-application-at-hostinger\/","default":1}],"include_on_kodee":true,"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":2,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":13711,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/644\/revisions\/13711"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}