Developing ASP.NET Core App in Visual Studio and Deploying on Simple Application Server

Quick FAQ

What Is Visual Studio?

Developing Example Application

Prerequisites

Alibaba Cloud account

Create ASP.NET core project from template

select File > New > Project
select ASP.NET Core Web Application as shown in image below

Edit and Change Code

This is where one would add their own development code. We are going to make a simple change.

<a asp-page="/Index" class="navbar-brand">Alibaba SAS Demo</a>

Run Application

Press Ctrl+F5 (or Debug > Start Without Debugging). The app opens in a browser.

Publish Example Project for Deployment

With Visual Studio 2017, we can package our web application with all of the references needed to run on Linux by building a “Self-Contained Application”. This type of deployment does not require a .NET SDK to be installed on a server, and can be deployed to any machine that does not have any .NET features installed.

Prepare the Web Application

Unless your application uses Globalization rules and the data, it is better to go for globalization invariant mode. This ensures you do not need to install additional ICU (http://icu-project.org/) packages on linux.

<RuntimeHostConfigurationOption Include="System.Globalization.Invariant" Value="true" />
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://*:5000")
.UseStartup<Startup>();

Publish Project as Self-Contained Application

After debugging and testing the application, create the files to be deployed.

Publish from Visual Studio

Right click on Project name (not the solution) in Visual Studio Solution Explorer and select Publish.

Publish from Command Prompt

On development machine (in our case, our Windows machine), open a command prompt at the root of your web application (location where .csproj file exists) and run the following.

dotnet publish -c release -r linux-x64

Create a Simple Application Server Instance on Alibaba Cloud

Log in and Select Simple Application Server

If you do not have Alibaba Cloud Account get one free at https://account.alibabacloud.com/register/intl_register.htm

Setup Server Configuration

Region

Deploy application on simple application server

Create Password

Log back into console and select Simple Application Server

Install Prerequisites on Server

yum

sudo yum -y update
sudo yum install nginx
sudo service nginx start
systemctl status nginx
sudo yum update
sudo yum install nano
sudo yum update
sudo yum install supervisor

Deploy Application

There are multiple ways of moving published application files to the server including the usage of FTP Client. We will use Filezilla. To install Filezilla for free visit https://filezilla-project.org/

chmod +x alibabadotnetcore
./alibabadotnetcore

Configure Nginx

Now, we configure NGINX to serve our .NET Core web application as a reverse proxy.

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo nano /etc/nginx/nginx.conf
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;
}
sudo nginx –t
sudo nginx -s reload
cd dotnetappdir/publish
./alibabadotnetcore

Final Steps

Close the terminal and now refresh the browser window.

nginx error!
The page you are looking for is temporarily unavailable. Please try again later.
cp -r ~/dotnetappdir /var
touch /var/dotnetappdir/startscript
nano /var/dotnetappdir/startscript
#!/bin/sh
cd /var/dotnetappdir/publish
./alibabadotnetcore
chmod +x /var/dotnetappdir/startscript
nano /etc/supervisord.conf
[program:start_script_alibabadotnetcore]
command=/var/dotnetappdir/startscript
autostart=true
autorestart=true
stderr_logfile=/var/dotnetappdir/alibabadotnetcore.err.log
stdout_logfile=/var/dotnetappdir/alibabadotnetcore.out.log
sudo supervisord -c /etc/supervisord.conf

Conclusion

These are the basic steps one has to do to deploy an ASP.NET Core application after building the same on Visual Studio on Alibaba Cloud Simple Application Server with CentOS.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com