Centos7+Nginx+Python3.7+Django
整體流程
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
venv
創(chuàng)建并激活虛擬環(huán)境(以下所有操作均以root用戶為例子)
cd ~
python3.7 -m venv uwsgi-tutorial
cd uwsgi-tutorial
source bin/activate
Django
在虛擬環(huán)境中网缝,安裝Django框架并創(chuàng)建mysite示例項目船惨,由于默認安裝的2.2版本需要升級centos系統(tǒng)自帶的sqlite版本适刀,所以指定安裝低版本的Django。
pip install Django==2.1.8
django-admin.py startproject mysite
cd mysite
python manage.py migrate
uWSGI
在虛擬環(huán)境中葛作,安裝uwsgi乳怎。
pip install uwsgi
Basic test
安裝完成后芹橡,在虛擬環(huán)境中進行基礎測試。
test.py
新建一個測試文件
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
Run uwsgi
直接以uwsgi框架運行測試文件
uwsgi --http :8000 --wsgi-file test.py
stack of components#1
此時實現的流程:
the web client <-> uWSGI <-> Python
Test Django project
創(chuàng)建Django測試項目
Modify Settings
修改配置文件有勾,允許任意主機訪問疹启。
vi mysite/settings.py
modify
ALLOWED_HOSTS = ['*']
Django test
采用Django自帶框架測試:
python manage.py runserver 0.0.0.0:8000
采用uwsgi測試Django項目:
uwsgi --http :8000 --module mysite.wsgi
stack of components#2
此時實現的流程:
the web client <-> uWSGI <-> Django
Nginx
nginx安裝過程略去,以下僅講安裝后的配置問題蔼卡。
uwsgi_params
拷貝默認配置文件到項目目錄
cp /etc/nginx/uwsgi_params ~/uwsgi-tutorial/mysite/
conf.d
編輯nginx的配置文件喊崖,在8000端口監(jiān)聽Django項目。
vi /etc/nginx/conf.d/mysite_nginx.conf
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///root/uwsgi-tutorial/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 localhost; # 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 /root/uwsgi-tutorial/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /root/uwsgi-tutorial/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 /root/uwsgi-tutorial/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
Deploying static files
搜集Django靜態(tài)文件
vi mysite/settings.py
# add line
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
python manage.py collectstatic
Restart Nginx
重啟nginx
systemctl stop nginx
systemctl start nginx
Add root to Nginx Group
將root用戶添加到nginx組,將nginx用戶添加到root組荤懂,避免出現sock通信時的權限問題:
usermod -a -G nginx root
usermod -a -G root nginx
Deploy
部署測試
test.py
測試test文件茁裙,不加最后的666權限,可能導致訪問時502节仿。
uwsgi --socket /root/uwsgi-tutorial/mysite/mysite.sock --wsgi-file test.py --chmod-socket=666
編輯Django項目的ini配置文件
vi ./mysite_uwsgi.ini
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /root/uwsgi-tutorial/mysite
# Django's wsgi file
module = mysite.wsgi
# the virtualenv (full path)
home = /root/uwsgi-tutorial/
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /root/uwsgi-tutorial/mysite/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
Django
測試Django項目部署
uwsgi --ini mysite_uwsgi.ini
# 后臺運行
uwsgi -d --ini mysite_uwsgi.ini
# 關閉進程
killall -9 uwsgi
# 查找進程
ps aux|grep uwsgi
另一種后臺運行的方法是修改ini配置文件晤锥,添加一行:
daemonize = /var/log/uwsgi/mysite.log # background the process & log
文件目錄結構(僅供參考)
- uwsgi-tutorial
- bin/
- include/
- lib/
- lib64/ -> lib
- mysite/
- db.sqlite3
- manage.py
- mysite/
- mysite_uwsgi.ini
- static/
- uwsgi_params
- pyvenv.cfg
- test.py
查看調試log
cat /var/log/nginx/error.log