Nginx + uWSGI + virtualenv + Debian SqueezeのDjango

少し前に、Djangoプロジェクトをデプロイする方法を見つけることに困惑しました。これには2つの要件がありました。

  1. 1つのホスト上の複数のプロジェクトの便利な開始/停止/再開制御
  2. さまざまなプロジェクトのさまざまな仮想環境のサポート


2番目のポイントでは、私の選択はNginx + uWSGIを支持することでした。 最初に検討したオプションのうち、DebianのuWSGIのバインディングが最も好きでした。



ソフトウェアのインストール



uWSGIのサポートは、バージョン0.8.40からNginxに登場しましたが、安定版Debianブランチには最初も2番目もありません。 したがって、バックポートからNginxを、不安定なブランチからuWSGIを取得します。 これを行うには、次の行を/etc/apt/sources.listに追加します。

deb http://backports.debian.org/debian-backports squeeze-backports main contrib non-free deb http://ftp.ru.debian.org/debian testing main non-free contrib deb http://ftp.ru.debian.org/debian unstable main non-free contrib
      
      





そして/ etc / apt / preferencesのようなものを作成します:

 Package: * Pin: release a=stable Pin-Priority: 700 Package: * Pin: release a=squeeze-backports Pin-Priority: 675 Package: * Pin: release a=testing Pin-Priority: 650 Package: * Pin: release a=unstable Pin-Priority: 600
      
      





aptitude updateを使用して同期し 、必要なパッケージをインストールします。

 aptitude -t squeeze-backports install nginx aptitude -t unstable install uwsgi aptitude -t unstable install uwsgi-plugin-python aptitude -t unstable install python-virtualenv
      
      





プロジェクトのディレクトリ構造



サンプルプロジェクトの展開を検討してください。 明確にするために、すべてのプロジェクトがWebユーザーのホームディレクトリのprojサブディレクトリにあるとします 。 また、 projには、 静的プロジェクトファイルが配置される静的サブディレクトリがあり、その配布にはNginxが責任を負います。 サンプルプロジェクトでは、次のパスを取得します。

 /home/web/proj/example /home/web/proj/static/example
      
      





サンプルプロジェクトのsettings.pyには、次の設定があります。

 STATIC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '../static/example')) STATIC_URL = '/static/'
      
      





したがって、 collectstatic管理チームは、 / home / web / proj / static / example内のすべてのプロジェクト統計を収集します。



サンプルプロジェクトの仮想環境は、 / home / web / envs / exampleにあります。



uWSGIおよびNginxの構成



サンプルプロジェクトのuWSGI構成ファイルは/etc/uwsgi/apps-available/example.iniと呼ばれ、次の形式を取ります。

 [uwsgi] plugins = python27 virtualenv = /home/web/envs/example/ chdir = /home/web/proj/example/ pythonpath = .. env = DJANGO_SETTINGS_MODULE=example.settings module = django.core.handlers.wsgi:WSGIHandler() touch-reload = /home/web/proj/example/touchme
      
      





また、 / etc / uwsgi / apps-enabledにシンボリックリンクを作成します。

 cd /etc/uwsgi/apps-enabled ln -s ../apps-available/example.ini .
      
      





次に、uWSGIデーモンを起動します: /etc/init.d/uwsgi start example



Nginx構成ファイルは/ etc / nginx / sites-available / exampleと呼ばれます。 その最小オプションは次のとおりです。

 server { listen 80; server_name example; access_log /var/log/nginx/example.access.log; error_log /var/log/nginx/example.error.log; location /static/ { alias /home/web/proj/static/example/; } location / { include uwsgi_params; uwsgi_pass unix:///var/run/uwsgi/app/example/socket; } }
      
      





同様に、/ etc / nginx / sites-enabledにシンボリックリンクを作成します。

 cd /etc/nginx/sites-enabled/ ln -s ../sites-available/example .
      
      





Nginxを再起動します: /etc/init.d/nginx restart



ボンネットの下



最初に、uWSGI構成ファイルのパラメーターについて簡単に説明します。 pluginsパラメーターは、Pythonのバージョンを明示的に設定します。 chdirおよびpythonpathパラメーターは、プロジェクトディレクトリとその親ディレクトリをPython検索パスに追加します。 envおよびmoduleパラメーターは、uWSGIデーモンでプロジェクトを開始するための特別なスクリプトを作成することなく行います。 touch-reloadパラメーターを使用すると、 touch / home / web / proj / example / touchmeコマンドを使用してプロジェクトを再起動できます。



起動スクリプト/etc/init.d/uwsgiが別の構成ファイル/usr/share/uwsgi/conf/default.iniを使用するため、uWSGIのこのような簡潔な構成ファイルが判明しました。デフォルトで。 必要に応じて、 /etc/uwsgi/apps-available/example.iniで上書きできます。 コメントを削除すると、/ usr / share / uwsgi / conf / default.iniの内容は次のようになります。

 [uwsgi] # try to autoload appropriate plugin if "unknown" option has been specified autoload = true # enable master process manager master = true # spawn 2 uWSGI worker processes workers = 2 # automatically kill workers on master's death no-orphans = true # write master's pid in file /run/uwsgi/<confnamespace>/<confname>/pid pidfile = /run/uwsgi/%(deb-confnamespace)/%n/pid # bind to UNIX socket at /run/uwsgi/<confnamespace>/<confname>/socket socket = /run/uwsgi/%(deb-confnamespace)/%n/socket # set mode of created UNIX socket chmod-socket = 660 # place timestamps into log log-date = true # user identifier of uWSGI processes uid = www-data # group identifier of uWSGI processes gid = www-data
      
      





Debianは、uWSGIバインディングに構成名前空間の概念を導入します。 default.iniでは 、変数%(deb-confnamespace)とデフォルト値appを使用して表されます。 構成名前空間の名前は次のように定義されます。開始スクリプト/etc/init.d/uwsgiは、 / etc / uwsgiディレクトリでs-enabledで終わるすべてのサブディレクトリを探しs-enabledのサブディレクトリ名の一部がファイルの構成スペースの名前になりますこのサブディレクトリ。 この場合、 / etc / uwsgiに、より使い慣れたサブディレクトリを作成して、 site.availableおよびsite -enabledを作成し、それぞれにexample.ini構成ファイルとシンボリックリンクを配置することができます。 この構成のpidfileとソケットは/ run / uwsgi / site / example / pidおよび/ run / uwsgi / site / example / socketにあり、この構成のuWSGIデーモンは/etc/init.d/コマンドを使用して制御されますuwsgi start | stop | restart site / example 。 プレフィックスがない場合、 アプリの名前空間が想定されます。



All Articles