部分原文:http://www.cnblogs.com/jhao/p/6071790.html
1、nginx安装
sudo apt-get install nginx
ubantu安装完Nginx后,文件结构大致为:
所有的配置文件都在 /etc/nginx下;
启动程序文件在 /usr/sbin/nginx下;
日志文件在 /var/log/nginx/下,分别是access.log和error.log;
并且在 /etc/init.d下创建了启动脚本nginx。
nginx命令:
service nginx star/stop/restart/status
2、uwsgi安装
进入到Python虚拟环境中
pip install uwsgi
web → django大致通信原理:
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django
nginx作为服务器最前端,负责接收client的所有请求,统一管理。静态请求由Nginx自己处理。非静态请求通过uwsgi传递给Django,由Django来进行处理,从而完成一次WEB请求。
3、配置 uwsgi
# Django-related settings [uwsgi] #socket = 0.0.0.0:8001 http = 0.0.0.0:8001 pidfile = /tmp/uwsgi.pid # the base directory (full path) chdir = /home/mugz/sites/soowhat.cn/django_mxonline # Django s wsgi file module = mxonline.wsgi # 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 daemonize = /var/log/uwsgi.log
进入到项目所在目录,启动uwsgi
cd /home/mugz/sites/soowhat.cn/django_mxonline
sudo uwsgi --ini uwsgi.ini
4、配置nginx
nginx默认会读取 /etc/nginx/sites-enabled/default 文件中的配置,修改其配置如下:
server { # the port your site will be served on listen 80; # the domain name it will serve for server_name 127.0.0.1; # substitute your machine‘s IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /home/mugz/sites/soowhat.cn/django_mxonline/media; # your Django project‘s media files - amend as required } location /static { alias /home/mugz/sites/soowhat.cn/django_mxonline/static; # your Django project‘s static files - amend as required } # Finally, send all non-media requests to the Django server. location / { include uwsgi_params; # the uwsgi_params file you installed uwsgi_pass 127.0.0.1:8001; } }
在 /etc/nginx/sites-available/ 放置了配置文件,接下来需要创建一个符号链接,把这个配置文件加入到启用的网站列表中去,被启用网站的目录在 /etc/nginx/sites-enabled/,你可以理解为从 sites-available/ 目录下发送了一个配置文件的快捷方式到 sites-enabled/ 目录。具体命令如下:
(mxonline) [email protected]:/etc/nginx/sites-available$ sudo ln -s /etc/nginx/sites-available/soowhat.cn /etc/nginx/sites-enabled/soowhat.cn (mxonline) [email protected]:/etc/nginx/sites-enabled$ ls -l total 4 lrwxrwxrwx 1 root root 34 Jul 1 12:02 default -> /etc/nginx/sites-available/default
5、搜集django静态文件
把Django自带的静态文件收集到同一个static中,不然访问Django的admin页面会找不到静态文件。在django的setting文件中,添加下面一行内容:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
然后在项目路径下执行
python manage.py collectstatic
修改settings文件;
DEBUG = False ALLOWED_HOSTS = [‘*‘] #根据情况设定
6、最后重启 nginx 和 uwsgi,完成!