Good day, gentlemen. Today we will touch on a topic that, if we were to discuss it two years ago, we would have burned at the stake - launching an ASP.NET application under linux. Ubuntu 16.04 will be used in this article.
Environment preparation
First, add the dotnet repository:
sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list' sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893
At the output we get about the following:

')
Now we update the index of our packages:
sudo apt-get update
Next, we can simply install the dotnet package using apt-get:
sudo apt-get install dotnet-dev-1.0.4
Now we can safely start creating the application.
Create application
With the
dotnet new
command, we can create a template for our application, similar to templates from Visual Studio.
Detailed team documentation .
Currently (07.2017), the dotnet new command supports the following patterns:

We will create an ASP.NET Core web application:
dotnet new mvc
At the output, the console will give us the following message:

To make sure that the template was generated correctly, look at the contents of the folder using the
ls -la
.

All the necessary folders to build the application in place, let's get started! To begin with, restore all packages using
dotnet restore
.

Now we can build the application:
dotnet build

Run the application with:
dotnet run

The console tells us that the application has started at
localhost : 5000 /. Check:

Those wishing to learn more about how the web server works refer to the
official source .
Now kill the process by pressing Ctrl + C and publish the application with the dotnet publish command. This command packages the application and all its dependencies for further deployment (wanting intimate details
here ).
In case of problems with access rights, the sudo chmod command and this
documentation page will help you.
Deploy to server.
If we want to deploy our application under a linux server, you need to configure a proxy and demonize the application launch process. For proxying, we will use nginx to demonize the systemd process.
Brief description of the utilityCreate a proxy server.
As follows from the documentation above, with asp.net core in the box comes kestrel - the web server for asp.net applications. Why do we need a proxy server then? The answer is given on the official Microsoft page:

If you expose your application to the Internet, you must use IIS, Nginx or Apache as a reverse proxy server.
If you do not know what a reverse proxy is
The reverse proxy server receives HTTP requests from the network and sends them to Kestrel after initial processing, as shown in the following diagram:

The main reason for using a reverse proxy server is security. Kestrel is relatively new and does not yet have a full set of protection against attacks.
Another reason to use a reverse proxy server is the presence of several applications on the server using one port. Kestrel does not support sharing one port between multiple applications.
Also, using a reverse proxy server can facilitate load balancing and SSL support.
As mentioned above, we will use nginx as a proxy server.
Since we use non-IIS as a proxy server, we should add the following lines to the Configure method of the Startap.cs file.
app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });
Here we include ForwardedHeaders support for the middleware from the package. Microsoft.AspNetCore.HttpOverrides, which will insert the X-Forwarded-For and X-Forwarded-Proto headers into the Http request, which are used to determine the source IP address of the client and transfer it to the proxy server. The definition of middlelvers and practical use will also be discussed in further parts of this guide.
If your nginx is not installed, run the following command.
sudo apt-get install nginx
and run it with the command:
sudo service nginx start
Next, we need to configure nginx for proxying http requests.
Create the file /etc/nginx/sites-available/aspnetcore.conf. The sites-avalible folder tells nginx what websites are available on the current server for processing. Add the following lines to it:
server { listen 8888;
Let's create a symbolic link to aspnetcore.conf in the sites-enabled folder, which reflects the sites that are started by nginx.
sudo ln -s /etc/nginx/sites-available/aspnetcore.conf /etc/nginx/sites-enabled/aspnetcore.conf
What is a symbolic link(symbolic link - a special file in the file system, which instead of user data contains the path to the file opened when accessing this link (file) (C) Wikipedia - note auth.)
Nginx is configured to accept requests from localhost: 8888. Restart nginx with the
sudo service nginx restart
command so that the configuration files we create take effect. Checking:

Error 502 says that the server redirects us to another place and something went wrong at this place. In our case, I killed the process with our web application, which was previously launched by the dotnet run command. Because I can :)
In fact, because it is sad to run the dotnet run in the console and always keep this tab open. That is why we will demonize the process, that is, we will configure autorun after a reboot and automatic work in the background using systemd.
To do this, create a file in the directory / etc / systemd / system / with the extension .service
Let's call it kestrel-test:
sudo nano /etc/systemd/system/kestrel-test.service
And we put in it the following content:
[Unit]
Description = Example .NET Web API Application running on Ubuntu
[Service]
WorkingDirectory = / home / robounicorn / projects / asp.net / core / test-lesson / bin / Debug / netcoreapp1.1 / publish # path to the publish folder of your application
ExecStart = / usr / bin / dotnet /home/robounicorn/projects/asp.net/core/test-lesson/bin/Debug/netcoreapp1.1/publish/test-lesson.dll #path to the published dll
Restart = always
RestartSec = 10 # Restart the service after 10 seconds when the application is more beautiful
SyslogIdentifier = dotnet-example
User = root # user to run your service as
Environment = ASPNETCORE_ENVIRONMENT = Production
[Install]
WantedBy = multi-user.target
Now turn on and run the service with the following commands:
sudo systemctl enable kestrel-test.service sudo systemctl start kestrel-test.service
Check service status:
sudo systemctl status kestrel-test.service
If everything was done correctly, this command will give us the following:

Follow the link again:

Well, that's the hat. Thanks for attention!