python - uwsgi+django的搭建问题

 sl51866 发布于 2022-10-30 17:46

各位大虾好,小虾效仿http://www.runoob.com/django/...搭建了一个nginx+uwsgi+django的环境。其中uwsgi已经成功了。

我写了一个新的project叫logan,目录就放在/django里,django也看到it works了。然后在/django/logan里写了一个logan_wsgi.py ,如下

#!/usr/bin/env python
# coding: utf-8

import os
import sys

# 将系统的编码设置为UTF8
reload(sys)
sys.setdefaultencoding('utf8')

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

from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

我修改了一下uwsgi9090.ini,详情如下:

[uwsgi]
socket = 127.0.0.1:9090
master = true         //主进程
vhost = true          //多站模式
no-site = true        //多站模式时不设置入口模块和文件
workers = 2           //子进程数
reload-mercy = 10
vacuum = true         //退出、重启时清理文件
max-requests = 1000
limit-as = 512
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid
daemonize = /website/uwsgi9090.log

然后我的nginx.conf详情如下:

server {
        listen       80;
        server_name  localhost;

        location / {
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;
            uwsgi_param UWSGI_SCRIPT logan_wsgi.py;
            uwsgi_param UWSGI_CHDIR /django/logan;
            index  index.html index.htm;
            client_max_body_size 35m;
        }

然后我通过uwsgi --ini /etc/uwsgi9090.ini & 启动uwsgi,然后又启动了nginx。

现在浏览器上输入 服务器外网地址 就会出现502 Bad gateway错误,然后打开uwsgi的日志,出现这样:

your mercy for graceful operations on workers is 60 seconds
mapped 296016 bytes (289 KB) for 2 cores
*** Operational MODE: preforking ***
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 3868)
spawned uWSGI worker 1 (pid: 3869, cores: 1)
spawned uWSGI worker 2 (pid: 3870, cores: 1)
-- unavailable modifier requested: 0 --
-- unavailable modifier requested: 0 --
-- unavailable modifier requested: 0 --
-- unavailable modifier requested: 0 --
-- unavailable modifier requested: 0 --

我就yum install uwsgi-plugin-python,用uwsgi --plugin python --ini /etc/uwsgi9090.ini & 重启了uwsgi进程。然后在浏览器输入外网地址之后,不再是502了,而是Internal Server Error。

打开uwsgi的日志一看是:

Traceback (most recent call last):
File "./logan_wsgi.py", line 13, in

from django.core.handlers.wsgi import WSGIHandler

ImportError: No module named django.core.handlers.wsgi
unable to load app 0 (mountpoint='外网地址|') (callable not found or import error)
--- no python application found, check your startup logs for errors ---
外网地址 [pid: 4282|app: -1|req: -1/12] 60.191.94.120 () {46 vars in 763 bytes} [Wed Mar 8 17:06:31 2017] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)

但是我用python单独打开是不报错的,如下:

Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.core.handlers.wsgi import WSGIHandler
>>> 

请问这样我应该怎么办?

2 个回答
  • from django.core.handlers.wsgi import WSGIHandler有问题
    这是老版本的写法,改成from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    今天我也遇到这问题,希望能帮到你

    2022-10-31 21:28 回答
  • 问题解决了,在uwsgi里使用了chdir 就好了

    话说django的资料真的很少,而且他和python升级太快,很多资料的解答放在最新版本里已经不能用了。

    2022-10-31 21:28 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有