Centos7+Nginx+Python3.7+Django
整体流程
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
venv
创建并激活虚拟环境(以下所有操作均以root用户为例子)
cd ~
python3.7 -m venv uwsgi-tutorial
cd uwsgi-tutorial
source bin/activate
Django
在虚拟环境中,安装Django框架并创建mysite示例项目,由于默认安装的2.2版本需要升级centos系统自带的sqlite版本,所以指定安装低版本的Django。
pip install Django==2.1.8
django-admin.py startproject mysite
cd mysite
python manage.py migrate
uWSGI
在虚拟环境中,安装uwsgi。
pip install uwsgi
Basic test
安装完成后,在虚拟环境中进行基础测试。
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
创建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端口监听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静态文件
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
网友评论