本文主要介紹服務器部署時Django需要的配置和uwsgi以及nginx的配置,不介紹Python的安裝以及虛擬環境的安裝創建,也不涉及Mysql數據庫的安裝以及配置,Python以及虛擬環境和Mysql的安裝可以自行網上搜索,一般不會有坑,能順利安裝配置成功。
1. 上傳本地項目到服務器
使用Xftp連接服務器,通過Xftp上傳本地項目到服務器指定位置,比如我會上傳到 home/username文件夾下.
2. 配置Django項目
在項目的setting.py裡面,注釋掉STATICFILES_DIRS,新增STATIC_ROOT。
# STATICFILES_DIRS = (
# os.path.join(BASE_DIR,'static'),
# )
STATIC_ROOT = os.path.join(BASE_DIR,'static/'
配置url.py文件,在urlpatterns裡面新增:
url(r'media/(?P.*)$', serve, {"document_root": MEDIA_ROOT}),
url(r'^static/(?P.*)$', serve, {"document_root": STATIC_ROOT}),
因為用到了serve、MEDIA_ROOT和STATIC_ROOT,需要導入:
from django.views.static import serve
from newblog.settings import MEDIA_ROOT, STATIC_ROOT
3. 收集靜態文件
進入到項目根目錄,運行
python manage.py collectstatic
pip install uwsgi
5. 安裝nginx
pip install uwsgiyum install nginx
新建nginx.cong文件,輸入如下:
user root;
events{}
http{
include /etc/nginx/mime.types;
server{
listen 80;
server_name 你的域名;
index index.html ;
root 你的項目目錄;
location /static {
alias 你的項目目錄/static; # your Django project's static files - amend as required
}
location /media {
alias 你的項目目錄/media;
}
# Finally, send all non-media requests to the Django server.
location / {
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
uwsgi_pass 127.0.0.1:8000;
}
}
}
用新建的nginx.conf文件替換 /etc/nginx/nginx.conf文件,建議在替換之前先備份原始的nginx.conf文件
新建uwsgi.ini文件,輸入如下內容:
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = 你的項目目錄
# Django's wsgi file
module = 項目名.wsgi
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
virtualenv = 虛擬環境路徑
uwsgi uwsgi.ini -d conf/uwsgi.log(-d後面為你想存放log的路徑)
sudo /usr/sbin/nginx
浏覽器裡輸入你的域名,就可以正常訪問你的網站了
Now , Python It has become a m