環(huán)境安裝
yum install python-devel.x86_64 nginx supervisor gcc python-pip -y
pip install uwsgi
測試virtualenv Django環(huán)境搭建
pip install virtualenv
mkdir myproject
cd myproject/
virtualenv --no-site-packages venv
source venv/bin/activate
pip install Django==1.9
django-admin startproject mysite
cd mysite/
python manage.py startapp blog
python manage.py runserver 0.0.0.0:8080
測試uwsgi啟動Django程序
# uwsgi --http :8001 --chdir /path/to/project --home=/path/to/env --module project.wsgi
uwsgi --http :8001 --chdir /root/myproject/mysite --home=/root/myproject/venv --module mysite.wsgi
創(chuàng)建uwsgi配置文件
mysite-uwsgi.ini
[uwsgi]
# http = :8080
#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /root/myproject/mysite
# the virtualenv directory(full path)
home = /root/myproject/venv
# Django's wsgi file
wsgi-file = mysite/wsgi.py
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
#monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit
vacuum = true
配置suppervisor服務
/etc/supervisord.d/mysite.ini
[program:mysite]
command=/usr/bin/uwsgi --ini /root/myproject/mysite-uwsgi.ini
directory=/root/myproject/mysite
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
啟動supervisor服務
systemctl enable supervisord
systemctl start supervisord
配置nginx服務
/etc/nginx/conf.d/mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///root/myproject/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 192.168.75.104; # 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/myproject/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /root/myproject/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 uwsgi_params; # the uwsgi_params file you installed
}
}
啟動nginx服務
# 檢查配置文件是否有語法錯誤
nginx -t
# 啟動服務
systemctl enable nginx && systemctl start nginx
參考文檔
- https://alanhou.org/centos-7-uwsgi-nginx-django/
- https://code.ziqiangxuetang.com/django/django-nginx-deploy.html
- https://www.cnblogs.com/alex3714/p/6538374.html
- https://stackoverflow.com/questions/32974204/got-no-such-file-or-directory-error-while-configuring-nginx-and-uwsgi
官方文檔