Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy ASP.NETCore applications to production environments in CentOS7

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

It is believed that many inexperienced people don't know what to do about how to deploy ASP.NETCore applications to the production environment in CentOS7. Therefore, this paper summarizes the causes and solutions of the problems. Through this article, I hope you can solve this problem.

During this period of time, I have been using a system of Rabbit RPC refactoring company (related to Wechat), and recently the relevant tests (logic test, stress test) have been completed, which is close to being deployed to an online production environment, thus messing with the deployment plan of ASP.NET Core applications on CentOS. Today, I will share with you how to deploy ASP.NET Core applications on CentOS with production standards.

Environment description

Server system: CentOS 7.2.1511

Related tools: Xshel, Xftp

Server software: .netcore, nginx, supervisor, policycoreutils-python

Prepare your ASP.NET Core application

First release your application in a portable mode.

Ps: here I use an empty Web project to demonstrate, because this article focuses on the deployment of a production environment, independent of the application.

The command is:

Dotnet publish-c release

Make sure the release app runs on windows to reduce subsequent problems.

Why not deploy on a self-hosting basis?

It is much easier to deploy from the host release, so why should the production environment publish in a portable way?

Reason 1: the performance is lower than the portable one (main).

Reason 2: suggestions given by Microsoft (times).

There is no evidence of words, there is a picture and the truth.

Reference address: https://docs.microsoft.com/zh-cn/dotnet/articles/core/app-types

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

Install CentOS7

I won't go into details about this. There are many online tutorials. Here I use Hyper-V to virtualize CentOS7.

Install the .NET Core SDK for CentOS7.

Sudo yum install libunwind libicu

(install libicu dependency)

Curl-sSL-o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=809131 (download sdk package) sudo mkdir-p / opt/dotnet & & sudo tar zxf dotnet.tar.gz-C / opt/dotnet (decompress) sudo ln-s / opt/dotnet/dotnet / usr/local/bin (create link)

Enter dotnet-info to see if the installation is successful

If it can be executed, it indicates that the .NET Core SDK installation is successful.

Reference: https://www.microsoft.com/net/core#centos

Deploy ASP.NET Core applications

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

Here I use Xftp to upload files.

Check to see if it can run

Command:

Dotnet / home/wwwroot/WebApplication1/WebApplication1.dll

If these messages appear, it indicates that the operation is successful.

We cannot access this page at this time, so we need to deploy a web container to forward it.

Configure Nginx

Install Nginx

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

Rpm-ivh nginx.rpmyum install nginx

Installation successful!

Enter: systemctl start nginx to start nginx.

Enter: systemctl enable nginx to set the startup of nginx (when linux is down and restart will run nginx automatically, you don't need to connect and enter commands).

Configure the firewall

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

Command: systemctl restart firewalld (restart the firewall to make the configuration take effect immediately)

Test whether nginx is accessible.

Configure nginx to forward 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 overwriting.

Execute: nginx-s reload to make it effective immediately

Run the ASP.NET Core application

Command:

Dotnet / home/wwwroot/WebApplication1/WebApplication1.dll

Try to visit again at this time.

I have a heart that wants to cry. As we later learned, this problem is caused by the SELinux protection mechanism, and we need to add nginx to the whitelist of SELinux.

Next, we use some commands to solve this problem.

Yum install policycoreutils-pythonsudo cat / var/log/audit/audit.log | grep nginx | grep denied | audit2allow-M mynginxsudo semodule-I mynginx.pp

Try to access it again.

At this point, the deployment is basically completed.

Configure the daemon service (Supervisor)

There are three problems at present.

The problem 1:ASP.NET Core application runs in shell, and if you close shell, you will find that the ASP.NET Core application is closed, making the application inaccessible, which is certainly something we do not want to encounter, and the production environment has zero tolerance for this situation.

Question 2: if the ASP.NET Core process terminates unexpectedly, it needs to be restarted artificially by connecting to the shell, which is often not timely enough.

Question 3: if the server is down or needs to be rebooted, we still need to connect to shell to start.

To solve this problem, we need a program to monitor the state of the ASP.NET Core application. Restart immediately when the application stops running. Here we use the tool Supervisor, which is developed by Supervisor using Python.

Install Supervisor

Yum install python-setuptoolseasy_install supervisor

Configure Supervisor

Mkdir / etc/supervisorecho_supervisord_conf > / etc/supervisor/supervisord.conf

Modify the supervisord.conf file to change the configuration at the end of the file

Modify to

Ps: if the service is started, modify the configuration file with the "supervisorctl reload" command to make it effective

Configure guardians for ASP.NET Core applications

Create a WebApplication1.conf file, which is roughly as follows

[program:WebApplication1] command=dotnet WebApplication1.dll; command directory=/home/wwwroot/WebApplication1/ that runs the program; directory autorestart=true where the command is executed; whether the program automatically restarts stderr_logfile=/var/log/WebApplication1.err.log when exiting unexpectedly; error log file stdout_logfile=/var/log/WebApplication1.out.log; output log file environment=ASPNETCORE_ENVIRONMENT=Production; process environment variable user=root; user identity stopsignal=INT of the process execution

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

Run supervisord to see if it works

Supervisord-c / etc/supervisor/supervisord.confps-ef | grep WebApplication1

If there is a dotnet WebApplication1.dll process, it means that it is running successfully, and you are using a browser to access it.

At this point, the daemon for the ASP.NET Core application is configured.

Configure Supervisor boot

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=forkingExecStart=/usr/bin/supervisord-c / etc/supervisor/supervisord.confExecStop=/usr/bin/supervisorctl shutdownExecReload=/usr/bin/supervisorctl reloadKillMode=processRestart=on-failureRestartSec=42s [Install] WantedBy=multi-user.target

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

Execute command: systemctl enable supervisord

Execute the command: systemctl is-enabled supervisord # to verify that it is booting up

test

After reading the above, have you learned how to deploy ASP.NETCore applications to a production environment in CentOS7? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report