为了在服务器上运行django项目,需要对项目做以下配置:
1.setting.py文件配置DEBUG=False,ALLOWED_HOSTS = ['*']
2.项目根目录下创建script文件夹,文件夹下新建uwsgi.ini文件,用于启动uWSGI,内容如下:
# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/opt/project/ncpadmin/ncpAdmin
# 指定项目的application
module=ncpAdmin.wsgi:application
# 指定sock的文件路径
socket=127.0.0.1:8001
# 进程个数
workers=5
stats=%(chdir)/script/uwsgi.status
pidfile=%(chdir)/script/uwsgi.pid
# 指定IP端口
http=0.0.0.0:8000
# 指定静态文件
static-map=/static=%(chdir)/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=%(chdir)/script/uwsgi.log
3.在script文件新建文件mysite_nginx.conf,用于配置nginx和uWSGI的关联:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
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 8088;
# 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 /path/to/your/mysite/media; # your Django project's media files - amend as required
# }
location /static {
alias /opt/project/ncpadmin/ncpAdmin/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
# 交给djagno处理
uwsgi_pass django;
# the uwsgi_params file you installed,nginx高版本的/etc/nginx下有该配置文件
include /etc/nginx/uwsgi_params;
}
}
4.创建软链接,让nginx能读到该配置文件(nginx.conf文件默认include 包含conf.d下的所有.conf结尾的文件)
ln -s /opt/project/ncpadmin/ncpAdmin/script/mysite_nginx.conf /etc/nginx/conf.d/
5.把admin的静态文件拷贝到项目的static路径下:
在django项目的setting文件中添加:(注意:setting文件中存在一个STATIC_URL,不管他):
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
导入(项目根目录下执行):
python manage.py collectstatic
注意:(以上设计三个不同的端口号:8000,8001,8088,还有一个nginx默认的80端口)
网友评论