为了账号安全,请及时绑定邮箱和手机立即绑定

Django+nginx+uwsgi上线记录

标签:
Python
不使用nginx

直接用uwsgi启动,不实用nginx代理,此时

  • setting.py修改
ALLOWED_HOSTS = ('*',)
#setting文件最后添加
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static/')
# 设置图片等静态文件的路径
STATICFILES_DIRS = (
    ('css',os.path.join(STATIC_ROOT,'css').replace('\\','/') ),
    ('js',os.path.join(STATIC_ROOT,'js').replace('\\','/') ),
    ('images',os.path.join(STATIC_ROOT,'images').replace('\\','/') ),
    ('upload',os.path.join(STATIC_ROOT,'upload').replace('\\','/') ),
)
  • url.py修改
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from django.contrib import staticfiles

 在urls.py文件最后加上以下内容:

#设置静态文件路径
urlpatterns += staticfiles_urlpatterns()

注意:设置静态文件的目录,很关键

  • 启动uwsgi

图片描述
图片和网页可以显示了
参考1

uwsgi.ini文件

不使用nginx时,uwsgi.ini被放在/root/myvirtualenvs/fishBooked/fishBooked下时

#mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
#chdir           = /myvirtualenvs/fishBooked/fishBooked
# Django's wsgi file
module          = fishBooked.wsgi
# the virtualenv (full path)

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
#threads         = 2
# 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 = /myvirtualenvs/fishBooked/fishBooked

#daemonize = /tmp/mylog.log

不使用nginx,uwsgi.ini被放在/root下时

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = myvirtualenvs/fishBooked/fishBooked
# Django's wsgi file
module          = fishBooked.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 = /myvirtualenvs/fishBooked/fishBooked

#logto = /tmp/mylog.log
使用nginx代理

/etc/nginx/conf.d文件夹下vim uc_nginx.conf

# 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:8000; # for a web port socket (we'll use this first)
}
# configuration of the server

server {
# the port your site will be served on
        listen      8001;
# the domain name it will serve for
        server_name wangfang.cn www.wangfang.cn 111.33.888.66; # 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 你的目录/Mxonline/media;  # 指向django的media目录
#}

        location /static {
                alias /myvirtualenvs/fishBooked/fishBooked/static; # 指向django的static目录
        }

# 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
        }
}

此时启动nginx,然后uwsgi -i uwsgi.ini启动uwsgi图片不能出现其它都可以出现
使用python manage.py collectstatic,说不存在路径
图片描述
再修改setting里的static_root

#未使用nginx时的static_root配置方法
#STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static/')
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'fishBooked/static/')

此时可以python manage.py collectstatic了
图片描述
但图片还是没有出现,感觉是由于nginx代理以后权限出了问题,所以把location /static 段注释调,让静态文件完全交给Django处理,不过这样子的话图片加载很慢。如果希望图片加载快的话,考虑使用cos

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消