nginx 的问题+ uwsgi处于虚拟主机模式,运行django应用程序
我的服务器上有几个 django 应用程序,在它们自己的套接字文件上完美运行,但我计划引入更多 django 应用程序,所以我更喜欢在 vhost 模式下运行 uwsgi,但似乎 import/ 存在一些问题路径
这是 nginx 虚拟主机配置
location / {
include uwsgi_params;
uwsgi_param UWSGI_PYHOME /home/httpd/django.udm.local/public_html;
uwsgi_param UWSGI_PYHOME /home/httpd/django.udm.local/public_html;
uwsgi_param UWSGI_PYTHONPATH /home/httpd/django.udm.local/public_html;
uwsgi_param UWSGI_CHDIR /home/httpd/django.udm.local/public_html
uwsgi_param UWSGI_ENV DJANGO_SETTINGS_MODULE=settings;
uwsgi_param UWSGI_MODULE mysite;
uwsgi_pass 127.0.0.1:1088;
,在“mysite.py”文件中我有这个:
import os, sys
sys.path.append(os.path.dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
我用这个命令运行 uwsgi:
uwsgi -s 127.0.0.1:1088 -M --no-site --vhost
错误
*** Operational MODE: single process ***
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1838)
spawned uWSGI worker 1 (pid: 1839, cores: 1)
Traceback (most recent call last):
File "./mysite.py", line 7, in <module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
unable to load app SCRIPT_NAME=django.udm.local|
这里是你可以想象的 ,django 是正确的安装在系统上
# python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django.core.handlers.wsgi
>>>
谢谢!
更新 18/8终于我找到了解决方案..我更改了 mysite.py 文件并添加了 django 和 python 库的路径
import os, sys
sys.path.append(os.path.dirname(__file__))
sys.path.append('/usr/lib/pymodules/python2.6')
sys.path.append('/usr/lib/python2.6/dist-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I have a couple of django apps on my servers, running perfectly on their own socket file, but i plan to introduce some more django apps, so i prefer to run uwsgi in vhost mode, but it seems to have some problem with the import/paths
Here is the nginx virtualhost configuracion
location / {
include uwsgi_params;
uwsgi_param UWSGI_PYHOME /home/httpd/django.udm.local/public_html;
uwsgi_param UWSGI_PYHOME /home/httpd/django.udm.local/public_html;
uwsgi_param UWSGI_PYTHONPATH /home/httpd/django.udm.local/public_html;
uwsgi_param UWSGI_CHDIR /home/httpd/django.udm.local/public_html
uwsgi_param UWSGI_ENV DJANGO_SETTINGS_MODULE=settings;
uwsgi_param UWSGI_MODULE mysite;
uwsgi_pass 127.0.0.1:1088;
and in the "mysite.py" file i have this:
import os, sys
sys.path.append(os.path.dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
i'm running uwsgi with this command:
uwsgi -s 127.0.0.1:1088 -M --no-site --vhost
and HERE is the error
*** Operational MODE: single process ***
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1838)
spawned uWSGI worker 1 (pid: 1839, cores: 1)
Traceback (most recent call last):
File "./mysite.py", line 7, in <module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
unable to load app SCRIPT_NAME=django.udm.local|
as you can imagine, django is correctly installed on the system
# python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django.core.handlers.wsgi
>>>
thanks!
UPDATE 18/8 finally i've found the solution.. i've changed the mysite.py file and added the paths to django and the python libs
import os, sys
sys.path.append(os.path.dirname(__file__))
sys.path.append('/usr/lib/pymodules/python2.6')
sys.path.append('/usr/lib/python2.6/dist-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UWSGI_PYHOME 将设置一个 virtualenv,因此如果您不在 virtualenv 下,则必须将其删除。您可能对此配置感兴趣: http://projects.unbit.it/uwsgi/wiki/TipsAndTricks
UWSGI_PYHOME will set a virtualenv, so if you are not under a virtualenv you have to remove it. You may be interested in this config: http://projects.unbit.it/uwsgi/wiki/TipsAndTricks
我认为在 wsgi 文件中添加内容是不对的。
如果这样做的话,实际上是硬编码的。
如果将代码部署到不同的系统甚至不同版本的 Python 会发生什么?也许是 python2.6、python2.7 等,因此必须在部署的服务器上附加正确的 sys.path。
I don't think it's right to added things in wsgi file.
It's actually hard coded if doing that.
What happened if you deploy code to different systems or even different versions of Python? maybe python2.6, python2.7 .etc, so have to append correct sys.path upon deployed server.