Deploying an ASP. NET core application to production (CENTOS7)

Source: Internet
Author: User
Tags dotnet

Read Catalogue

    • Environment description
    • Prepare your ASP. NET Core Application
    • Installing CentOS7
    • Install the. NET Core SDK for CentOS7.
    • Deploying an ASP. NET Core Application
    • Configure Nginx
    • Configuring the Daemon Service (Supervisor)

This period of time is using rabbit RPC to restructure the company's system (related), and recent related tests (logic tests, stress tests) have been completed, near deployment to the online production environment and thus the implementation of the ASP. NET core application deployment on CentOS, Let's share with you today how to deploy the ASP. NET core application to CentOS on a production standard.

Back to Directory environment description

Server System: CentOS 7.2.1511

Related tools: Xshel, Xftp

Server Software software:. Netcore, Nginx, supervisor, Policycoreutils-python

Back to catalog prepare your ASP. NET Core Application

First publish your application in a portable mode.

PS: Here I use an empty Web project to demonstrate, because this article focuses on the deployment of the production environment, regardless of the application.

The command is:dotnet publish–c Release

Specifically: embracing. NET Core, how to develop cross-platform applications and deploy to Ubuntu, this blog post describes publishing Web apps in a portable and self-hosted fashion.

Make sure that the publishing app can run on Windows to reduce subsequent issues.

Why not deploy in a self-hosted way?

Deploying from a host is a lot easier, why is the production environment being published in a portable way?

Cause 1: Performance is lower than portable (primary).

Reason 2: Microsoft gives advice (times).

Accreditations, there is a picture of the truth.

Reference Address: Https://docs.microsoft.com/zh-cn/dotnet/articles/core/app-types

So, since it is used in the production environment, of course, we have to pursue higher performance.

Back to directory Installation CentOS7

This does not elaborate, online tutorials a lot, this way I used Hyper-V to virtualize the CentOS7.

Go back to the directory to install the. NET Core SDK for CentOS7.

sudo yum install libunwind libicu(Installation Libicu dependent)

Curl-ssl-o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=809131 (Download SDK compression pack)

sudo mkdir-p/opt/dotnet && sudo tar zxf dotnet.tar.gz-c/opt/dotnet (uncompressed)

sudo ln-s/opt/dotnet/dotnet/usr/local/bin (create link)

Enter Dotnet-info to see if the installation was successful

If you can do this, the. NET Core SDK installation is successful.

References: Https://www.microsoft.com/net/core#centos

Back to directory Deploy ASP. NET Core Application

Upload the previously published folder to/home/wwwroot/.

This way I used the xftp to upload the file.

Check if you can run

Command: Dotnet/home/wwwroot/webapplication1/webapplication1.dll

If this message appears, it will run successfully.

We are unable to access this page at this time and we need to deploy a Web container to forward it.

Back to directory configuration Nginx installation Nginx

Curl-o nginx.rpm http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

sudo rpm-ivh nginx.rpm

sudo yum install Nginx

Installation is successful!

Input:sudo systemctl start nginx to start Nginx.

Input:sudo systemctl enable Nginx to set the Nginx boot (Linux down, restart will automatically run Nginx does not need to connect to enter the command).

Configuring firewalls

command: sudo firewall-cmd--zone=public--add-port=80/tcp--permanent(open 80 port)

command: sudo systemctl restart Firewalld(reboot the firewall for immediate configuration)

Test if Nginx can be accessed.

Configuring Nginx forwarding for ASP. NET core applications

Modify the /etc/nginx/conf.d/default.conf file.

Replace the contents of the file with

server {
Listen 80;
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;
}
}

Upload to CentOS for overlay.

Execute:sudo nginx-s reload make it effective immediately

Running an ASP. NET Core Application

Command: Dotnet/home/wwwroot/webapplication1/webapplication1.dll

Try to access it again.

Want to cry heart all have ... After a follow-up understanding, this problem is due to the SELinux protection mechanism, we need to add nginx to SELinux whitelist.

Next we have some commands to solve this problem.

sudo yum install Policycoreutils-python

sudo cat/var/log/audit/audit.log | grep Nginx | grep denied | Audit2allow-m Mynginx

sudo semodule-i mynginx.pp

Try to access again.

The deployment is basically complete.

Back to Directory Configuration Daemon Service (Supervisor)

There are currently three issues

Issue 1: TheASP. NET core application runs in the shell, and if you close the shell you will notice that the ASP. NET core application is turned off, which makes the app inaccessible, which is certainly not what we would like to encounter, and the production environment is 0 tolerant of this situation.

Issue 2: if the ASP. NET core process terminates unexpectedly, it is often not timely to have to start the shell again.

Issue 3: If the server is down or needs to be restarted we still need to connect to the shell to boot.

To solve this problem, we need to have a program to monitor the status of the ASP. Restart immediately when the application stops running. Here we use the Supervisor tool, which supervisor developed using Python.

Installing Supervisor

sudo yum install Python-setuptools

sudo easy_install supervisor

Configure Supervisor

sudo mkdir/etc/supervisor

echo_supervisord_conf >/etc/supervisor/supervisord.conf

Modify the supervisord.conf file to configure the tail of the file

Revision changed to

PS: If the service is started, modify the configuration file to use the "supervisorctl Reload" command to make it effective

Configure the daemon for ASP. NET core applications

Create a webapplication1.conf file with the following content:

[Program:webapplication1]
Command=dotnet WebApplication1.dll; command to run the program
directory=/home/wwwroot/webapplication1/; Directory for command execution
Autorestart=true; Program quits unexpectedly if auto-restart
Stderr_logfile=/var/log/webapplication1.err.log; Error log file
Stdout_logfile=/var/log/webapplication1.out.log; Output log file
Environment=aspnetcore_environment=production; Process Environment variables
User=root; User identity for Process execution
Stopsignal=int

Copy the file to: "/etc/supervisor/conf.d/webapplication1.conf"

Run Supervisord to see if it takes effect

Supervisord-c/etc/supervisor/supervisord.conf

Ps-ef | grep WebApplication1

If the dotnet WebApplication1.dll process is present, the operation succeeds and is accessed using a browser.

This is the configuration for the gatekeeper of the ASP. NET Core application.

Configuring Supervisor Boot-up

Create a new "Supervisord.service" file

# Dservice for Systemd (CentOS 7.0+)
# by Et-cs (Https://github.com/ET-CS)
[Unit]
Description=supervisor Daemon

[Service]
Type=forking
Execstart=/usr/bin/supervisord-c/etc/supervisor/supervisord.conf
Execstop=/usr/bin/supervisorctl shutdown
Execreload=/usr/bin/supervisorctl Reload
Killmode=process
Restart=on-failure
Restartsec=42s

[Install]
Wantedby=multi-user.target

Copy the file to: "/usr/lib/systemd/system/supervisord.service"

Execute command: Systemctl enable Supervisord

Execute command: Systemctl is-enabled supervisord #来验证是否为开机启动

Test

Deploying an ASP. NET core application to production (CENTOS7)

Related Article

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.