How to deploy ASP.NET Core Linux project

Outline

This article documents how to build .Net Core environment for Linux server and ASP.NET Core website project deployed to the server, and use Nginx as a reverse proxy server, with Supervisor process to achieve the project's management.
Original link: How to deploy ASP.NET Core Linux project

Here Insert Picture Description


First, the project configuration and release

1. The project configuration (optional)

ASP.Net Core project default listening port 5000, the port if the occupation occurs, this step can be defined from the port.

1.1 configuration files

New configuration file hosting.json in the project root directory, where 8080 is the custom port number example:

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

1.2 Modify Program.cs

public static void Main(string[] args)
        {
            //CreateHostBuilder(args).Build().Run();
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("hosting.json", optional: true)
                .Build();
            WebHost.CreateDefaultBuilder(args)
                .UseConfiguration(config)
                .UseStartup<Startup>()
                .Build()
                .Run();
        }

2. Project Releases

Right in the Solution Explorer your project, select the publication , use Folder mode to publish, find release build publish folder, the contents uploaded to the corresponding project on the server empty directory by Xftp or otherwise.

Here Insert Picture Description

Second, build .Net Core environment

1. Register a signature key Microsoft

sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

2. Install the .Net SDK

#验证 yum
sudo yum update
#安装 .Net Core
sudo yum install dotnet-sdk-3.1 -y

3. Verify the installation

dotnet --version

Here Insert Picture Description

Three, Nginx Installation and Configuration

nginx reverse proxy to do, nginx monitor port 80, when the request url to access the server through port 80, it will be forwarded to a custom port project in conjunction with the corresponding domain name accessed by nginx.

1. Install Nginx

Click on the link epel-release-7-12.noarch.rpm direct download,

Or your own go https://dl.fedoraproject.org/pub/epel download version epel need and uploaded to the server, in order to complete the installation using the following command in the directory where the epel rpm package:

# 安装epel
rpm -ivh epel-release-7-9.noarch.rpm
# 安装nginx
yum install nginx
# 启动nginx
systemctl start nginx

Use the following command to test Nginx is operating properly:

curl http://127.0.0.1

2. Configure Nginx

Nginx configuration file default location is /etc/nginx/nginx.conf, complete the configuration according to their needs, the following is my configuration reference:

server {
        listen       80;
        server_name  此处为你的域名;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass http://localhost:此处为你的项目监听端口;
        }

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

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

After configuration reload the configuration file:

nginx -s reload

Nginx is enabled at startup:

systemctl enable nginx service

Fourth, launch the website

At this point, you can already start site and access from outside the network. On start-up mode, you can Xshell session window, released prior to entering into the project directory on the server, the command dotnet 你的项目名.dllto start the site, but so long as Ctrl + C or disconnect sessions, project process was over, so you can use do temporary test. To make the site continued to run down, you can use one of two methods.

nohup start

nohup dotnet 你的项目名.dll

As a run up your site, and the process will not disconnect the session was closed, no configuration is simple and it has the advantage, but if you want to close trouble, you need to ps -ef | grep dotnetfind the corresponding process id and then it kill. Another disadvantage is that, if the server is restarted, or your project error unexpectedly quit, then your project is not automatically restart. So if you mind, it is recommended that the following methods.

Supervisor Guardian

Supervisor is Microsoft recommended a process management tool that allows to specify the program runs in the background and it will automatically restart when the process is abnormal exit.

Installation supervisor

yum install python-setuptools
easy_install supervisor

Configuration supervisor

mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
mkdir /etc/supervisor/conf.d
  • Modify supervisor.conf end of the file include the following:
;[include]
;files = conf.d/*.conf
  • Create profiles for projects under the /etc/supervisor/conf.d catalog:vim /etc/supervisor/conf.d/你的项目名.conf
[program:你的项目名]
command=dotnet 你的项目名.dll 
directory=你的项目目录
autorestart=true
stderr_logfile=/var/log/你的项目名.err.log
stdout_logfile=/var/log/你的项目名.out.log 
environment=ASPNETCORE_ENVIRONMENT=Production 
user=root
stopsignal=INT
  • Startup configuration file:
supervisord -c /etc/supervisor/supervisord.conf

Related Commands

  • Reload the configuration file

    supervisorctl reload
    
  • View the guardian of the status of all processes

    supervisorctl status all
    
  • Close all daemons

    supervisorctl stop all
    

So far, ASP.NET Core project deployment is complete.

Published 26 original articles · won praise 24 · views 30000 +

Guess you like

Origin blog.csdn.net/iSunwish/article/details/105162027