為什麼django項目部署失敗?- django + nginx + uwsgi
我正在嘗試通過 Nginx 和 uWSGI 部署一個 Django 項目。如果我手動執行項目,
python manage.py runserver :8000
它工作正常,所以項目本身不是我猜的問題。我在專用的 virtualenv 中安裝了 Django 和依賴項,並將項目放入其中。我還在 virtualenv 之外安裝了 nginx 和 uwsgi,並開始創建配置文件:
/etc/nginx/sites-available/myproject
(啟用站點的符號連結):upstream uwsgi_myproject { server 127.0.0.1:5678; } server { listen 80; server_name my.url.net; set $home /path/to/myvirtualenv; access_log /path/to/myvirtualenv/log/access_uwsgi.log; error_log /path/to/myvirtualenv/log/error_uwsgi.log; client_max_body_size 10m; keepalive_timeout 120; location / { uwsgi_pass uwsgi_myproject; include uwsgi_params; gzip on; uwsgi_param UWSGI_CHDIR $home/path/to/myproject; uwsgi_param UWSGI_SCRIPT uwsgi; uwsgi_param UWSGI_PYHOME $home; root $home; } location /static/ { alias /path/to/myvirtualenv/path/to/myproject/static/; expires max; autoindex off; } location /media_adm/ { alias /path/to/myvirtualenv/lib/python2.7/site-packages/grappelli/static/; autoindex off; } }
/etc/init/uwsgi_myproject.conf
:description "uWSGI starter for myproject" start on (local-filesystems and runlevel [2345]) stop on runlevel [016] respawn exec /path/to/myvirtualenv/bin/uwsgi \ --uid venvowner \ --home /path/to/myvirtualenv \ --pythonpath /path/to/myvirtualenv/path/to/myproject/ \ --socket 127.0.0.1:5678 \ --chmod-socket \ --module wsgi \ -b 8192 \ --logdate \ --optimize 2 \ --processes 2 \ --master \ --logto /path/to/myvirtualenv/log/uwsgi.log
/path/to/myvirtualenv/path/to/myproject/wsgi.py
:import os, sys, site site.addsitedir('/path/to/myvirtualenv/lib/python2.7/site-packages') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
然後我重新啟動機器並嘗試連接到 my.url.net,但是沒有響應(當我在導入 myproject 之前安裝 nginx 時,它響應“歡迎使用 Nginx!”)。我注意到
lsof | grep LISTEN
尚未創建上游套接字。日誌不輸出任何相關內容,因此我嘗試使用相同的參數手動執行 uwsgi,uwsgi_myproject.conf
並獲得以下輸出:current working directory: /etc/init detected binary path: /path/to/myvirtualenv/bin/uwsgi uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI without its master process manager *** your processes number limit is 6674 your memory page size is 4096 bytes detected max file descriptor number: 1024 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) The -s/--socket option is missing and stdin is not a socket.
編輯:所以我嘗試的最後一件事是重新排序參數。現在 –socket 參數已成功處理,但我仍然在輸出行中獲得此消息:
*** no app loaded. going in full dynamic mode ***
我一直在閱讀的一些文件:
關於我遺漏或忽略什麼的任何想法?
**繼續:**我再次查看了文件並嘗試了不同的方法:使用 .ini 文件並手動執行它以查看會發生什麼:
這有點難看,但這裡有:
[uwsgi] socket = 127.0.0.1:5678 uid = venvowner chdir = /path/to/myvirtualenv home = /path/to/myvirtualenv virtualenv = /path/to/myvirtualenv pythonpath = /path/to/myvirtualenv/path/to/myproject module = wsgi master = true optimize = 2 processes = 2 logto = /path/to/myvirtualenv/log/uwsgi.log
我獲得了一些有用的日誌:
*** Python threads support is disabled. You can enable it with --enable-threads *** *** Operational MODE: preforking *** ImportError: No module named wsgi unable to load app 0 (mountpoint='') (callable not found or import error) *** no app loaded. going in full dynamic mode *** *** uWSGI is running in multiple interpreter mode ***
產生了工人,但堆棧顯然失敗了。我改天再拿這個。
**最後,我讓它工作了。**感謝@DhirajThakur 的評論,這幫助我找到了正確的方向。
/etc/init/uwsgi_myproject.conf
:description "uWSGI starter for myproject" start on (local-filesystems and runlevel [2345]) stop on runlevel [016] respawn exec /usr/local/bin/uwsgi /etc/init/myproject.ini
/etc/init/myproject.ini
:[uwsgi] master = true socket = 127.0.0.1:5678 uid = venvowner chdir = /path/to/myvirtualenv home = /path/to/myvirtualenv virtualenv = /path/to/myvirtualenv pythonpath = /path/to/myvirtualenv/path/to/myproject wsgi-file = /path/to/myvirtualenv/path/to/myproject/wsgi.py optimize = 2 processes = 2 logto = /path/to/myvirtualenv/log/uwsgi.log
不知何故,我覺得我解決問題的方式有點難看。