"Selfless sharing: ASP. NET Core Project Combat (chapter tenth)" Launch project to Linux running CORE project

Source: Internet
Author: User
Tags dotnet terminates

"Selfless sharing: ASP. NET CORE Project Combat" Catalog Index

Brief introduction

  

The biggest highlight of the ASP. NET Core is the cross-platform, I built a CentOS7 on my Computer (Win7) with a virtual machine to demonstrate how the projects on our Windows publish the project to run on Linux. I have a program built on windows, we mainly show how to run on Linux, so let's not dwell on these, you can create a new Web project to try.

  

Installing the. NET Core SDK for CentOS

Microsoft has detailed installation commands and can refer to the install. NET Core SDK

① installation Libicu dependency: sudo yum install libunwind Libicu

  

Installation succeeded:

  

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

③ extract: sudo mkdir-p/opt/dotnet && sudo tar zxf dotnet.tar.gz-c/opt/dotnet

  

④ Creating a connection: sudo ln-s/opt/dotnet/dotnet/usr/local/bin

  

Test whether the installation was successful

by dotnet--info to see, the following interface appears, indicating success:

  

Test project Run

Note: Friends who are not familiar with Linux please note that the Liunx path is directory-case-sensitive.

① Create a new directory: mkdir TestApp

  

② Entry directory: CD TestApp

  

③ Create a new Default Web project: Dotnet new-t Web

  

④ Recovery package: dotnet Restore

This time may be longer, look at the speed, we have to wait patiently.

  

⑤ Let's run it: dotnet run

  

Publish our project to Linux

You can use FTP and other ways to publish the file to the server, I publish it directly to the local folder, and then sent to the server via FileZilla.

There are several ways to publish a project, such as using the dotnet Publish command, where we use VS:

First, we open Project.json to add several releases of the runtime (this step can not, if not configured here, then the release is any, we will talk about later)

  

  

Then, right-click on our project and select Publish:

  

Create a new publish profile, publish method Select File system:

  

setting, we select Target Runtime: centos.7-x64 (above, we talked about adding several runtimes in Project.json, if there is no one, then there is no option, only one any)

  

Then click Publish and we upload the published file to CentOS: I put it in the Wwwroot directory.

  

  

Let's compile our files: dotnet XXX.dll

Here's an error for everyone: My wkmvc.dll is in the Wwwroot directory, so I execute the command (note the path case, Linux case-sensitive, we mentioned above) dotnet Wwwroot/wkmvc.dll

  

  

The display succeeds, we open the browser, enter localhost:5000

  

is blank, we modify our program, output a log to see:

  

What is the reason for this? Why didn't you find index.cshtml? Please pay attention again to our content root path, the answer is that she does not have access to the root of the operation, we will compare our installation process to test the success of the Web and our web Content root path

  

Let's try it: we go into the wwwroot directory and execute dotnet wkmvc.dll

  

  

We open the browser again, enter localhost:5000

  

OK, let's go to the login page of our area to test it:

  

  

No problem, the project will run successfully, but in practice, we may have to install Nginx, configure our FireWall and configure the daemon service Supervisor and so on, which is not demonstrated here.

  

Since everyone is not very understanding, or online to find information more cumbersome, for Nginx firewall and supervisor configuration, I will step by step to give you a demonstration.

Installing Nginx

Nginx installation is relatively simple, on the three command:

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



②RPM-IVH nginx.rpm

  

  

③yum Install Nginx

 

Starting Nginx:systemctl start Nginx

Set boot start (Linux down, restart will automatically run Nginx does not need to connect to enter command): Systemctl enable Nginx

 

   

Configuring firewalls

① Open 80 port: Firewall-cmd--zone=public--add-port=80/tcp–permanent

② Restart the firewall to make the configuration effective immediately: Systemctl restart FIREWALLD

   

  

Let's test if Nginx can access:

  

Configuring Nginx forwarding for ASP. NET Core Applications

Modify Nginx default.conf file (directory:/ect/nginx/conf.d/default.conf)

  

Replace the content with (configure Nginx forwarding):

  

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 Overwrite default.conf file

Reload, immediate effect: Nginx–s Reload

Let's test it out:

  

  

What the hell is this? This problem is caused by the SELinux protection mechanism and we need to add nginx to the SELinux whitelist.

We solve this problem by order:

①yum Install Policycoreutils-python

  

 

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

  

  

③sudo semodule-i mynginx.pp

We try again to access:

  

Supervisor Guardian Service

Why do you want this thing?

Let's take a look at our existing problem: ① We're running our application in the demo above with dotnet ***.dll or dotnet run, which is our ASP. NET core application running in the shell, If you close the shell, you will notice that the ASP. NET core application is turned off

Not accessible to the app.

② if the ASP. NET core process terminates unexpectedly, it is necessary to manually boot into the shell.

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

So to solve these problems, we have to implement a function: if ASP. NET Core terminates unexpectedly, then we want to restart automatically, if the server server restarts, we want to have a similar script command, the automatic execution of the dotnet command.

Now there's a Python-based tool Supervisor can solve our problems:

① Installing Supervisor:

1.1:yum Install Python-setuptools

1.2:easy_install Supervisor

② Configuration Supervisor:

Mkdir/etc/supervisor

echo_supervisord_conf >/etc/supervisor/supervisord.conf

  

Modify supervisord.conf file (directory:/etc/supervisor/supervisord.conf)

At the end of the file is a: [include] files = Relative/directory/*.ini

Modified to: [include] files = conf.d/*.conf

If the service is already started, you need to use the Supervisorctl Reload command for the new configuration to take effect, of course, we do not start here, so we do not need this step.

③ Configuring the Supervisor for ASP. NET Core Applications:

  

Create a conf file because our app is called WKMVC, so I created a wkmvc.conf file, created locally with Notepad, and then saved as wkmvc.conf:

[PROGRAM:WKMVC]

Command=/usr/bin/dotnet/root/wwwroot/wkmvc.dll
directory=/root/wwwroot/
Autostart=true
Autorestart=true
Stderr_logfile=/var/log/wkmvc.err.log
Stdout_logfile=/var/log/wkmvc.out.log
Environment=aspnetcore_environment=production
User=root
Stopsignal=int

  

Upload to:/etc/supervisor/conf.d/directory, then execute:

Supervisord-c/etc/supervisor/supervisord.conf

Ps-ef | grep WKMVC

 

   

  

④ Configuration 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=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"

Systemctl Enable Supervisord

  

Verify that you are booting up for boot: systemctl is-enabled Supervisord

"Selfless sharing: ASP. NET Core Project Combat (chapter tenth)" Launch project to Linux running CORE project

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.