目的
ASP.NET Coreで作成したwebアプケーションをRaspberry pi上で実行し、PCからアクセスする
対象物
以下記事で作成したチャットアプリを公開する
https://qiita.com/takmot/items/7fc13b50a3c8d0f6d2aa
dotnet run
で実行する場合
Properties/launchSettings.jsonを以下のように変更
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49801",
"sslPort": 44357
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SignalRChat": {
"commandName": "Project",
"launchBrowser": true,
- "applicationUrl": "https://localhost:5001;http://localhost:5000", // 変更前
+ "applicationUrl": "https://0.0.0.0:5001;http://0.0.0.0:5000", // 変更後
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
publishによって発行したファイルを実行する場合
Properties/launchSettings.jsonの変更では、publishで発行した場合に設定が反映されなかった
そのため、Program.csを以下のように変更
namespace SignalRChat
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseUrls("http://0.0.0.0:5001"); // 追加
});
}
}
Raspberry pi準備
Raspberry piに.NET Coreをインストール
公式ドキュメント通りでうまくいかなかったため、以下記事を参照
https://qiita.com/logikuma/items/de8c987dc2308a96256d
以下抜粋
下記コマンドを実行し、必要な事前取得パッケージをインストールする
sudo apt-get install curl libunwind8 gettext
下記コマンドを実行し、.NET Core ランタイムをCurlでダウンロードする
curl -sSL -o dotnet.tar.gz https://dotnetcli.blob.core.windows.net/dotnet/Runtime/release/2.0.0/dotnet-runtime-latest-linux-arm.tar.gz
下記コマンドを実行し、.NET Coreランタイムをインストールする
sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
下記コマンドを実行し、「dotnet」コマンドを実行可能にする
sudo ln -s /opt/dotnet/dotnet /usr/local/bin
下記コマンドを実行し、結果を確認する
dotnet --help.
Raspberry pi上での実行
-
dotnet publish -r linux-arm
でRaspberry pi用に実行ファイルを生成 - bin\Debug\netcoreapp3.1\linux-armに生成されたpublishフォルダをzip圧縮して、
Raspberry piにFTP等で転送 - 転送したファイルを
unzip publish
- publishフォルダの権限を変更
chmod 755 -R publish
- アプリを実行
./SignalRChat
実行結果
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://0.0.0.0:5001
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /home/pi/work/DGC/publish
http://localhost:5001
ではなくhttp://0.0.0.0:5001
になっていれば成功