django項目在服務器上的部署
在linux服務器上採用uwsgi + nginx的方式部署運行。
uwsgi負責django項目的python動態解析;nginx負責靜態文件轉發,以及uwsgi_pass到uwsgi。
此外,在運行nginx之前,需要先收集Django項目的靜態文件到static目錄。
首先,需要在settings.py文件中添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
並註釋掉之前的:
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
注意:上面兩條配置不能並存。如果是本地開發的話,用STATICFILES_DIRS這條配置
然後,運行collectstatic命令:
python manage.py collectstatic
相關的uwsgi配置: uwsgi.ini
[uwsgi]
chdir = /opt/mysite
module = mysite.wsgi
master = true
processes = 4
socket = 127.0.0.1:8001
vacuum = true
uwsgi運行命令
類似如下:
nohup uwsgi uwsgi.ini --plugin python >> uwsgi.log &
相關nginx配置
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
server_name localhost;
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 /opt/xloader/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 /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}