Deploying an ASP. NET core application on CentOS

Source: Internet
Author: User
Tags dotnet

As a new Linux novice, many people preach to me about how powerful CentOS is, so I started posting a demo program to CentOS, trying to figure out how powerful it really is. Prior to this, I have successfully published the same program to Ubuntu, I think, since the previous successful experience, whether CentOS or Ubuntu are Linux, the rationale should be similar. But it turns out that there are some differences, some of the issues on CentOS are not on Ubuntu, so my feeling is that Ubuntu deploys the ASP. NET core program is easier.


The process is not smooth, but eventually after a day of groping has been seven or eight, the application can finally run.


An ASP. NET core application requires reverse proxy mates to run, and Web services software such as IIS, Apache, and Nginx can be used as its reverse proxy. This uses nginx as the reverse proxy.


1. Install the. NET Core SDK. Four commands, executed in turn:

sudo yum install libunwind Libicu

Curl-ssl-o dotnet.tar.gz https://go.microsoft.com/fwlink/?linkid=847103

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

sudo ln-s/opt/dotnet/dotnet/usr/local/bin


2. Install Nginx.

Yum Install Nginx


3. Upload the application. Unlimited use of tools, on Windows can use Mobaxterm, this thing is good, both can log on the system, but also can transfer and download files, but also can visually browse the server directory, open files for editing.


4. Configure Nginx. Open/etc/nginx/nginx.conf, find the "server" block, delete or comment out the previous things, and then add the following configuration:

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;

}

At this point, the application's internal kestrel listens on a port of 5000, while the external user accesses the app still using port 80.


Note Find "user XXX;" This sentence, "XXX" can be any string, this thing to remember, later to set the. NET Core app listening service to system services when it is needed.


5. After the 4th step configuration, go to the directory where the application is located and execute

Dotnet App.dll (App.dll is the file generated after the ASP. NET Core application is published, you need to replace App.dll with your own app name)


If all is well, the application should be accessible, open the browser and try it out.


So far, the application has been deployed. However, you find that a command window has been open, it is the execution of dotnet xxxx.dll that window, if the window is closed, then the application will not be accessible. As the perfect program ape, certainly do not want this window always lying on the screen, then use the system services, so that the application monitoring service starts automatically when the system starts, eliminating the need to execute the dotnet command to run the service again every time you start.


6. Set the application's listening service to system service. In this step, many articles that describe the deployment of. NET core applications use a thing called "Supervisor", which I am not familiar with, of course, I am not familiar with the whole Linux, through reference, I use the system comes with the SYSTEMD (it is said that the previous version does not have this thing, I'm using CentOS 7) to make the daemon process.


In the/etc/systemd/system/directory, build a service file Xxxx.service (replace xxxx with your own name), then open, write:

[Unit] description= application description [Service] Workingdirectory=/sites/test_app #工作目录, reasonable configuration #执行dotnet命令的地方, pay attention to configure the correct dotnet on their own machine path and process Path Execstart=/usr/local/bin/dotnet/sites/test_app/webapp.dll restart=always restartsec=10 # Note that the value of the user here    Should be the same as user xxx's xxx in the 4th step nginx.conf configuration file. # For example, in nginx.conf, there is a: User Nginx, then the value here is the user is Nginx user=nginx environment=aspnetcore_environment=production [I Nstall] Wantedby=multi-user.target

After the "#" above is the comment, I added some instructions, actually use can remove these comments.

In this step there is a problem, in the beginning, after "Execstart ..." there is a comment, in the execution of Systemctl enable Xxxx.service always error. The comment was later removed and the space after the sentence was removed and then normal.


7. Set the service to boot, so that whenever the machine restarts it does not need to execute the dotnet command to start the application monitoring, the annoying dotnet listening window can also be closed.

To set the service to start:

Systemctl Enable Xxxx.service


Start the service:

Systemctl Start Xxxx.service


View service Status:

Systemctl Status Xxxx.service

If the service does not start properly, this command can show where the error occurred. If there is an error, modify the error.

Here, the deployment is complete and you should be able to access the application.


The next day, I remembered that the monitoring service has been set up as a system service, can be restarted after the execution of the dotnet command, so I would like to test the end of the unreliable, the machine restarted. The result browser gave me back a "bad Gateway", it is not reliable Ah, said the Linux strong?


I use the Systemctl status check the service, display running, not the service did not start, indicating that the system services or reliable. How can you not access it? On the internet for a long time did not find the answer, suddenly thought, is not nginx this thing hung, because in the installation, configuration Nginx when the goods have been paralyzed, use nginx-s reload command want to restart Nginx, the results found it prompted Nginx.pid file out a problem. The solution to this problem, I mentioned in the "Nginx in CentOS lost nginx.pid file processing."

Deployment in Ubuntu is similar to this, some commands are different. Reference: Https://docs.microsoft.com/zh-cn/aspnet/core/publishing/linuxproduction.


This article is from the "Sunset Tower" blog, please be sure to keep this source http://luoriloutai.blog.51cto.com/4349732/1920375

Deploying an ASP. NET core application on CentOS

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.