Linux+uwsgi+nginx+Django部署,以及Nginx资源服务,搭建文件服务器,实现上传和下载

开篇

为啥要费劲搭建这种文件服务,答案肯定有需求,目前开发测试环境我部署了一套samba的共享服务器,所有人通过映射磁盘的方式下载自己的资源,使用过程中,发现,第一很不安全,说不定哪天来个精神状态不好给删除了就瞎了。所以从昨天筹划,到搭建到应用。一天的时间。

环境介绍:

Linux Centos 6.4

Python 3.7

uwsgi 后面有安装过程

Nginx 1.5.6 后面有安装过程

Django 2.2

创建Django工程:

django-admin startproject tm

创建app

django-admin startapp tmapp

Linux+uwsgi+nginx+Django部署,以及Nginx资源服务,搭建文件服务器,实现上传和下载_第1张图片

上图说明:我在创建app之间,把templates 和static 的目录创建了,另外由于我是后来补充此文档,所以截图与当时的界面不太一样。还有同学会问,怎么这里面还能用ls命令,原因是我的机器安装了cygwin。

下一步,

创建模型,在tmapp下的models.py:

# models.py
from django.db import models
class Tool(models.Model):
    name=models.CharField('工具名称',max_length=20)
    url=models.CharField('工具地址',max_length=200)
    desc=models.CharField('描述',max_length=200)
    class Meta:
        verbose_name='工具表'
        verbose_name_plural=verbose_name

注册模型,在tmapp 的admin.py

from django.contrib import admin
from .models import Tool
# Register your models here.
@admin.register(Tool)
class ToolAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'url', 'desc')


注册app
在tm下settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tmapp',
]

修改tm下settings.py

ALLOWED_HOSTS = ['*']
'DIRS': [BASE_DIR + "/templates",],
STATIC_URL = '/static/'
#这个是设置静态文件夹目录的路径
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')


然后提交模型到数据库

Linux+uwsgi+nginx+Django部署,以及Nginx资源服务,搭建文件服务器,实现上传和下载_第2张图片

修改tm下view.py

from django.shortcuts import render
from tmapp.models import Tool
import os
from django.http import HttpResponse, Http404, StreamingHttpResponse
from django.conf import settings
def toollist(request):
    tools=Tool.objects.all()
    return render(request, 'toollist.html',locals())
    #return HttpResponse('weishixian')

def manage(request):
    pass
    return render(request, 'toollist2.html')
def upload(request):
    name=request.POST.get("name")
    filename = request.FILES.get("file", None)
    if request.method == "POST":
        myfile = request.FILES.get("file", None)
        if not myfile:
            return HttpResponse("nofiles for uplad")
        destination = open(os.path.join(settings.MEDIA_ROOT, 'files/' + myfile.name), 'wb+')
        for chunk in myfile.chunks():
            destination.write(chunk)
        destination.close()
    try:
        tool=Tool.objects.create()
        tool.name=name
        tool.desc=name
        tool.url='files/' + myfile.name
        tool.save()
    except:
        return HttpResponse('添加失败
' + name + "
" + filename.name) return HttpResponse('添加成功
'+name+"
"+filename.name) def stream_http_download(request, sid): file_path=Tool.objects.get(id=sid) filepath = os.path.join(settings.MEDIA_ROOT, file_path.url) fp = open(filepath, 'rb') response = StreamingHttpResponse(fp) # response = FileResponse(fp) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="%s"' % file_path.url return response fp.close()

配置路由

tm 的urls.py

from . import view
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', view.toollist,name='index'),
    url(r'^manage/',view.manage),
    path('upload',view.upload,name='upload'),
    path('down-.html', view.stream_http_download, name='down'),  

]

toollist.html

{%for t in tools %}
  • {{t.name}} HTTP
  • {%endfor%}

    toollist2.html

    在cmd下 python manage.py runserver 0.0.0.0:9000,测试一下没有问题,通过ftp将整包传到linux的/data目录

    如图:

    Linux+uwsgi+nginx+Django部署,以及Nginx资源服务,搭建文件服务器,实现上传和下载_第3张图片

     

    linux下安装部署:

    安装uwsgi

    pip install uwsgi

    安装nginx

    ./configure --prefix=/data/nginx
    
    make && make install

    修改uwsgi的配置,

    vi /etc/uwsgi9090.ini

    编辑如下:

    [uwsgi]
    socket = 127.0.0.1:9090
    chdir = /data/tm/
    wsgi-file = tm/wsgi.py
    processes = 4
    threads = 2
    stats = 127.0.0.1:9191
    ~                         

    修改nginx配置

    vi /data/nginx/conf/nginx.conf , 如下:

    server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
            location /files/ {
            root /data/tm/media/;
            autoindex on;
            }
    
            location /static/ {
                    root /data/tm/;
            }
            location / {
                include  uwsgi_params;
                uwsgi_pass  127.0.0.1:9090;
                uwsgi_param UWSGI_SCRIPT demosite.wsgi;
                uwsgi_param UWSGI_CHDIR /demosite;
                index  index.html index.htm;
                client_max_body_size 35m;
            }

     

    启动uwsgi服务:

    uwsgi --ini /etc/uwsgi9090.ini &

    启动nginx服务

    cd /data/nginx/sbin
    
    ./nginx

    浏览器访问

    http://192.168.200.128 展现所有列表

    Linux+uwsgi+nginx+Django部署,以及Nginx资源服务,搭建文件服务器,实现上传和下载_第4张图片

    http://192.168.200.128/manage 可以上传新的内容

    gook luck!

     

    你可能感兴趣的:(Linux+uwsgi+nginx+Django部署,以及Nginx资源服务,搭建文件服务器,实现上传和下载)