Publish asp.net core website to Linux server

Time:2021-11-20

For a long time, applications developed with. Net can only run on the windows platform. At present, due to cost considerations, domestic booming Internet companies use a large number of free Linux platforms, which makes. Net have unique skills, but can not get a large display space. The. Net platform is considered to be only suitable for developing enterprise internal application systems.

On June 27, 2016, Microsoft officially released. Net core 1.0, asp.net 1.0 and Entity Framework core 1.0, taking all three operating systems of windows, OS X and Linux Net core, as a new generation of cross platform and open source. Net platform, has attracted much attention. Some people say that the spring of. Net programmers is coming.

This article will introduce how to publish the website of asp.net core to Linux server.

Environmental preparation

The operating system and software versions used in this paper are as follows:

  1. Visual studio 2017 Enterprise Edition
  2. .NET Core 1.1
  3.  CentOS 7 X64

Create and publish asp.net core web site projects

1. Create project

Open vs2017, create a new project, and select asp.net core web application (. Net core)

Select a web application template.

Directly F5 test whether the website is normal.

2. Add URL URL configuration file

  Project default http://localhost:5000 We can add a configuration file to modify the URL address at any time.

  Add a hosting.json file in the root directory of the project. The contents of the file are as follows (192.168.57.7 is the server IP):


{
 "server.urls": "http://192.168.57.7:8080"
}

Edit the program.cs file to read as follows:


    public static void Main(string[] args)
    {
      var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("hosting.json", optional: true)
        .Build();

      var host = new WebHostBuilder()
        .UseKestrel()
        .UseConfiguration(config)
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseApplicationInsights()       
        .Build();

      host.Run();
    }

3. Release Project 

Right click Project – publish and select Folder mode.

Compress the published publishoutput folder into ZIP format and upload it to CentOS server.

Installing. Net core

1. Download the latest. Net core version

Official website download address:https://www.microsoft.com/net/download/linux

The version I use is dotnet CentOS x64.1.1.1.tar.gz.

2. Upload and unzip the downloaded installation file.

$# create dotnet folder
$ mkdir ~/dotnet
$# copy the dotnet installation file to the dotnet folder
$ cp dotnet-centos-x64.1.1.1.tar.gz ~/dotnet
$# unzip the installation file
$ tar -xzf ~/dotnet/dotnet-centos-x64.1.1.1.tar.gz
$# add soft connection, which can be used globally
$ ln -s ~/dotnet/dotnet /usr/local/bin
$# test whether the installation is successful. If successful, the version number will be displayed
$ dotnet –version

Launch site

$# unzip the previously uploaded compressed website files. If unzip is not installed, run Yum install - y unzip zip to install
$ unzip ~/dotnet/PublishOutput.zip
$# turn off the firewall first
$ systemctl stop firewalld.service
$# launch site
$ cd PublishOutput
$ dotnet TestAspNetCoreWeb.dll
$if the error failed to bind to coreclr is reported, run Yum install - y libunwind

Started successfully, can access http://192.168.57.7:8080 It’s open.

Install configuration daemon (supervisor)

Use the supervisor to monitor the asp.net core website application so that the website can run continuously, otherwise the website will stop after exiting the shell.

$# install Supervisor
$ yum install python-setuptools
$ easy_install supervisor
$# configure Supervisor
$ mkdir /etc/supervisor
$ echo_supervisord_conf > /etc/supervisor/supervisord.conf

Modify the supervisor.conf file and modify the final contents of the file as follows:

Create the directory conf.d, and create the file testaspnetcoreweb.conf in the directory

The contents of the document are as follows:


[program:TestDotNetCoreWeb]
command=dotnet TestAspNetCoreWeb.dll 
directory=~/dotnet/PublishOutput
autorestart=true
stderr_logfile=/var/log/TestDotNetCoreWeb.err.log
stdout_logfile=/var/log/TestDotNetCoreWeb.out.log 
environment=ASPNETCORE_ENVIRONMENT=Production 
user=root
stopsignal=INT

Run Supervisor and see if the process is effective.


$ supervisord -c /etc/supervisor/supervisord.conf
$ ps -ef | grep TestDotNetCoreWeb

If the configuration file is modified, use the command supervisorctl reload to reload.

Installing and configuring nginx

visithttp://dl.fedoraproject.org/pub/

Download the appropriate version of EPEL and upload it to the server.

  For example:http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm

$# install EPEL
$ rpm -ivh epel-release-7-9.noarch.rpm
$# install nginx
$ yum install nginx
$# start nginx
$ systemctl start nginx
$# add nginx to the white list of SELinux, otherwise 502 error will be reported.
$ yum install policycoreutils-python
$ cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
$ semodule -i mynginx.pp
$# test whether nginx is normal
$ curl http://127.0.0.1

Modify the server part in the configuration file / etc / nginx / nginx.conf to the following content, and configure the website before nginx listening.


server {
    listen 80 ;
    location / {
       proxy_pass http://192.168.57.7:8080;
    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;
    }

    error_page 404 /404.html;
      location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
      location = /50x.html {
    }
  }

Reload the configuration file after $# modification
$ nginx -s reload

It can be used after configuration http://192.168.57.7 Visit the website.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.