码迷,mamicode.com
首页 > 数据库 > 详细

mysql + Python3.5.2 + Django + Uwsgi + Nginx实现生产环境

时间:2018-01-13 19:00:23      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:ring   ati   deb   uid   chdir   htm   thread   --   return   

官方文档:http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html

前面已经安装好mysql数据库,Python3.5.2,  pip,  django1.8.5,  nginx(前面安装脚本需要删除:--without-http_uwsgi_module)


安装uwsgi: 

pip install uwsgi

uwsgi --version

2.0.15


测试uwsgi:

cat test.py

# test.py

def application(env, start_response):

    start_response('200 OK', [('Content-Type','text/html')])

    return [b"Hello World"] # python3

#return ["Hello World"] # python2


运行:

uwsgi --http :8000 --wsgi-file test.py

浏览器访问服务器IP:8000,返回Hello World,测试正常。

用uwsgi 启动django测试:

uwsgi --http :8001 --chdir /root/blogproject/ --module blogproject.wsgi

浏览器访问服务器IP:8001,返回网页内容,测试正常。


把参数写到配置文件里:

[uwsgi]

# 项目目录

chdir=/root/blogproject/

# 指定项目的application

module=blogproject.wsgi

# 进程个数

workers=5

pidfile=/root/blogproject/uwsgi.pid

# 指定IP端口

http= :8080

# 指定静态文件

static-map=/static=/root/blogproject/static

# 启动uwsgi的用户名和用户组

# uid=root

# gid=root

# 启用主进程

master=true

# 自动移除unix Socket和pid文件当服务停止的时候

vacuum=true

# 序列化接受的内容,如果可能的话

thunder-lock=true

# 启用线程

enable-threads=true

# 设置自中断时间

harakiri=30

# 设置缓冲

post-buffering=4096

# 设置日志目录

daemonize=/root/blogproject/script/uwsgi.log

# 指定sock的文件路径

socket=/root/blogproject/script/uwsgi.sock

#socket = 127.0.0.1:8001

#设置sock文件权限

#chmod-socket = 664


启动和关闭进程

uwsgi --ini uwsgi.ini   # 启动uwsgi配置 

[uWSGI] getting INI configuration from uwsgi.ini

[uwsgi-static] added mapping for /static => /root/blogproject/static  # 启动成功,可以执行ps -ef |grep uwsgi 查看相关进程


uwsgi --stop uwsgi.pid  # 关闭uwsgi

uwsgi --reload uwsgi.pid  #重新加载配置


Nginx配置:

upstream django {

#        server 127.0.0.1:8001;

server unix:///root/blogproject/script/uwsgi.sock;


 }

server {   

        listen 80; 

        server_name www.victory168.top victory168.top;

        access_log  /var/log/nginx/access.log;  # Nginx log file

        error_log  /var/log/nginx/error.log;    # Nginx error log file

        charset  utf-8; # Nginx coding

        gzip on;  # gzip

        gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;  # gzip type


        error_page  404           /404.html;  #404 file

        error_page   500 502 503 504  /50x.html;  # 50x file


        # set uwsgi path

        location / {

            include uwsgi_params;  # import Nginx module with uWSGI  communication 

            # set uwsgi sock path file

#            uwsgi_pass unix:/root/blogproject/script/uwsgi.sock;

            uwsgi_pass django;

        }


        # set static path

        location /static/ {

            alias  /root/blogproject/static/;

#            index  index.html index.htm;

        }

        }


nginx -t  # 检查nginx语法问题


django静态文件收集


1.setting.py设置

DEBUG = False

STATIC_ROOT = os.path.join(BASE_DIR, 'statics')


2. 执行collectstatic命令:

python manage.py collectstatic


3.启动Nginx和uwsgi程序的用户应一致


遇到问题查看Nginx error.log和uwsgi.log,


mysql + Python3.5.2 + Django + Uwsgi + Nginx实现生产环境

标签:ring   ati   deb   uid   chdir   htm   thread   --   return   

原文地址:http://blog.51cto.com/butterflykiss/2060608

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