.NETで作成したWebサイトをデプロイする方法 | How to deploy a website created with .NET
デプロイ方法
前提条件
- Ubuntu 22.04 を使用
- Nginx を使用
- .NET 9 を使用
- SSL有効化済み(後ほど自身で有効化も可能)
1. Ubuntu サーバーの準備
-
パッケージの更新とアップグレード:
sudo apt update && sudo apt upgrade -y
-
.NET ランタイムのインストール:
wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb rm packages-microsoft-prod.deb sudo apt install -y dotnet-sdk-8.0
- インストール確認:
dotnet --info
- インストール確認:
-
Nginx のインストール:
sudo apt install nginx -y
2. .NET アプリケーションの発行
-
アプリケーションの発行:
ローカルのプロジェクトディレクトリで以下を実行:dotnet publish -c Release -o ./publish
これにより、必要なファイルが含まれた
publish
フォルダが作成されます。 -
サーバーへのファイル転送:
scp
または他のファイル転送ツールを使用してpublish
フォルダをサーバーに転送します:scp -r ./publish username@your-server-ip:/var/www/portfolio/publish
username
とyour-server-ip
をサーバーの情報に置き換えてください。
3. アプリケーションの設定
-
サービスファイルの作成:
サーバー上で以下を実行して systemd サービスファイルを作成:sudo vim /etc/systemd/system/portfolio.service
次の内容を追加します
(/var/www/portfolio
をアプリのディレクトリに、your-app-name.dll
をアプリの実行可能ファイルに置き換えてください):[Unit] Description=Portfolio .NET App After=network.target [Service] WorkingDirectory=/var/www/portfolio ExecStart=/usr/bin/dotnet /var/www/portfolio/publish/your-app-name.dll Restart=always RestartSec=10 SyslogIdentifier=dotnet-portfolio User=www-data Group=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_TELEMETRICS=1 [Install] WantedBy=multi-user.target
-
サービスの開始と有効化:
sudo chown -R www-data:www-data /var/www/portfolio sudo chmod -R 755 /var/www/portfolio sudo systemctl daemon-reload sudo systemctl start portfolio.service sudo systemctl enable portfolio.service
-
サービスの状態確認:
sudo systemctl status portfolio.service
4. Nginx のリバースプロキシ設定
-
新しい Nginx 設定の作成:
sudo vim /etc/nginx/sites-available/portfolio
以下の内容を追加:
server { listen 80; server_name portfolio; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name portfolio; ssl_certificate /etc/letsencrypt/live/portfolio/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/portfolio/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { # .NET アプリのポートを指定(デフォルトポートは 5000) 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; # try_files $uri $uri/ =404; } }
-
設定の有効化:
sudo ln -s /etc/nginx/sites-available/portfolio /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx
5. サイトへのアクセス
- ドメイン(または IP アドレス)をブラウザで開いて、サイトが動作していることを確認します。
更新方法
- ローカルでコードを更新し、GitHub リポジトリにプッシュします。
- 必要に応じて、Ubuntu サーバーでリポジトリをプルします:
git pull origin main
- ローカルで以下を実行:
dotnet publish -c Release -o ./publish
- 前の publish フォルダを削除
rm -r /var/www/portfolio/publish
- ファイルをサーバーに転送します:
scp -r ./publish username@your-server-ip:/var/www/portfolio/publish
- サービスを再起動します:
sudo systemctl restart portfolio.service
その他
- bootstrapを読み込まないので、結局CDNにしました。なんでかわからなかった...
- dotnetのインストールから3時間もかかり、ちょっとめんどくさかいなと感じました。。↴
お役に立てたら幸いです。
よかったらいいねお願いします!dehadeha~
How to deploy
Prerequisites
- Using Ubuntu22
- Using Nginx
- Using dotnet
- SSL enabled
1. Prepare the Ubuntu Server
-
Update and Upgrade Packages:
sudo apt update && sudo apt upgrade -y
-
Install .NET Runtime:
wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb rm packages-microsoft-prod.deb sudo apt install -y dotnet-sdk-8.0
- Verify the installation:
dotnet --info
-
Install Nginx:
sudo apt install nginx -y
2. Publish Your .NET WebApp
-
Publish the Application:
On your local machine, navigate to your project directory and run:dotnet publish -c Release -o ./publish
This will create a
publish
folder containing the necessary files. -
Transfer the Files to Your Server:
Usescp
or any other file transfer method to move thepublish
folder to your server:scp -r ./publish username@your-server-ip:/var/www/portfolio/publish
Replace
username
andyour-server-ip
with your server credentials.
3. Configure the Application
-
Create a Service File for the App:
On the server, create a systemd service file:sudo vim /etc/systemd/system/portfolio.service
Add the following content
(replace/var/www/portfolio
with your app's directory
andyour-app-name.dll
with your app's/var/www/portfolio/publish/your-app-name.dll
file name):[Unit] Description=Portfolio .NET App After=network.target [Service] WorkingDirectory=/var/www/portfolio ExecStart=/usr/bin/dotnet /var/www/portfolio/publish/your-app-name.dll Restart=always RestartSec=10 SyslogIdentifier=dotnet-portfolio User=www-data Group=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_TELEMETRICS=1 [Install] WantedBy=multi-user.target
-
Start and Enable the Service:
sudo chown -R www-data:www-data /var/www/portfolio sudo chmod -R 755 /var/www/portfolio sudo systemctl daemon-reload sudo systemctl start portfolio.service sudo systemctl enable portfolio.service
-
Check Service Status:
sudo systemctl status portfolio.service
4. Configure Nginx as a Reverse Proxy
-
Create a New Nginx Configuration:
sudo vim /etc/nginx/sites-available/portfolio
Add the following content:
server { listen 80; server_name portfolio; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name portfolio; ssl_certificate /etc/letsencrypt/live/portfolio/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/portfolio/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { # Point to your .NET app .NET default port is 5000 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; # try_files $uri $uri/ =404; } }
-
Enable the Configuration:
sudo ln -s /etc/nginx/sites-available/portfolio /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx
6. Access Your Portfolio
- Visit your domain (or IP address) to see your site live.
How to Update
- Update your local Git repository and push the changes to the GitHub repository.
- If necessary, pull the updated Git repository on the Ubuntu server using:
git pull origin main
. - On your local machine, run:
dotnet publish -c Release -o ./publish
to create a release build. - Delete the previous folder, run:
rm -r /var/www/portfolio/publish
- Transfer the published files to the server using:
scp -r ./publish username@your-server-ip:/var/www/portfolio/publish
. - Restart the service on the server with:
sudo systemctl restart portfolio.service
.
I hope it helps.
If you like it, please like it! dehadeha~
Discussion