ASP.NET Core 2.0 : Nine. Cross-Platform Deployment from Windows Publishing to CentOS

  This article talks about how to develop and publish with VS on Windows, and then deploy it to CentOS. For some of us who often browse on Windows, CentOS is really troublesome to use. There is an official MSDN article about it ( link ). According to the above example of MSDN, I used vs to create a hellomvc project. I still stepped on a lot of pits. I will talk about the whole process and the pits encountered. I hope it will help friends in need. help. ( ASP.NET Core series catalog )

  The main content of this article:

  1. Tool preparation

  2. Install the .NET Core environment on CentOS

  3. Publish the project with VS on Windows

  4. Project run test

  5. Install and configure Apache

  6. Create a service management application

  7. Other precautions

  8. Standalone Deployment (SCD)

 

  Schematic:

  

  Recently, I got an ECS game on Alibaba Cloud. Since .NET Core is cross-platform, I chose a CentOS system, and then stepped on the pit to start.

1. Preparation of tools

  Putty: Alibaba Cloud provides a command-line tool for remotely operating CentOS through web pages. I can't find how to paste it, so it's not easy to use. This is a small command line software, which also saves the step of logging in to the Alibaba Cloud console every time. Link

  FileZila: sftp tool, used to get the distribution package generated on Windows to CentOS. Link

2. Install the .NET Core environment on CentOS

  There are two ways to install the .NET Core environment, SDK and Runtime, which are different from Java-like JDK and JRE.

  The official download page describes these two with Build Apps and Run Apps. We don't need to code on CentOS, so installing Runtime is enough.

  Find the Runtime version page ( link ) corresponding to CentOS in the all downloads of the page to install, pay attention here:

  Pit 1: Version problem, I saw that the Microsoft.AspNetCore.All version of the project in my VS is 2.0.6, and I went to find the 2.0.6 version of Runtime, otherwise it is easy to have some components referenced on VS version. Inconsistent error with the version in the environment on CentOS.

  Link to the CentOS server via Putty and follow the steps on that page to execute the following commands:

1 sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
2 sudo sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl= https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'
3 
4 sudo yum update
5 sudo yum install libunwind libicu

  Finally, there is the following key step. After I execute the deployment, there will still be a problem that a package cannot be found.

sudo yum install dotnet-runtime-2.0.6

  See this passage on github:

Linux
On supported Linux systems, register the Microsoft Product feed as described above and install dotnet-hosting-2.0.6 using your package manager. This will also install the .NET Core Runtime and other required packages.

  Later, I tested it without installing dotnet-runtime- 2.0. 6 but installing dotnet-hosting- 2.0. 6 successfully.

sudo yum install dotnet-hosting-2.0.6

3. Publishing projects with VS on Windows

  Right-click the project and select Publish. By default, it is FDD (Framework Dependent Deployment). The content generated by the release does not contain the dependent framework content and will depend on the runtime installed above.

  Create a folder on CentOS and upload the published files to this folder via FileZila.

  Refer to the create directory command:  mkdir -p / var /aspnetcore/hellomvc   

Fourth, the project operation test

  Execute the command to run the uploaded project:

dotnet /var/aspnetcore/hellomvc/hellomvc.dll

  We all know that by default, the project uses port 5000. When I run the project, I encountered a port conflict. It may be occupied. Modify Program.cs in VS and change the port to the commonly used 8080.

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseUrls("http://*:8080")
                .UseStartup<Startup>()
                .Build();

  Republish and upload, execute the above command successfully, prompting Kestrel to start listening on port 8080.

  Browser visit http://ip:8080 

  

  The result is very strange as shown in the above picture, and the second hole appears. Press F12 to check the prompt that xxx.css xxx.js cannot be found, etc., and confirm through FileZila that the corresponding css and js files have been successfully uploaded to the specified location.

  The first feeling is that UseStaticFiles() has not been executed, and confirmed that it has been executed. Then it was suspected that it was the case of the directory, and they were ruled out one by one, all of which were normal.

  Later, cd to the release directory, execute it again, and finally succeed.

cd / var / aspnetcore / hellomvc

  The result is as we are familiar with the following figure:

  

 Five, install and configure Apache

   Install Apache and configure a reverse proxy to forward port 80 requests to port 8080 above for processing by Kestrel .

  Install and start Apache

