{"id":1556,"date":"2022-04-16T01:09:01","date_gmt":"2022-04-15T16:09:01","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=1556"},"modified":"2022-04-16T01:09:01","modified_gmt":"2022-04-15T16:09:01","slug":"ubuntu_nginx_asp_net_core","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2022\/04\/16\/ubuntu_nginx_asp_net_core\/","title":{"rendered":"Ubuntu\u306bNginx \uff0b ASP.NET Core\u3092\u69cb\u6210\u3059\u308b"},"content":{"rendered":"<p><a href=\"https:\/\/docs.microsoft.com\/ja-jp\/aspnet\/core\/host-and-deploy\/linux-nginx?view=aspnetcore-6.0\">Nginx \u642d\u8f09\u306e Linux \u3067 ASP.NET Core \u3092\u30db\u30b9\u30c8\u3059\u308b | Microsoft Docs<\/a><\/p>\n<p>.NET 6.0 \u3067ASP.NET Core \u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3057\u3066\u3001Ubuntu\u4e0a\u306eNginx\u3068\u9023\u643a\u3055\u305b\u308b\u3002<\/p>\n<p>Ubuntu\u74b0\u5883\u306f\u3001Multipass\u4e0a\u306b\u69cb\u7bc9\u3057\u5916\u90e8\u304b\u3089\u63a5\u7d9a\u3067\u304d\u308b\u3088\u3046\u306b\u30d6\u30ea\u30c3\u30b8\u3092\u69cb\u6210\u3002mDNS\u3082\u5c0e\u5165\u3059\u308b\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u901a\u308a\u3002\u30db\u30b9\u30c8\u540d\u306fdotnet-study\u3068\u3057\u3066\u304a\u304f\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2021\/03\/10\/create_ubuntu_multipass_kvm_bridge_network\/\">Ubuntu\u3067Multipass(KVM)\u306e\u30d6\u30ea\u30c3\u30b8\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u74b0\u5883\u3092\u69cb\u7bc9\u3059\u308b\u624b\u9806 | Glob (typea.info)<\/a><\/p>\n<p>\u4ee5\u4e0b\u3001Multipass \u4e0a\u306b\u69cb\u7bc9\u3057\u305f\u3001dotnet-study Ubuntu\u30b5\u30fc\u30d0\u30fc\u306b\u3001ASP.NET Core\u3092\u5c0e\u5165\u3059\u308b\u3002<\/p>\n<h2>1.Ubuntu\u306b.NET\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>Ubuntu\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3054\u3068\u306e\u5c0e\u5165\u65b9\u6cd5<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/ja-jp\/dotnet\/core\/install\/linux-ubuntu\">Ubuntu \u306b .NET \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b &#8211; .NET | Microsoft Docs<\/a><\/p>\n<ul>\n<li>.NET \u30a2\u30d7\u30ea\u3092\u958b\u767a\u3059\u308b\u5834\u5408\u306f\u3001SDK \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb (\u30e9\u30f3\u30bf\u30a4\u30e0\u304c\u542b\u307e\u308c\u307e\u3059)<\/li>\n<li>\u30a2\u30d7\u30ea\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3060\u3051\u306e\u5834\u5408\u306f\u3001\u30e9\u30f3\u30bf\u30a4\u30e0\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/li>\n<li>\u30e9\u30f3\u30bf\u30a4\u30e0\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5834\u5408\u306f\u3001ASP.NET Core \u30e9\u30f3\u30bf\u30a4\u30e0\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3053\u3068\u3092\u63a8\u5968(.NET \u3068 ASP.NET \u306e Core \u30e9\u30f3\u30bf\u30a4\u30e0\u306e\u4e21\u65b9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u305f\u3081\uff09<\/li>\n<\/ul>\n<p>20.04\u306e\u5834\u5408<\/p>\n<h3>1.1 \u4fe1\u983c\u3055\u308c\u305f\u30ad\u30fc\u306e\u4e00\u89a7\u306b Microsoft \u30d1\u30c3\u30b1\u30fc\u30b8\u7f72\u540d\u30ad\u30fc\u3092\u8ffd\u52a0\u3057\u3001\u30d1\u30c3\u30b1\u30fc\u30b8 \u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u8ffd\u52a0<\/h3>\n<pre class=\"prettyprint\">$ wget https:\/\/packages.microsoft.com\/config\/ubuntu\/20.04\/packages-microsoft-prod.deb -O packages-microsoft-prod.deb\n$ sudo dpkg -i packages-microsoft-prod.deb\n$ rm packages-microsoft-prod.deb\n<\/pre>\n<h3>1.2 .NET 6.0 SDK \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p>\u4eca\u56de\u306fSDK\u306f\u4e0d\u8981\u306a\u306e\u3067\u3001\u30e9\u30f3\u30bf\u30a4\u30e0\u306e\u307f\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n<pre class=\"prettyprint\">$ sudo apt-get update\n$ sudo apt-get install -y apt-transport-https\n$ sudo apt-get install -y aspnetcore-runtime-6.0\n<\/pre>\n<h2>2.Ubuntu\u306bNginx\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<pre class=\"prettyprint\">$ sudo apt update\n$ sudo apt install nginx\n$ sudo service nginx start<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"Nginx.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/04\/Nginx.png\" alt=\"Nginx.png\" width=\"438\" height=\"240\" border=\"0\" \/><\/p>\n<h2>3.ASP.NET Core Web API \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u4f5c\u6210\u3068\u30c7\u30d7\u30ed\u30a4<\/h2>\n<h3>3.1 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u4f5c\u6210<\/h3>\n<p>\u30ed\u30fc\u30ab\u30eb\u306eMac\uff0bVisual Studio Code\u3067\u5b9f\u65bd<\/p>\n<p>dotnet new webapi -o {{\u51fa\u529b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea}}<\/p>\n<p>\u4eca\u56de\u306fwebapi\u3068\u3044\u3046\u30a2\u30d7\u30ea\u540d\u3067\u4f5c\u6210\u3002\u30ed\u30fc\u30ab\u30eb\u3067\u5b9f\u884c\u3057\u52d5\u4f5c\u78ba\u8a8d<\/p>\n<pre class=\"prettyprint\">$ dotnet new webapi -o webapi<br \/>$ dotnet run<\/pre>\n<p>\u767a\u884c<\/p>\n<pre class=\"prettyprint\">$ dotnet publish -c Release<\/pre>\n<h3>3.2\u00a0<a href=\"https:\/\/www.typea.info\/tips_\/index.php\/SCP%E3%81%A7%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E8%BB%A2%E9%80%81\">SCP<\/a>\u3092\u4f7f\u3063\u3066\u30d5\u30a1\u30a4\u30eb\u3092Ubuntu\u30b5\u30fc\u30d0\u30fc\u3078\u8ee2\u9001<\/h3>\n<p>\u30e6\u30fc\u30b6\u30fc\u306ehome\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\/webapi\/publish \u306b\u7f6e\u304f<\/p>\n<p>scp -r {{publish\u3055\u308c\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea}} {{\u30e6\u30fc\u30b6\u30fc}}\/{{\u30db\u30b9\u30c8}}:{{\u9001\u4fe1\u5148\u30c7\u30a3\u30ec\u30af\u30c8\u30ea}}<\/p>\n<pre class=\"prettyprint\">$scp -r \/Users\/hirotoyagi\/Workspaces\/dotnet\/webapi\/bin\/Release\/net6.0\/publish\/ piroto@dotnet-study.local:webap<\/pre>\n<h3>3.3 \u5358\u72ec\u3067\u52d5\u4f5c\u78ba\u8a8d<\/h3>\n<p>\u8ee2\u9001\u5148\u306eUbuntu\u306b\u5165\u308a\u3001\u8ee2\u9001\u3057\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3067\u3001<\/p>\n<pre class=\"prettyprint\">$ dotnet webapi.dll<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"run_only_asp.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/04\/run_only_asp.png\" alt=\"run_only_asp.png\" width=\"526\" height=\"264\" border=\"0\" \/><\/p>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u30dd\u30fc\u30c8\u3067\u3001\u5b9f\u884c\u3055\u308c\u305f\u306e\u3092\u78ba\u8a8d\u3057\u3001Ctrl+C\u3067\u505c\u6b62\u3057\u3066\u304a\u304f<\/p>\n<h2>4.ASP.NET \u00a0Core Web API \u3092\u30b5\u30fc\u30d3\u30b9\u3068\u3057\u3066\u767b\u9332\u3059\u308b<\/h2>\n<p>webapi\u3068\u3044\u3046\u540d\u524d\u306e\u30b5\u30fc\u30d3\u30b9\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<pre class=\"prettyprint\">$ sudo vi \/etc\/systemd\/system\/webapi.service<\/pre>\n<p>\u5185\u5bb9\u306f\u4ee5\u4e0b\u3002<\/p>\n<p>Description\u306b\u8aac\u660e\u3092\u3001WorkingDirectory\u3001ExecStart \u3092\u8ee2\u9001\u3057\u305f\u30d1\u30b9\u306b\u8a2d\u5b9a<\/p>\n<pre class=\"prettyprint\">[Unit]\nDescription=Example ASP.NET Core Web API running on Ubuntu\n\n[Service]\nWorkingDirectory=\/home\/piroto\/webapi\/publish\nExecStart=\/usr\/bin\/dotnet \/home\/piroto\/webapi\/publish\/webapi.dll\nRestart=always\n# Restart service after 10 seconds if the dotnet service crashes:\nRestartSec=10\nKillSignal=SIGINT\nSyslogIdentifier=dotnet-example\nUser=www-data\nEnvironment=ASPNETCORE_ENVIRONMENT=Production\nEnvironment=DOTNET_PRINT_TELEMETRY_MESSAGE=false\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n<p>\u30b5\u30fc\u30d3\u30b9\u3092\u8d77\u52d5<\/p>\n<pre class=\"prettyprint\">$ sudo service webapi start\n$ sudo service webapi status<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"asp_as_service.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/04\/asp_as_service.png\" alt=\"asp_as_service.png\" width=\"523\" height=\"292\" border=\"0\" \/><\/p>\n<h2>5.Nginx\u306e\u8a2d\u5b9a<\/h2>\n<p>\/etc\/nginx\/sites-available\/default \u306b\u4ee5\u4e0b\u3092\u8ffd\u8a18<\/p>\n<pre class=\"prettyprint\">server {\n    listen 80;\n    listen [::]:80;\n    server_name dotnet-study.local *.dotnet-study.local;\n    location \/ {\n        proxy_pass      http:\/\/127.0.0.1:5000;\n            proxy_http_version  1.1;\n            proxy_set_header    Upgrade $http_upgrade;\n            proxy_set_header    Connection keep-alive;\n            proxy_set_header    Host $host;\n            proxy_cache_bypass  $http_upgrade;\n            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;\n            proxy_set_header   X-Forwarded-Proto $scheme;\n    }\n}<\/pre>\n<p>\u3046\u3054\u3044\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"nginx_asp.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/04\/nginx_asp.png\" alt=\"nginx_asp.png\" width=\"627\" height=\"206\" border=\"0\" \/><\/p>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.amazon.co.jp\/gp\/product\/B08V8KW39K?ie=UTF8&amp;psc=1&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=aacd4180928cc98d4625b02b25092734&amp;language=ja_JP&amp;ref_=as_li_ss_il\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=B08V8KW39K&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22&amp;language=ja_JP\" border=\"0\" \/><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;language=ja_JP&amp;l=li2&amp;o=9&amp;a=B08V8KW39K\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/><\/p>\n<p>iOS\u306e\u30a2\u30d7\u30ea\u958b\u767a\u306e\u4e0a\u8a18Kindle\u672c\u3092\u5199\u7d4c\u3057\u3066\u3044\u308b\u306e\u3060\u304c\u3001\u4e8b\u4f8b\u306eWebAPI\u304c\u3059\u3067\u306b\u6b7b\u3093\u3067\u3044\u308b\u3063\u307d\u3044\u306e\u3067\u3001\u305d\u3082\u305d\u3082\u4f5c\u308a\u51fa\u3057\u305f\u3053\u3068\u3092\u601d\u3044\u51fa\u3057\u305f\u3002\u3002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nginx \u642d\u8f09\u306e Linux \u3067 ASP.NET Core \u3092\u30db\u30b9\u30c8\u3059\u308b | Microsoft Docs .NET 6.0 \u3067ASP.NET Core \u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3057\u3066\u3001Ubuntu\u4e0a\u306eNginx\u3068\u9023\u643a\u3055\u305b\u308b\u3002 Ub [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1555,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[106,180,60],"tags":[182,181,148],"class_list":["post-1556","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-core","category-nginx","category-ubuntu","tag-asp-net-core","tag-nginx","tag-ubuntu"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1556"}],"version-history":[{"count":1,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1556\/revisions"}],"predecessor-version":[{"id":1557,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1556\/revisions\/1557"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/1555"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}