- Web アプリのスケーリングは成功のために不可欠です。
- Django は優れていますが、真のスケーラビリティを実現するには Gunicorn と Nginx が必要です。
- この投稿では、Gunicorn と Nginx を使用してスケーラブルな Django アプリを構築する方法を示します。
- この投稿は「Kali Linux」に続きます。任意の Linux ベースの OS を使用できます。
導入:
- プロジェクトを成功させるには、増加するユーザーとリクエストを処理できる Web アプリケーションを構築することが不可欠です。
- 強力な Python フレームワークである Django は強固な基盤を提供しますが、真のスケーラビリティを得るには、Gunicorn や Nginx などのツールの力を活用する必要があります。
- この投稿では、Gunicorn と Nginx を使用してスケーラブルな Django アプリケーションをセットアップし、Web サイトが増大するユーザー ベースの要求に確実に対応できるようにするプロセスを説明します。
⚙️ セットアップ -
- 私は Linux でこれを行っています。
- まず、python3、pip、nginx がインストールされているかどうかを確認します。
- インストールするには、このコマンドを使用します (ホーム/デスクトップまたは新しい端末にインストールします)
sudo apt install python3 python3-pip nginx
- 次に、仮想環境を作成します (新しいフォルダーを作成しますか?)
- 環境をアクティブ化する
source environment_name/bin/activate # use name created above "environment_name"
- パッケージをインストールしますか?環境の中で
- ジャンゴ、ガニコーン
pip install django pip install gunicorn // or directly install both pip install django gunicorn
Django プロジェクトの作成
- このコマンドを使用して Django プロジェクトを作成します
django-admin startproject myproject # use any name "myproject"
Gunicorn 構成ファイルの作成
- このコマンドを使用します
nano gunicorn_conf.py # use any name but using same better "gunicorn_conf.py"
- 「gunicorn_config.py」に次のコードを追加します。
import multiprocessing bind = '127.0.0.1:8000' # Django running port/link workers = multiprocessing.cpu_count() * 2 + 1
- 「CTRL + o」(保存するには)を使用してファイルを保存し、ENTER を押し、「CTRL + X」(終了するには)を押します
- これによりワーカーが作成され、Django 実行リンクも追加する必要があります。 ### gunicorn を使用して Django プロジェクト/アプリケーションを実行します
- まずプロジェクトフォルダーに移動します
cd myproject # navigate to Django Project "myproject"
- 次にこのコマンドを実行します
gunicorn myapp.wsgi:application -c ../gunicorn_conf.py # here myapp is myproject # here "myapp" is a Django project name, and at last gunicorn_conf.py is a gunicorn configuration file which created above/before.
- Django プロジェクトにいくつかの変更を加えます
- まず、プロジェクト フォルダーの settings.py 内のコードをいくつか変更します。ディレクトリ myproject/myproject/settings.py >ジャンゴプロジェクト
- これには、VS Code などのコード エディターを使用します。ターミナルから開くには、このコマンドを使用します
code- oss # to open vs code from terminal
- このコードを追加してください
# example code ALLOWED_HOSTS = ['localhost', '0.0.0.0', '127.0.0.1'] # use IP address here
- 上記のコマンドでは IP アドレスを使用し、それを取得するために別の新しいターミナルを開き、このコマンドを実行してネットワークに関するすべてを取得しました。
ifconfig
Nginx構成ファイルを作成する
- 別の新しいターミナルを開き、次のコマンドを実行します
sudo nano /etc/nginx/sites-available/myapp # use any name "myapp" # better use vs code instead of nano sudo code- oss /etc/nginx/sites-available/myapp # check code-oss
- そのnginxファイルにコードを追加します
server { listen 80; server_name your_domain.com; # Replace with your domain or IP address # example for aboveline: server_name 'ipaddress' 'another domain' ; location / { proxy_pass http://127.0.0.1:8000; # Gunicorn's default address proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
- ファイルを保存します
- シンボリック リンクを作成して Nginx 構成を有効にし、次のコマンドを実行します
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled # change "myapp" to nginx config file name used above
- それだけです。 ## テスト:
- まず、Nginx 設定ファイルに構文エラーがないか確認します。
sudo nginx -t
- エラーがない場合は、まず次のコマンドを使用して Nginx を再起動します。
sudo systemctl restart nginx
Nginx 用のファイアウォールの設定
- 新しいターミナルを開き、このパッケージをインストールします
sudo apt install ufw
- ファイアウォールを通過する Nginx を許可します
sudo ufw allow 'Nginx Full'
使用説明書 -
- まず新しいターミナルを開いてNginxを起動します
sudo systemctl start nginx
- ここで完全なプロジェクトを開始するには、ターミナルを開き、作成された virtualenv に移動してアクティブ化します。
gunicorn myapp.wsgi:application -c ../gunicorn_conf.py # in above code "myapp" is a Django project name # last gunicorn_conf.py is a gunicorn configuration file name
- これで完全なプロジェクトが始まりました。
- これで、IP アドレスを使用して django プロジェクトを開くことができます。
- ブラウザに移動し、IP アドレスで検索します。
- Nginx を停止するには、このコマンドを使用します
sudo systemctl stop nginx
コーディングを楽しんでいますか? - 怠け者になってください
連絡先DM - Twitter(X)
連絡先メール - sanya.san@myyahoo.com
以上がGunicorn と Nginx を使用したスケーラブルな Django アプリケーションの構築の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 中国語版
中国語版、とても使いやすい

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

WebStorm Mac版
便利なJavaScript開発ツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。
