Django+Nginx+uwsgi部署

2017-03-17  本文已影响0人  笨笨的简书

部署篇:

1、选型

用Nginx+uwsgi+django部署;

2、安装环境

安装nginx

sudo apt-get install nginx

fnngj@ubuntu:~$ /etc/init.d/nginx start  #启动

fnngj@ubuntu:~$ /etc/init.d/nginx stop  #关闭

fnngj@ubuntu:~$ /etc/init.d/nginx restart  #重启

安装uwsgi

pip install uwsgi

3、部署django

Django的最终目录

myweb/

├── manage.py

├── myweb/

│   ├── __init__.py

│   ├── settings.py

│   ├── urls.py

│   └──wsgi.py

└──myweb_uwsgi.ini

其中,usgi.py是用django-admin startproject时自己创建的;myweb_uwsgi.ini是需要手工;

配置myweb_uwsgi.ini

可以参见如下文档:

# myweb_uwsgi.ini file[uwsgi]# Django-related settingssocket =:8000# the base directory (full path)chdir          = /home/fnngj/pydj/myweb# Django s wsgi filemodule          = myweb.wsgi# process-related settings

# mastermaster          = true# maximum number of worker processesprocesses      = 4# ... with appropriate permissions - may be needed

# chmod-socket    = 664

# clear environment on exitvacuum          = true

后台启动uwsgi

uwsgi /Users/harry/Documents/work/django/own/own_uwsgi.ini -d /Users/harry/Documents/work/django/own/own_uwsgi.log

配置nginx,默认位置:/usr/local/etc/nginx/nginx.conf

server {

listen8099;server_name127.0.0.1charset UTF-8;

access_log      /var/log/nginx/myweb_access.log;

error_log      /var/log/nginx/myweb_error.log;

client_max_body_size 75M;

location / {include uwsgi_params;

uwsgi_pass 127.0.0.1:8000;uwsgi_read_timeout 2;

}

location /static {

expires 30d;

autoindex on;

add_header Cache-Control private;

alias /home/fnngj/pydj/myweb/static/;

}

}

补充:

Nginx启停

nginx -s reload  :修改配置后重新加载生效

nginx -s reopen  :重新打开日志文件

nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确

关闭nginx:

nginx -s stop  :快速停止nginx

quit  :完整有序的停止nginx

静态文件:

修改setting文件

添加:STATIC_ROOT='/var/www/mysite/static'

注意,该目录不能跟现在statci目录重

执行

Python manage.py collectstatic

它相当于把所有的静态文件都拷贝到这目录一份

Nginx修改

添加location /static/ {

alias /var/www/mysite/static/;

}

参考:http://www.cnblogs.com/fnng/p/5268633.html

http://blog.csdn.net/u011068616/article/details/50237377

https://docs.djangoproject.com/en/1.10/howto/static-files/

上一篇下一篇

猜你喜欢

热点阅读