python+ nginx + django + uwsgi 之 (四 - 结束) nginx 和 uwsgi 结合

一,uwsgi配置

1. 创建uwsgi.ini文件 

    在项目目录下面新建uwsgi.ini文件 

[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# pwd
/home/wwwroot/files.***.com/wandehua
[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# ls
db.sqlite3  manage.py  test.py  wandehua  weidoo
[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# vi uwsgi.ini

    写入如下代码

[uwsgi]
socket=127.0.0.1:9090
chdir=/home/wwwroot/files.***.com/wandehua
module=wandehua.wsgi:application
master=true
vhost=true
no-site=true
workers=2
reload-mercy=10
vacuum=true
max-requests=1000
limit-as=512
buffer-size=30000
daemonize=/home/wwwroot/files.***.com/wandehua/uwsgi.log

socket字段值127.0.0.1:9090必须要和下面nginx web项目的conf 中的 uwsgi_pass 参数一样; 
chdir指自己项目的绝对路径/home/wwwroot/files.***.com/wandehua; 
module指的是wsgi.py在自己工程中的相对路径,”.”指代一层目录;我的django项目的wsgi.py文件是在/home/wwwroot/files.***.com/wandehua/wandehua/wsgi.py”,所以写成wandehua.wsgi; 
daemonize指定uWSGI日志的存储路径。

项目路径:                                /home/wwwroot/files.***.com/wandehua
wsgi.py的路径:                        /home/wwwroot/files.***.com/wandehua/wandehua/wsgi.py
uwsgi.ini的路径:                      /home/wwwroot/files.***.com/wandehua/uwsgi.ini
uwsgi日志路径:                       /home/wwwroot/files.***.com/wandehua/uwsgi.log
files.***.com.conf的路径:        /usr/local/nginx/conf/vhost/files.***.com.conf
uwsgi_params的路径:            /usr/local/nginx/conf/uwsgi_params

2. 启动uwsgi 

# 启动 uwsgi
[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# uwsgi --ini /home/wwwroot/files.***.com/wandehua/uwsgi.ini 
[uWSGI] getting INI configuration from /home/wwwroot/files.***.com/wandehua/uwsgi.ini

# 启动后,生成了uwsgi.log文件,是在uwsgi.ini文件配置的
[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# ls
db.sqlite3  manage.py  test.py  uwsgi.ini  uwsgi.log  wandehua  weidoo

3. 验证是否成功(查看uwsgi.log文件)

[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# vi uwsgi.log

显示如下:

*** Starting uWSGI 2.0.17.1 (64bit) on [Tue Jan 15 18:09:04 2019] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-18) on 15 January 2019 09:19:43
.....
mapped 296472 bytes (289 KB) for 2 cores
*** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./wandehua/wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi   # 这里有一个报错!!!
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
"uwsgi.log" 45L, 2389C

有一个报错(No module named django.core.wsgi),我们处理一下:

 解决:编辑项目app中的wsgi.py, 加入site-packages目录

 原因:

[root@iZwz96p7abljvv5gfgb34lZ wandehua]# pwd
/home/wwwroot/file.***.com/wandehua/wandehua
[root@iZwz96p7abljvv5gfgb34lZ wandehua]# vi wsgi.py

加入如下代码:

import os
import sys  # 新加的

sys.path.append('/usr/local/python27/lib/python2.7/site-packages') # 新加的

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

再去查看uwsgi.log文件就没有报错了

查看 python site-packages的目录

[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
/usr/local/python27/lib/python2.7/site-packages

二,nginx配置

找到我们 python+ nginx +  django + uwsgi   之 (二)  安装 django 中的2.1 的web项目的nginx配置文件

这里是:/usr/local/nginx/conf/vhost/file.***.com.conf

server
    {
        listen 80;
        #listen [::]:80;
        server_name file.***.com ;
        #index index.html index.htm index.php default.html default.htm default.php;
        #root  /home/wwwroot/file.***.com;

        #include none.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        #include enable-php.conf;
        # uwsgi 调试
        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:9090;          
        }
        # 静态资源
        location ~ ^/static/ {
            root /home/wwwroot/file.***.com/wandehua/weidoo/;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

看看配置有没有问题:

[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

访问地址:

至此,我们就全部配置好了!

----如果访问失败---  请尝试查看uwsgi.log文件,并重启uwsgi

[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# vi uwsgi.log

# 各种处理错误(自行脑补)

[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# killall -9 uwsgi
[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# rm -rf uwsgi.log 
[root@iZwz948fbj8fd5kmzmmaiyZ wandehua]# uwsgi --ini /home/wwwroot/files.***.com/wandehua/uwsgi.ini 
[uWSGI] getting INI configuration from /home/wwwroot/files.***.com/wandehua/uwsgi.ini

---如果修改代码--- 请尝试重启uwsgi  uwsgi 使用

猜你喜欢

转载自blog.csdn.net/wan271920545/article/details/86496846