安裝部分:
1.虛擬環(huán)境下安裝Djang
pip install django
2.安裝uwsgi
sudo pip install uwsgi
3.安裝Nginx
sudo apt-get install nginx
Nginx常用操作:
開(kāi)啟nginx:
sudo serverice nginx start
重啟nginx:sudo serverice nginx restart
關(guān)閉nginx:sudo serverice nginx stop
</br>
</br>
</br>
準(zhǔn)備&測(cè)試部分:
1.虛擬環(huán)境下創(chuàng)建Django項(xiàng)目
python django-admin startproject mysite
</br>
</br>
2.測(cè)試運(yùn)行uwsgi
(1)Django項(xiàng)目下manage.py同目錄創(chuàng)建測(cè)試文件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
</br>
(2)進(jìn)入manage.py同目錄下運(yùn)行uwsgi
uwsgi --http :8000 --wsgi-file test.py
</br>
(3)在瀏覽器輸入127.0.0.1:8000,如果正常顯示Hello World則說(shuō)明the web client <-> uWSGI <-> Python三者是通的
</br>
</br>
</br>
配置部分:
1.拷貝文件uwsgi_params到Django項(xiàng)目
cp /etc/nginx/uwsgi_params ~/mysite
</br>
2.項(xiàng)目manage.py同目錄下創(chuàng)建文件mysite_nginx.conf并寫(xiě)入:
# 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
}
}
</br>
3.創(chuàng)建軟鏈接到nginx
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
</br>
4.配置Django項(xiàng)目的setting.py,末尾添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
</br>
5.Django項(xiàng)目同步靜態(tài)文件
python manage.py collectstatic
</br>
6.項(xiàng)目manage.py同目錄下創(chuàng)建media文件夾,并放入media.png
</br>
7.重啟Nginx
sudo serverice nginx restart
8.在瀏覽器輸入127.0.0.1:8000/media/media.png,如果正常顯示media.png則說(shuō)明Django項(xiàng)目在nginx上正常運(yùn)行
9.打通nginx、uWSGI和test.py
uwsgi --socket :8001 --wsgi-file test.py
10.在瀏覽器輸入127.0.0.1:8000,如果正常顯示Hello World則說(shuō)明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運(yùn)行下面命令,在瀏覽器輸入127.0.0.1:8000,觀察是否成功
uwsgi --socket mysite.sock --wsgi-file test.py
13.如果出現(xiàn)502錯(cuò)誤,則需解決權(quán)限問(wèn)題
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)
14.調(diào)通django、nginx和uwsgi
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
15.在瀏覽器輸入127.0.0.1:8000,如果正常顯示Django的"It works"則說(shuō)明成功
</br>
</br>
</br>
優(yōu)化部分:
1.項(xiàng)目manage.py同目錄下創(chuàng)建uWSGI的初始化文件mysite_uwsgi.ini(簡(jiǎn)化運(yùn)行命令)
# 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.運(yùn)行
uwsgi --ini mysite_uwsgi.ini