参考官方文档
安装部分:
1.虚拟环境下安装Djang
pip install django
2.安装uwsgi
sudo pip install uwsgi
3.安装Nginx
sudo apt-get install nginx
Nginx常用操作:
开启nginx:
sudo serverice nginx start
重启nginx:sudo serverice nginx restart
关闭nginx:sudo serverice nginx stop
准备&测试部分:
1.虚拟环境下创建Django项目
python django-admin startproject mysite
2.测试运行uwsgi
(1)Django项目下manage.py同目录创建测试文件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
(2)进入manage.py同目录下运行uwsgi
uwsgi --http :8000 --wsgi-file test.py
(3)在浏览器输入127.0.0.1:8000,如果正常显示Hello World则说明the web client <-> uWSGI <-> Python三者是通的
配置部分:
1.拷贝文件uwsgi_params到Django项目
cp /etc/nginx/uwsgi_params ~/mysite
2.项目manage.py同目录下创建文件mysite_nginx.conf并写入:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # 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 /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
3.创建软链接到nginx
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
4.配置Django项目的setting.py,末尾添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
5.Django项目同步静态文件
python manage.py collectstatic
6.项目manage.py同目录下创建media文件夹,并放入media.png
7.重启Nginx
sudo serverice nginx restart
8.在浏览器输入127.0.0.1:8000/media/media.png,如果正常显示media.png则说明Django项目在nginx上正常运行
9.打通nginx、uWSGI和test.py
uwsgi --socket :8001 --wsgi-file test.py
10.在浏览器输入127.0.0.1:8000,如果正常显示Hello World则说明the web client <-> the web server <-> the socket <-> uWSGI <-> Python三者是通的
11.使用Unix sockets代替端口,更改mysite_nginx.conf:
server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
12.连接nginx、uWSGI和test.py运行下面命令,在浏览器输入127.0.0.1:8000,观察是否成功
uwsgi --socket mysite.sock --wsgi-file test.py
13.如果出现502错误,则需解决权限问题
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)
14.调通django、nginx和uwsgi
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
15.在浏览器输入127.0.0.1:8000,如果正常显示Django的"It works"则说明成功
优化部分:
1.项目manage.py同目录下创建uWSGI的初始化文件mysite_uwsgi.ini(简化运行命令)
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = project.wsgi
# the virtualenv (full path)
home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
vacuum = true
2.运行
uwsgi --ini mysite_uwsgi.ini