9

サーバーで実行されている Python アプリケーション (具体的な Django) があります。昨日までは、mod-wsgi を使用して apache でほとんど問題なく動作していました。nginx に切り替える主な理由は 2 つあります。

  • パフォーマンス - nginx では、各リクエストにほぼ半分の時間を費やしています
  • 2 つのアプリケーションが一緒に apache で正常に実行されませんでした - nginx によって解決されました
  • 3番目の理由は、私にとってより良い構成です

uwsgi サービスに問題があります。まず、アプリの wsgi ファイルを含めます。

import os
import sys 

path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

if path not in sys.path:
sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "usporion.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

次に、init アプリ用の uwsgi.ini ファイルが次の場所にあります/etc/uwsgi/apps-enabled/usporion.ini

[uwsgi]
plugins = python
uid = www-data
gid = www-data
uwsgi-socket = /srv/sockets/usporion.sock
chmod-socket = 664
chdir = /srv/www/usporion
pythonpath = /srv/www/usporion
module = usporion.wsgi
env = DJANGO_SETTINGS_MODULE=usporion.settings
logdate = True
logto = /var/log/uwsgi/usporion.log
#daemonize = /var/log/uwsgi/usporion.log
vacuum = True
max-requests = 1000
processes = 5
threads = 10
workers = 5
vhost = True

注: デーモン化をコメント解除しようとしました (ただし、これは現在の使用法では機能しません)。

最後に、このnginx構成があります:

upstream django {
    server 95.168.193.219:80;
}

server {
    listen          95.168.193.219:80;
    server_name     usporion.cz;
    return      301 $scheme://www.usporion.cz$request_uri;
}

server {
    listen          95.168.193.219:80;
    server_name     www.usporion.cz;
    charset         utf-8;

    client_max_body_size 100M;

    location /media {
        alias       /srv/www/usporion/media;
        expires     1d;
    }

    location /static {
        alias       /srv/www/usporion/static;
        expires     1d;
    }

    location / {
        root        /srv/www/usporion;
        include     uwsgi_params;
        uwsgi_pass  unix:///srv/sockets/usporion.sock;
    }
}

コマンドの実行はuwsgi --ini /etc/uwsgi/apps-enabled/usporion.ini正常に機能しており、アプリが Web 上で動作していることを確認できます。ただし、service uwsgi startそうすると、サービスが開始されず (FAIL)、メッセージが表示されず、ログに何も見つかりません。usporion.iniアプリ対応なしでこのサービスを実行すると、正常に動作します。

画面の下でuwsgi「サービス」を実行するのを避け、通常のサービスとして実行することができる助けがあれば幸いです。

ここにdist情報があります:

root@[name]:/etc/nginx/sites-enabled# uname -a
Linux [name] 2.6.32-5-amd64 #1 SMP Sun Sep 23 10:07:46 UTC 2012 x86_64 GNU/Linux
root@[name]:/etc/nginx/sites-enabled# cat /etc/debian_version 
6.0.7
root@[name]:/etc/nginx/sites-enabled# nginx -v
nginx version: nginx/1.2.6
root@[name]:/etc/nginx/sites-enabled# uwsgi --version
1.2.3-debian
root@[name]:/etc/nginx/sites-enabled# python --version
Python 2.7.3

最後に、設定に関するアドバイスがあれば (私は nginx を初めて使用しますが、大歓迎です)、これは 8 コア Xeon サーバー 2.4 GHz で 16 GB の RAM を備えており、その半分はこのアプリ用に予約されています。

4

1 に答える 1

3

Error is uwsgi configuration:

[uwsgi]
plugins = python
uid = www-data
gid = www-data
uwsgi-socket = /srv/sockets/usporion.sock
chmod-socket = 664
chdir = /srv/www/usporion
pythonpath = /srv/www/usporion
wsgi-file = /srv/www/usporion/usporion/wsgi.py
env = DJANGO_SETTINGS_MODULE=usporion.settings
logdate = True
logto = /var/log/uwsgi/usporion.log
#daemonize = /var/log/uwsgi/usporion.log
vacuum = True
max-requests = 1000
master = True
enable-threads = True
processes = 5
threads = 10
vhost = True

Difference is in wsgi-file, what have replaced old module config value. Then, error about missing wsgi file appeared (first written error). daemonize is not necessary here as debian's service is automaticly defined this. Still, I think vacuum, logto is not neccessary there, as well chmod-socket and uwsgi-socket - all of them is defined by debian's service. I will aprove this and complete this answer.

Still, from trying etc., this configuration is basic and everything else should be denifed automaticly or have some default value or by Django itselves:

[uwsgi]
plugins = python
chdir = /srv/www/usporion
pythonpath = /srv/www/usporion
wsgi-file = /srv/www/usporion/usporion/wsgi.py
于 2013-05-18T00:44:28.160 に答える