码迷,mamicode.com
首页 > 其他好文 > 详细

nginx + uwsgi + django

时间:2017-04-01 22:17:13      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:django uwsgi nginx

注:

    python3.5

    uwsgi 2.0.15

    nginx 1.11.12

    django 1.10

一、安装python3.5

    yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel  安装一大堆的插件,我怕记不住,在这写一下

     剩下的就是./configure --prefix=/usr/local/python3 && make && make install

     在此就不过多说明了


二、安装uwsgi

  •        我的python装在/usr/local/python3下(不在重复说明)

  •        在python3目录下python3 install uwsgi (方式不重要,安装上就行)

  •        ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi(做一个软链接)

  •        测试uwsgi能不能用

  •        vim test.py

def application(env, start_response):
    start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)])
    return [b"Hello World"]  #注意以字节形式,否则前端看不到Helloween World

         运行uwsgi --http :8001 --wsgi-file test.py

         在浏览器访问 127.0.0.1:8001 如果看到Hello World就成功了

三、安装django

  •        python3 install django(方式不重要,安装上就行)

  •        在/usr/local/python3/bin/下执行创建命令

    python3 django-admin.py startproject dida7 #创建一个项目
    cd dida7                                   #cd到项目里
    python3 manage.py runserver 0.0.0.0:8000   #启动项目

        在浏览器内输入:http://127.0.0.1:8002,检查django是否运行正常。

四、安装nginx

       我的nginx直接从官网上下载的 http://nginx.org/en/download.html

       nginx 1.11.12

   mkdir /usr/usr/nginx
   tar xf nginx-1.11.12.tar.gz -C /usr/src/nginx 
   #解压文件放到/usr/src/nginx目录下
   
   ./configure --prefix=/usr/local/nginx && make && make install 
   #安装目录/usr/local/nginx

五、创建uwsgi.ini文件(可以用xml)

我的uwsgi.ini文件放在/root/dida7目录下,和它平级的有manage.py文件
# myweb_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8888 #指定项目执行的端口号
# the base directory (full path)
chdir           = /root/dida7  #指定项目的目录

# Django s wsgi file
wsgi-file = /root/dida7/dida7/wsgi.py 
#这个指定django项目的wsgi.py文件,wsgi.py文件创建项目就有,我项目放在/root/dida7

# process-related settings
# master
master          = true  #主进程
# maximum number of worker processes
processes       = 4
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

运行uwsgi.ini文件

先cd到/root/dida7目录
[root@CentOS-mode dida7]# uwsgi --ini uwsgi.ini 
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.15 (64bit) on [Sat Apr  1 13:50:08 2017] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-17) on 31 March 20
17 11:45:24os: Linux-2.6.32-642.13.1.el6.x86_64 #1 SMP Wed Jan 11 20:56:24 UTC 201
7nodename: CentOS-mode
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /root/dida7
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
 chdir() to /root/dida7
your processes number limit is 7347
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)
uwsgi socket 0 bound to TCP address :8888 fd 3
Python version: 3.5.1 (default, Mar 31 2017, 10:53:29)  [GCC 4.4.7 2012
0313 (Red Hat 4.4.7-17)]*** Python threads support is disabled. You can enable it with --enable
-threads ***Python main interpreter initialized at 0x206b9a0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 363840 bytes (355 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint=‘‘) ready in 1 seconds on interpreter 0x206b9a0 
pid: 60031 (default app)*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 60031)
spawned uWSGI worker 1 (pid: 60033, cores: 1)
spawned uWSGI worker 2 (pid: 60034, cores: 1)
spawned uWSGI worker 3 (pid: 60035, cores: 1)
spawned uWSGI worker 4 (pid: 60036, cores: 1)

运行起来,看看有没有报错

六、修改nginx配置

vim /usr/local/nginx/conf/nginx.conf

        location / {
            include uwsgi_params;
            uwsgi_pass 192.168.1.90:8888; #这个地址要和uwsgi.ini中的一致
            uwsgi_read_timeout 2;
            index  index.html index.htm;
        }

接下来运行uwsgi,运行nginx

访问192.168.1.90  成功

七、结合nginx,django-admin的样式丢失

技术分享访问django-admin没有样式

解决办法:

       修改settings.py文件

STATIC_URL = ‘/static/‘
STATIC_ROOT = os.path.join(BASE_DIR,"static") 
#这个static是手工建立的文件夹,STAIC_ROOT的路径是dida7/static

       在项目目录下执行python3 manage.py collectstatic

       会django安装目录下的static文件拷贝到我们创建的static目录下

       现在已经在/root/dida7/static/下生成admin文件夹,里边有css、img、js

       修改nginx.conf文件

        location /static/ {
        alias /root/dida7/static/;
     }   #又写了一个location定义静态文件路径

重启uwsgi和nginx

技术分享

成功!!!  我也是坑了两天希望能帮到各位技术分享

本文出自 “python之api” 博客,转载请与作者联系!

nginx + uwsgi + django

标签:django uwsgi nginx

原文地址:http://zhangkk.blog.51cto.com/10823996/1912394

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!