本ドキュメントのゴール

  1. Vueで作ったアプリをNGINX上にデプロイする

環境

  • ハードウェア

    • Raspberry Pi 4

  • OS

    • Ubuntu 24.04.1 LTS

  • 前提条件

    • DockerおよびDocker Composeのインストール済みであること

手順

NGINXコンテナの起動

  1. コンテナ用のディレクトリを作成

    $ mkdir -p docker/nginx_vue
    $ cd docker/nginx_vue
  2. 任意のディレクトリに`docker-compose.yml`を作成

    version: '3.8'
    
    services:
      app:
        image: nginx:alpine
        ports:
          - "11080:80"
        volumes:
          - ./dist:/usr/share/nginx/html  # ビルドした静的ファイルをマウント
          - ./conf.d:/etc/nginx/conf.d  # カスタム Nginx 設定ファイルをマウント
        restart: always
  3. マウント用のフォルダを作成

    # vueのdistを置くフォルダ
    $ mkdir -p ./dist
    # configを置くフォルダ
    $ mkdir -p ./conf.d
  4. confディレクトリにnginxのconfファイルを作成

    $ nano conf.d/default.conf
  5. confファイルを編集する

    server {
        listen 80;
    
        root /usr/share/nginx/html;
        index index.html;
    
        location / {
            try_files $uri $uri/ /index.html;
        }
    }

Vueファイルの配布

  1. Viteでビルドする

    $ npm run build
  2. distフォルダをNGINXコンテナのマウント先にコピーする

    $ scp -r dist/ XXX

NGINXの起動

  1. NGINXコンテナを起動

    $ docker compose up -d

アプリの確認

  1. ブラウザからアプリが開けることを確認する