概要
AWS LightsailでホスティングされているCentOS 7のサーバで
ASP.NET Coreアプリケーションを公開するにあたりサービス化して自動起動設定を行ったのでそのメモです。
手順的には公式ドキュメント通りなので公式ドキュメント見たほうが早い説あります。
目次
環境
$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
$ dotnet --version
2.2.104
参考サイト様
- https://docs.microsoft.com/ja-jp/aspnet/core/host-and-deploy/linux-nginx?tabs=aspnetcore2x&view=aspnetcore-2.2#monitor-the-app 🔗
- https://qiita.com/chroju/items/4aff0a895f2790f8df8e 🔗
サービスファイルの作成
サービス化するためにサービスファイルを作成します。
編集内容については公式ドキュメントに記載の通りでなんら問題ないと思います。
hoge
とfuga
を適宜読み替えてください。
- hoge:ASP.NET Coreアプリケーションの配置ディレクトリ
- fuga:対象のASP.NET Coreアプリケーションのdll
$ sudo vi /etc/systemd/system/<hoge>.service
/etc/systemd/system/<hoge>.service
[Unit]
Description=本サービスの説明
[Service]
ExecStart=/usr/bin/dotnet /var/<hoge>/<fuga>.dll
Restart=always
RestartSec=10
SyslogIdentifier=<hoge>
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
WorkingDirectory=/var/<hoge>/
# runlevel = 3
[Install]
WantedBy=multi-user.target
環境変数のASPNETCORE_ENVIRONMENT
についてはappsettings.Production.json
を使用するためProduction
を指定しています。
dotnetコマンドのパスについては下記コマンドで確認可能です。
$ which dotnet
/usr/bin/dotnet
作成したサービスの登録&起動
# サービス登録
$ sudo systemctl enable <hoge>.service
# サービス起動
$ sudo systemctl start <hoge>
# サービスの状態確認
$ sudo systemctl status <hoge>
もしサービスファイルの記載に不備があり起動しないなどの場合 サービスファイルを変更すると下記のコマンドが要求されます。
$ sudo systemctl daemon-reload
雑感
workingディレクトリが間違っていて「起動しない!!!!」と苦戦しました…