Djangoアプリケーションを同じVPSに複数デプロイするには



Djangoアプリケーションを同じVPSに複数デプロイするには、Nginxをリバースプロキシとして使用し、各アプリケーションのGunicornインスタンスを独立して管理します。


 前提条件


 VPS Digital Ocean

 OS Ubuntu

 Webサーバー Nginx

 アプリケーションサーバー Gunicorn

 ドメイン example.com、tasks.example.com


 1. Gunicornのセットアップ


各DjangoアプリケーションのGunicornインスタンスを設定します。


 アプリケーション①(example.com/api)


Gunicornを次のコマンドで起動します。


(sh)


gunicorn --workers 3 --bind unix:/home/username/myproject/myproject.sock myproject.wsgi:application



 アプリケーション②(tasks.example.com/api)


Gunicornを以下のコマンドで起動します。


(sh)


gunicorn --workers 3 --bind unix:/home/username/tasksproject/tasksproject.sock tasksproject.wsgi:application



 2. Nginxの設定


Nginxの設定ファイルを編集して、リバースプロキシ設定を行います。


 Nginxの設定ファイルの作成または編集


次のように設定します。


(nginx)


# /etc/nginx/sites-available/default


server {

    listen 80;

    server_name example.com;


    location /api {

        include proxy_params;

        proxy_pass http://unix:/home/username/myproject/myproject.sock;

    }

}


server {

    listen 80;

    server_name tasks.example.com;


    location /api {

        include proxy_params;

        proxy_pass http://unix:/home/username/tasksproject/tasksproject.sock;

    }

}



 設定の有効化


シンボリックリンクを作成して設定を有効にします。


(sh)


sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled



 Nginxの設定テストと再起動


Nginxの設定をテストして、エラーがないことを確認します。


(sh)


sudo nginx -t



エラーがなければ、Nginxを再起動します。


(sh)


sudo systemctl restart nginx



ここから先は

1,279字

¥ 1,000

期間限定 PayPay支払いすると抽選でお得に!

この記事が気に入ったらサポートをしてみませんか?