Centos +django+nginx

WSGI配置

 1 #!/usr/bin/python
 2     """
 3     WSGI config for rana project.
 4 
 5     It exposes the WSGI callable as a module-level variable named ``application``.
 6 
 7     For more information on this file, see
 8     https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
 9     """
10 
11     import os, sys
12     # 这里是服务器中的项目目录
13     sys.path.append('/home/www/rana')
14 
15     from django.core.wsgi import get_wsgi_application
16 
17     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rana.settings")
18 
19     application = get_wsgi_application()
wsgi.py

接下来使用uwsgi运行一下项目

uwsgi --plugin http,python --http :8000 --wsgi-file wsgi.py

使用http协议运行wsgi.py,这时用浏览器访问会看到没有静态文件(js,css等)的页面,然后就是第二步,使用nginx做静态文件服务器。

nginx做静态文件服务器

想让nginx跑起来就要麻烦一点了。

1.修改nginx.conf

nginx.conf文件在 /etc/nginx 目录下,把user的值改为当前用户,否则会有权限问题。

2.如果需要用到80端口

如果需要用到80端口的话,就把 /etc/nginx/conf.d 目录下的default.conf删掉吧,因为80端口会冲突的。

3.新建conf文件

还是在 /etc/nginx/conf.d 目录下,新建一个conf文件,名字随便取,因为nginx.conf中有

include conf.d/*.conf

  这句,所有conf文件都会被读取。
新建的conf文件内容如下:

server {
    listen 80;  #启动的nginx进程监听请求的端口
    server_name localhost;  #域名,如果用vps就把vps的ip填上
    #nginx错误日志,可自行设置,但必须保证提前建立好该目录和文件
    error_log /home/www/log/error.log;   
    location / {
        include /etc/nginx/uwsgi_params;  
        uwsgi_pass localhost:9090;  #对于动态请求,转发到本机的9090端口,
        #也就是uwsgi监听的端口,这个只要不冲突就可以随意,
        #不过之后运行uwsgi时填写的socket要和它一样
    }
    #error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;  
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    location /static/ {
        alias /home/www/rana/static/;   #设定静态文件所在目录,要保证目录已经建好
    }
    location /media/ {  
        alias /home/www/rana/media/;    #设定用户上传的媒体文件所在目录,要保证目录已经建好
    }
}

View Code

5.运行nginx

查看端口占用

netstat -autp

停止80端口程序

kill pid

直接在控制台运行

nginx

6.运行uwsgi

这条命令和之前的有区别,将http改为了socket,让nginx去访问此端口。
同样也要先建好log文件。deamonize是让uwsgi在后台运行,并把日志记录在log文件中,否则一关掉控制台就不能访问了。

uwsgi --socket :9090 --wsgi-file wsgi.py --daemonize /home/www/log/deamon.log





原文地址:https://www.cnblogs.com/tongchengbin/p/7470987.html