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

An example of Building .NET Core2.0+Nginx+Supervisor Environment under Centos7 system

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces an example of building a .NET Core2.0+Nginx+Supervisor environment under the Centos7 system, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article.

A brief introduction to Linux .NET Core

For a long time, Microsoft has only provided .NET support for its own platform, which means that the support of this "theoretically" cross-platform framework on Linux and macOS can only be provided by third-party projects (such as Mono .NET).

Until Microsoft launched a fully open source .NET Core. This open source platform is compatible with .NET Standard and provides a fully consistent API on Windows, Linux, and MacOS. Although this small .NET framework is only a subset of the standard .NET, it is already quite powerful.

On the one hand, this compact framework allows some functional applications to run on three platforms at the same time (like some functional Python scripts). On the other hand, it also allows server operators to deploy ASP .NET services on Linux servers (especially for servers that are difficult to run Windows Server).

Official website reference: https://www.microsoft.com/net/core#linuxcentos

II. Pre-deployment preparation for Linux .NET Core2.0 environment

1. Environment description:

Server system: CentOS 7.2.1511

two。 Prepare before installation (turn off firewall, turn off selinux)

1) close firewall:

Systemctl stop firewalld.service # stop firewallsystemctl disable firewalld.service # prevent firewall from starting firewall-cmd-- state # check the default firewall status (show notrunning when turned off, show running when turned on)

2) close selinux

Sed-I "s/SELINUX=enforcing/SELINUX=disabled/g" / etc/selinux/config

Check the revised file as follows:

[root@localhost] # cat / etc/selinux/config # This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing-SELinux security policy is enforced.# permissive-SELinux prints warnings instead of enforcing.# disabled-No SELinux policy is loaded.SELINUX=disabled# SELINUXTYPE= can take one of three two values:# targeted-Targeted processes are protected,# minimum-Modification of targeted policy. Only selected processes are protected. # mls-Multi Level Security protection.SELINUXTYPE=targeted

3) restart Centos

Reboot

Third, Centos deploys the .NET Core2.0 environment

1. Add DOTNET products

Before installing the .NET core, you need to register for the Microsoft product feed. It only needs to be done once. First, register the Microsoft signature key, and then add the Microsoft product feed.

Rpm-- import https://packages.microsoft.com/keys/microsoft.asc sh-c 'echo-e "[packages-microsoft-com-prod] nname=packages-microsoft-com-prod nbaseurl= https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prodnenabled=1ngpgcheck=1ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > / etc/yum.repos.d/dotnetdev.repo'

two。 Install the .NET Core SDK

Before taking the next step, remove any previous preview versions of .NET from your system.

The following command updates the list of products for installation, installs the components required by the .NET core, and then installs the .NET core SDK.

Yum updateyum install libunwind libicu-yyum install dotnet-sdk-2.0.0-y

3. Check whether dotnet is installed successfully and check the version

Dotnet-infodotnet-version

4. Test the .NET Core2.0 environment

1. Initialize a test environment under the home directory and output the "Hello World" content (test method 1, negligible)

Cd / homedotnet new console-o hwappcd hwappdotnet run

The output empty content is as follows:

[root@localhost hwapp] # dotnet runHello World!

two。 Upload the instance page of .net core for testing (test method 2, recommended)

Under Centos. Net core 2 environment test case (upload it to / home directory or custom directory)

Download address:

Http://down.51cto.com/data/2334968

Execute the following command

Cd / home/WebApplication1dotnet restore / / if you have used test mode 1, you need to execute this command to reload the current new website file dotnet run

After operation, the figure is as follows:

Access the test page through IE

5. Install and configure nginx to forward ASP.NET Core applications

1. Install the Nginx environment

[root@localhost ~] # curl-o nginx.rpm http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm[root@localhost ~] # rpm-ivh nginx.rpm [root@localhost ~] # yum install nginx-y

Enter: systemctl start nginx to start nginx.

[root@localhost ~] # systemctl start nginx

