📚

.NETで作成したWebサイトをデプロイする方法 | How to deploy a website created with .NET

2025/01/26に公開

デプロイ方法

前提条件

  • Ubuntu 22.04 を使用
  • Nginx を使用
  • .NET 9 を使用
  • SSL有効化済み(後ほど自身で有効化も可能)

1. Ubuntu サーバーの準備

  1. パッケージの更新とアップグレード:

    sudo apt update && sudo apt upgrade -y
    
  2. .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
      
  3. Nginx のインストール:

    sudo apt install nginx -y
    

2. .NET アプリケーションの発行

  1. アプリケーションの発行:
    ローカルのプロジェクトディレクトリで以下を実行:

    dotnet publish -c Release -o ./publish
    

    これにより、必要なファイルが含まれた publish フォルダが作成されます。

  2. サーバーへのファイル転送:
    scp または他のファイル転送ツールを使用して publish フォルダをサーバーに転送します:

    scp -r ./publish username@your-server-ip:/var/www/portfolio/publish
    

    usernameyour-server-ip をサーバーの情報に置き換えてください。


3. アプリケーションの設定

  1. サービスファイルの作成:
    サーバー上で以下を実行して 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
    
  2. サービスの開始と有効化:

    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
    
  3. サービスの状態確認:

    sudo systemctl status portfolio.service
    

4. Nginx のリバースプロキシ設定

  1. 新しい 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;
        }
    }
    
  2. 設定の有効化:

    sudo ln -s /etc/nginx/sites-available/portfolio /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl restart nginx
    

5. サイトへのアクセス

  • ドメイン(または IP アドレス)をブラウザで開いて、サイトが動作していることを確認します。

更新方法

  1. ローカルでコードを更新し、GitHub リポジトリにプッシュします。
  2. 必要に応じて、Ubuntu サーバーでリポジトリをプルします:
    git pull origin main
    
  3. ローカルで以下を実行:
    dotnet publish -c Release -o ./publish
    
  4. 前の publish フォルダを削除
    rm -r /var/www/portfolio/publish
    
  5. ファイルをサーバーに転送します:
    scp -r ./publish username@your-server-ip:/var/www/portfolio/publish
    
  6. サービスを再起動します:
    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

  1. Update and Upgrade Packages:

    sudo apt update && sudo apt upgrade -y
    
  2. 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
    
  3. Install Nginx:

    sudo apt install nginx -y
    

2. Publish Your .NET WebApp

  1. 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.

  2. Transfer the Files to Your Server:
    Use scp or any other file transfer method to move the publish folder to your server:

    scp -r ./publish username@your-server-ip:/var/www/portfolio/publish
    

    Replace username and your-server-ip with your server credentials.


3. Configure the Application

  1. 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
    and your-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
    
  2. 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
    
  3. Check Service Status:

    sudo systemctl status portfolio.service
    

4. Configure Nginx as a Reverse Proxy

  1. 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;
        }
    }
    
  2. 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

  1. Update your local Git repository and push the changes to the GitHub repository.
  2. If necessary, pull the updated Git repository on the Ubuntu server using: git pull origin main.
  3. On your local machine, run: dotnet publish -c Release -o ./publish to create a release build.
  4. Delete the previous folder, run: rm -r /var/www/portfolio/publish
  5. Transfer the published files to the server using: scp -r ./publish username@your-server-ip:/var/www/portfolio/publish.
  6. 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