. NET Core RC2 Cross-platform deployment

Source: Internet
Author: User

Objective

Now that ASP. NET Core RC2 has been officially released, you can refer to the following links:

https://blogs.msdn.microsoft.com/dotnet/2016/05/06/net-core-rc2-improvements-schedule-and-roadmap/

https://blogs.msdn.microsoft.com/dotnet/2016/05/16/announcing-net-core-rc2/

Although documentation is available on publish (see Resources section), there are several issues with the current documentation:

    1. For Ubuntu, our company servers are CentOS, so the installation in the documentation is inappropriate for us, so here's a detailed description of how to publish the. NET Core RC2 application on the CentOS 7.x release.
    2. This document is obviously outdated, not updated in time, and is now based on RC1, not RC2. (for example, you need to configure commands, which is obviously outdated because commands settings have been removed from RC2)
    3. This document has errors, clean up the Socket resource section, should use-s instead of-F to determine whether the socket file exists.
Environment Building Operating System

CentOS 7.1 or above is required and CentOS 7.2 is used here

Install the. NET Core

You can refer to the official installation instructions for. NET Core, Https://www.microsoft.com/net/core#centos. A. NET Core installation package cannot be downloaded on a corporate virtual machine when it is actually installed. Solution:

    1. You can download it locally (which is slower) and upload it to the server root directory.
    2. Unzip the file into the/root/dotnet directory
    3. Create a connection file for/root/dotnet/dotnet files in the/usr/local/bin directory: Ln-s/root/dotnet/dotnet/usr/local/bin/dotnet
    4. Create a new directory where you can run dotnet new, dotnet Restore, dotnet Run to verify that the dotnet run environment is installed successfully
Installing Nginx

Main references Nginx Official document: https://nginx.org/en/linux_packages.html

    1. Create a/etc/yum.repos.d/nginx.repo with the following content:
      [nginx]
      name=nginx repo
      baseurl=http://nginx.org/packages/centos/7/$basearch/
      gpgcheck=0
      enabled=1
    2. Yum installs Nginx, you can install the
Installing Supervisor
  1. Install Setuptools,https://pypi.python.org/pypi/setuptools First
  2. Then run Easy_install supervisor to complete the supervisor installation.
  3. To configure supervisor, add supervisord.conf under the/etc/supervisor directory, as follows:
    [supervisord]
    logfile = /tmp/supervisord.log
    logfile_maxbytes = 50MB
    logfile_backups=10
    loglevel = info
    pidfile = /tmp/supervisord.pid
    nodaemon = false
    minfds = 1024
    minprocs = 200
    umask = 022
    user = root
    identifier = supervisor
    directory = /tmp
    nocleanup = true
    childlogdir = /tmp
    strip_ansi = false
  4. Run Supervisord under Bash, if there is no error, it means supervisor can run the
  5. Configured as a service. In the/usr/lib/systemd/system directory, add the Supervisord.service file with the following content:
    [Unit]
    Description=Supervisor daemon 

    [Service]
    Type=forking
    ExecStart=/usr/bin/supervisord
    ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
    ExecReload=/usr/bin/supervisorctl $OPTIONS reload
    KillMode=process
    Restart=on-failure
    RestartSec=42s [Install]
    WantedBy=multi-user.target
  6. Start Services, service Supervisord start
Website release
  1. Modify the URL of the site listener to be configurable so that it is easy to modify, the sample code is as follows

    IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("config.json").Build();var host = new WebHostBuilder()   .UseConfiguration(configuration)   .UseKestrel()   .UseContentRoot(Directory.GetCurrentDirectory())   .UseIISIntegration()   .UseStartup<Startup>()   .Build();   host.Run();
    {"server.urls": "http://+:5000" //注意这里用+,而不是localhost,用localhost的话只能在本机访问,其他机器不能访问}
  2. Run the dotnet Publish command to publish the Web site and upload the site to a Linux server. Then go to the Site directory, run dotnet App.dll, start the website. See if the http://serverip:5000/can respond properly in the browser. If this succeeds, it means that the. NET core-related release has been successful. Next, start configuring Nginx.

Configuring Nginx as a reverse proxy server
    1. Modify Nginx's running account as root. Edit the/etc/nginx/nginx.conf file to change the user configuration to root
    2. Modify the URL of the Web site listener to listen for UNIX sockets, as modified here: Http://unix:/var/aspnet/WebApplication1/kestrel.sock
    3. Modify the Nginx configuration, add the webserver configuration of. NET core, and change the Proxy_pass configuration as follows:
      server{
        listen 8080;
        server_name localhost;
        location / {
           proxy_pass http://unix:/var/aspnet/WebApplication1/kestrel.sock;
           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;
        }
      }
    4. Access http://serverip:8080 Verify that the response is OK
Some points to be aware of
    1. Nginx is the web site that forwards requests to. NET core through a UNIX socket, which is implemented by writing a request to a socket file. So running Nginx account needs to have write access to the socket file, I am simply configured as the root account
    2. If you encounter a shutdown outside the dotnet process, the. sock file may not be automatically cleaned up, resulting in a. Sock resource leak. At this point you need to clean down the. Sock. Otherwise, you will encounter the following error when starting the Web site:
      Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -98 EADDRINUSE address already in use) ---> Microsoft.AspNetCore.Server.Kestrel.Networking.UvException: Error -98 EADDRINUSE address already in use
    3. When listening to a socket, the directory where the. sock file is located must first exist, not exist, or it should be created first, or the following error is reported:
      Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -13 EACCES permission denied) ---> Microsoft.AspNetCore.Server.Kestrel.Networking.UvException: Error -13 EACCES permission denied
    4. If there is a problem, you can look at the Nginx Error.log and Access.log to troubleshoot the problem. Path is:/var/log/nginx
Monitoring website applications
    1. Add the web.sh Bash script under the root of the website, as follows, the main purpose of this script is to clean up the socket resources before starting the Web site, to avoid the site cannot start because the socket is not cleaned:
      if [ -S "/var/aspnet/WebApplication1/kestrel.sock" ]; then
        rm "/var/aspnet/WebApplication1/kestrel.sock"
      fi
      dotnet /root/WebApp2/WebApplication1.dll
    2. In the above supervisord.conf file, append the following configuration:
      [program:WebApplication1]
      command=bash /root/WebApp2/web.sh
      autostart=true
      autorestart=true
      stderr_logfile=/root/WebApp2/WebApplication1.err.log
      stdout_logfile=/root/WebApp2/WebApplication1.out.log
      environment=Hosting__Environment=Production
      user=root
      stopsignal=INT
    3. Restart Supervisor Service
      service supervisor stop
      service supervisor start
Resources
    1. Https://docs.asp.net/en/1.0.0-rc2/publishing/linuxproduction.html

. NET Core RC2 Cross-platform deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.