Enter: systemctl enable nginx to set the startup of nginx (linux downtime, restart will automatically run nginx, no need to connect to enter commands)

[root@localhost] # systemctl enable nginxCreated symlink from / etc/systemd/system/multi-user.target.wants/nginx.service to / usr/lib/systemd/system/nginx.service.

two。 Check whether you can access it through iE

[root@localhost nginx-1.8.1] # ps-ef | grep nginxroot 14626 1 0 08:47? 00:00:00 nginx: master process nginxnginx 14627 14626 0 08:47? 00:00:00 nginx: worker processroot 14636 3269 0 08:49 pts/1 00:00:00 grep-- color=auto nginx

Operation commands commonly used in nginx

Systemctl start nginx.service # start the nginx service

Systemctl enable nginx.service # set Boot self-boot

Systemctl disable nginx.service # stop booting self-starting

Systemctl status nginx.service # View the current status of the service

Systemctl restart nginx.service # restart the service

Systemctl list-units-type=service # View all started services

4. Firewall configuration (write rules are required if the system has a 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)

5. 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:88; 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;}}

Reload nignx

[root@localhost nginx] # nginx-s reload

Configuration of nginx has been completed

6. Open dotnet run for testing

[root@localhost ~] # cd / home/WebApplication1/ [root@localhost WebApplication1] # dotnet runUsing launch settings from / home/WebApplication1/Properties/launchSettings.json...Hosting environment: DevelopmentContent root path: / home/WebApplication1Now listening on: http://[::]:88Application started. Press Ctrl+C to shut down.

Access through IP port 80

6. Configure 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.

1. Install Supervisor

[root@localhost /] # yum install python-setuptools-y [root@localhost /] # easy_install supervisor

two。 Configure Supervisor

[root@localhost /] # mkdir / etc/supervisor [root@localhost /] # echo_supervisord_conf > / etc/supervisor/supervisord.conf

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

[root@localhost /] # vi / etc/supervisor/supervisord.conf

Put the last two lines inside:

; [include]; files = relative/directory/*.ini

Change to

[include] files = conf.d/*.conf

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

3. Configure guardians for ASP.NET Core applications

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

[root@localhost /] # vi WebApplication 1.confo [program: WebApplication1] command=dotnet WebApplication1.dll; command directory=/home/WebApplication1/ that runs the program; directory where the command is executed; whether stderr_logfile=/var/log/WebApplication1.err.log is automatically restarted when the program exits 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"

[root@localhost /] # mkdir / etc/supervisor/conf.d [root@localhost /] # cp WebApplication1.conf / etc/supervisor/conf.d/

Run supervisord to see if it works

[root@localhost /] # supervisord-c / etc/supervisor/supervisord.confsupervisord-c / etc/supervisor/supervisord.conf [root@localhost /] # ps-ef | grep WebApplication1root 29878 29685 0 09:57? 00:00:00 dotnet WebApplication1.dllroot 29892 29363 0 09:57 pts/3 00:00:00 grep-- color=auto 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.

Common operations of Supervisor daemons

[start supervisord]

After ensuring that the configuration is correct, you can start supervisor's server-side supervisord on each host using the following command

Supervisord

[stop supervisord]

Supervisorctl shutdown

[reload configuration file]

Supervisorctl reload

7. Configure Supervisor to boot

Create a new "supervisord.service" file

[root@localhost /] # vi supervisord.service# 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"

[root@localhost /] # cp supervisord.service / usr/lib/systemd/system/

Execute command: systemctl enable supervisord

[root@localhost /] # systemctl enable supervisordCreated symlink from / etc/systemd/system/multi-user.target.wants/supervisord.service to / usr/lib/systemd/system/supervisord.service.

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

[root@localhost /] # systemctl is-enabled supervisord

Restart the system to see if it can be accessed successfully.

[root@localhost /] # reboot

Thank you for reading this article carefully. I hope the article "an example of building a .NET Core2.0+Nginx+Supervisor environment under the Centos7 system" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

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

Servers

Wechat

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

12
Report