sudo yum -y install httpd mod_ssl
sudo systemctl start httpd

  Visit http://ip, the page is the default page of Apache, and the installation is successful.

  To configure the proxy, create and open the file hellomvc.conf:

nano /etc/httpd/conf.d/hellomvc.conf

  nano is a text editing tool. If you are prompted nano: command not found, nano may not be installed .
  Execute the    yum install nano   command to install it.

  Write the following content in the hellomvc.conf file:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
    ServerName www.example.com
    ServerAlias *.example.com
    ErrorLog ${APACHE_LOG_DIR}hellomvc-error.log
    CustomLog ${APACHE_LOG_DIR}hellomvc-access.log common
</VirtualHost>

  Restart the Apache service and set the service to start automatically:

sudo systemctl restart httpd
sudo systemctl enable httpd

  After running the project through  dotnet / var /aspnetcore/hellomvc/hellomvc.dll again, access to  http: // ip  or  http: // ip:8080 is normal.

  Now some people may be more confused, since the previous project can be accessed normally, why use Apache? Is it OK to directly specify listening on port 80 in the project? Because this service directly occupies port 80, but in some cases, we need to assign access from different domain names to different ports for processing. For example, we can assign requests from a.com to 8080 and requests from b.com to To 8081. Of course, if there is no such requirement, use Kestrel directly as a service without a reverse proxy.

  In addition, it is not a good experience to start by commanding  dotnet xxx.dll every time . We can create a service to manage it, which is also a bit like a windows service.

 

 6. Create a service management application

  Create the file again with nano:

sudo nano /etc/systemd/system/kestrel-hellomvc.service

  The contents of the file are as follows:

[Unit]
Description=Example .NET Web API App running on CentOS 7

[Service]
WorkingDirectory=/var/aspnetcore/hellomvc
ExecStart=/usr/local/bin/dotnet /var/aspnetcore/hellomvc/hellomvc.dll
Restart=always
# Restart service after 10 seconds if dotnet service crashes
RestartSec=10
SyslogIdentifier=dotnet-example
User=apache
Environment=ASPNETCORE_ENVIRONMENT=Production 

[Install]
WantedBy=multi-user.target

  Save and start the service:

systemctl enable kestrel-hellomvc.service
systemctl start kestrel-hellomvc.service

  Check for success:

systemctl status kestrel-hellomvc.service

  I encountered a problem here, and the prompt was wrong..........(code=exited, status=203/EXEC)....... kestrel-hellomvc. service failed. Pit three appeared, and it was various searches. Later, I found that ExecStart=/usr/local/bin/dotnet in the content of the above kestrel-hellomvc.service file provided in msdn  did not exist in my CentOS system.  Check it through which dotnet My system is in  /usr/bin/dotnet , modify kestrel-hellomvc.service and re-execute  systemctl start kestrel-hellomvc.service to prompt success. Note that after modifying the file, you will be prompted to execute  systemctl daemon-reload to reload.

  So far, the main work has been completed.

7. Other matters needing attention

User= apache  in A.kestrel-hellomvc.service

    Before installing Apache, the project can be run through  dotnet / var /aspnetcore/hellomvc/hellomvc.dll . At that time, I wanted to create a Service first, because I thought it had nothing to do with Apache. As a result, the service always failed to start, and I noticed it later. To this User= apache , this User is required to exist and have corresponding permissions. Since I am not familiar with CentOS, this has been around for a long time.

  B. Enable ForwardedHeaders middleware

    Since a reverse proxy is used, ForwardedHeaders middleware forwarding needs to be enabled. Add the following code to Configure in Startup. Note that UseForwardedHeaders should be used before UseAuthentication. ( detailed instructions on MSDN )

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

app.UseAuthentication();

 

8. Standalone Deployment (SCD)

   Let's talk about the release method of independent deployment (including dependencies).

  Right-click the project file in VS, note that it is  .csproj instead of  .sln , select Edit xxx.csproj, and open the file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
  </ItemGroup>

</Project>

  Add RuntimeIdentifiers tag in PropertyGroup

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeIdentifiers>win10-x64;centos.7-x64</RuntimeIdentifiers>
  </PropertyGroup>

 win10 -x64;centos.7-x64 is called .NET Core RID, which is some fixed content. For specific options, see the .NET Core RID directory .

  When we publish again, these two options appear in the target runtime of the publish settings. We can select the corresponding RID according to the system to be deployed and publish.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325175997&siteId=291194637