{"id":42420,"date":"2024-10-03T12:56:37","date_gmt":"2024-10-03T04:56:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/42420\/"},"modified":"2024-10-03T12:56:37","modified_gmt":"2024-10-03T04:56:37","slug":"asp-net-core-%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1%e9%9b%86%e7%be%a4%e6%90%ad%e5%bb%ba%ef%bc%88centos7nginxsupervisorkestrel%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/42420\/","title":{"rendered":"ASP.NET Core \u8ca0\u8f09\u5747\u8861\u96c6\u7fa4\u642d\u5efa\uff08CentOS7+Nginx+Supervisor+Kestrel\uff09"},"content":{"rendered":"<h1 id=\"asp-net-core-%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1%e9%9b%86%e7%be%a4%e6%90%ad%e5%bb%ba%ef%bc%88centos7nginxsupervisorkestrel%ef%bc%89-xsjBhfaomL\">ASP.NET Core \u8ca0\u8f09\u5747\u8861\u96c6\u7fa4\u642d\u5efa\uff08CentOS7+Nginx+Supervisor+Kestrel\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u61c9\u7528\u7a0b\u5e8f\u7684\u53ef\u7528\u6027\u548c\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002\u70ba\u4e86\u78ba\u4fdd\u61c9\u7528\u7a0b\u5e8f\u80fd\u5920\u8655\u7406\u5927\u91cf\u7684\u8acb\u6c42\uff0c\u8ca0\u8f09\u5747\u8861\u96c6\u7fa4\u7684\u642d\u5efa\u6210\u70ba\u4e86\u4e00\u500b\u5fc5\u8981\u7684\u6b65\u9a5f\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 CentOS 7 \u74b0\u5883\u4e0b\uff0c\u4f7f\u7528 Nginx\u3001Supervisor \u548c Kestrel \u642d\u5efa ASP.NET Core \u7684\u8ca0\u8f09\u5747\u8861\u96c6\u7fa4\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-xsjBhfaomL\">\u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u6e96\u5099\u4ee5\u4e0b\u74b0\u5883\uff1a<\/p>\n<ul>\n<li>\u4e00\u53f0\u904b\u884c CentOS 7 \u7684\u4f3a\u670d\u5668<\/li>\n<li id=\"%e5%ae%89%e8%a3%9d-net-core-sdk-xsjBhfaomL\">\u5b89\u88dd .NET Core SDK<\/li>\n<li id=\"%e5%ae%89%e8%a3%9d-nginx-xsjBhfaomL\">\u5b89\u88dd Nginx<\/li>\n<li id=\"%e5%ae%89%e8%a3%9d-supervisor-xsjBhfaomL\">\u5b89\u88dd Supervisor<\/li>\n<\/ul>\n<h2 id=\"%e5%ae%89%e8%a3%9d-net-core-sdk-xsjBhfaomL\">\u5b89\u88dd .NET Core SDK<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88dd .NET Core SDK\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo yum install -y dotnet-sdk-6.0<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5\u5b89\u88dd\u662f\u5426\u6210\u529f\uff1a<\/p>\n<pre><code>dotnet --version<\/code><\/pre>\n<h2 id=\"%e5%bb%ba%e7%ab%8b-asp-net-core-%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f-xsjBhfaomL\">\u5efa\u7acb ASP.NET Core \u61c9\u7528\u7a0b\u5e8f<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5275\u5efa\u4e00\u500b\u65b0\u7684 ASP.NET Core \u61c9\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre><code>dotnet new webapp -n MyApp<\/code><\/pre>\n<p>\u9032\u5165\u61c9\u7528\u7a0b\u5e8f\u76ee\u9304\u4e26\u904b\u884c\u61c9\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre><code>cd MyApp\ndotnet run<\/code><\/pre>\n<p>\u6b64\u6642\uff0c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u61c9\u8a72\u53ef\u4ee5\u5728 <code>http:\/\/localhost:5000<\/code> \u8a2a\u554f\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-nginx-xsjBhfaomL\">\u5b89\u88dd Nginx<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5b89\u88dd Nginx \u4f5c\u70ba\u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668\uff1a<\/p>\n<pre><code>sudo yum install -y epel-release\nsudo yum install -y nginx<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u555f\u52d5 Nginx \u4e26\u8a2d\u7f6e\u70ba\u958b\u6a5f\u81ea\u555f\uff1a<\/p>\n<pre><code>sudo systemctl start nginx\nsudo systemctl enable nginx<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-nginx-xsjBhfaomL\">\u914d\u7f6e Nginx<\/h2>\n<p>\u7de8\u8f2f Nginx \u914d\u7f6e\u6587\u4ef6\u4ee5\u8a2d\u7f6e\u53cd\u5411\u4ee3\u7406\uff1a<\/p>\n<pre><code>sudo vi \/etc\/nginx\/conf.d\/myapp.conf<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name your_domain.com;\n\n    location \/ {\n        proxy_pass http:\/\/localhost:5000;\n        proxy_http_version 1.1;\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection 'upgrade';\n        proxy_set_header Host $host;\n        proxy_cache_bypass $http_upgrade;\n    }\n}<\/code><\/pre>\n<p>\u4fdd\u5b58\u4e26\u9000\u51fa\u5f8c\uff0c\u6aa2\u67e5 Nginx \u914d\u7f6e\u662f\u5426\u6b63\u78ba\uff1a<\/p>\n<pre><code>sudo nginx -t<\/code><\/pre>\n<p>\u5982\u679c\u6c92\u6709\u932f\u8aa4\uff0c\u91cd\u65b0\u52a0\u8f09 Nginx \u914d\u7f6e\uff1a<\/p>\n<pre><code>sudo systemctl reload nginx<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-supervisor-xsjBhfaomL\">\u5b89\u88dd Supervisor<\/h2>\n<p>Supervisor \u662f\u4e00\u500b\u9032\u7a0b\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u7ba1\u7406 Kestrel \u4f3a\u670d\u5668\u3002\u5b89\u88dd Supervisor\uff1a<\/p>\n<pre><code>sudo yum install -y supervisor<\/code><\/pre>\n<p>\u555f\u52d5 Supervisor \u4e26\u8a2d\u7f6e\u70ba\u958b\u6a5f\u81ea\u555f\uff1a<\/p>\n<pre><code>sudo systemctl start supervisord\nsudo systemctl enable supervisord<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-supervisor-xsjBhfaomL\">\u914d\u7f6e Supervisor<\/h2>\n<p>\u7de8\u8f2f Supervisor \u914d\u7f6e\u6587\u4ef6\u4ee5\u7ba1\u7406 Kestrel \u4f3a\u670d\u5668\uff1a<\/p>\n<pre><code>sudo vi \/etc\/supervisord.d\/myapp.ini<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>[program:myapp]\ncommand=\/usr\/bin\/dotnet \/path\/to\/MyApp.dll\nautostart=true\nautorestart=true\nstderr_logfile=\/var\/log\/myapp.err.log\nstdout_logfile=\/var\/log\/myapp.out.log<\/code><\/pre>\n<p>\u4fdd\u5b58\u4e26\u9000\u51fa\u5f8c\uff0c\u91cd\u65b0\u52a0\u8f09 Supervisor \u914d\u7f6e\uff1a<\/p>\n<pre><code>sudo supervisorctl reread\nsudo supervisorctl update\nsudo supervisorctl start myapp<\/code><\/pre>\n<h2 id=\"%e6%b8%ac%e8%a9%a6%e9%9b%86%e7%be%a4-xsjBhfaomL\">\u6e2c\u8a66\u96c6\u7fa4<\/h2>\n<p>\u73fe\u5728\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u700f\u89bd\u5668\u8a2a\u554f\u60a8\u7684\u57df\u540d\uff0c\u6aa2\u67e5\u61c9\u7528\u7a0b\u5e8f\u662f\u5426\u6b63\u5e38\u904b\u884c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u8ca0\u8f09\u6e2c\u8a66\u5de5\u5177\uff08\u5982 Apache Benchmark \u6216 JMeter\uff09\u4f86\u6e2c\u8a66\u96c6\u7fa4\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-xsjBhfaomL\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u5df2\u7d93\u6210\u529f\u5728 CentOS 7 \u74b0\u5883\u4e0b\u642d\u5efa\u4e86 ASP.NET Core \u7684\u8ca0\u8f09\u5747\u8861\u96c6\u7fa4\u3002\u9019\u7a2e\u67b6\u69cb\u4e0d\u50c5\u63d0\u9ad8\u4e86\u61c9\u7528\u7a0b\u5e8f\u7684\u53ef\u7528\u6027\uff0c\u9084\u80fd\u6709\u6548\u5730\u5206\u64d4\u6d41\u91cf\uff0c\u63d0\u5347\u6027\u80fd\u3002\u5982\u679c\u60a8\u9700\u8981\u9032\u4e00\u6b65\u7684\u652f\u6301\u6216\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to set up an ASP.NET Core load balancing cluster using CentOS 7, Nginx, Supervisor, and Kestrel for optimal performance and scalability.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-42420","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/42420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=42420"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/42420\/revisions"}],"predecessor-version":[{"id":42421,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/42420\/revisions\/42421"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=42420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=42420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=42420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}