美文网首页
CentOS服务器配置Nginx、uwsgi实现Android文

CentOS服务器配置Nginx、uwsgi实现Android文

作者: Allen3333 | 来源:发表于2017-08-13 15:45 被阅读0次

    系统准备

    首先,我们安装yum-utils,一个yum的拓展库,能够实现完整得yum功能和插件
    sudo yum -y install yum-utils
    接下来安卓CentOS开发者工具,用于编译源代码
    sudo yum -y groupinstall development

    安装python3

    安装IUS
    sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
    安装最新版本python
    sudo yum -y install python36u
    检查python版本
    python3.6 -V
    接下来安装python36u0devel包,提供了python3环境下需要的库和头文件
    sudo yum -y install python36u-devel

    配置虚拟环境

    虚拟环境可以提供独立的环境给不同的python项目,保证你的项目有独立的依赖不会干扰到其他项目 。每个编程环境都可以安装不同的python库
    选择一个路径去放置python编程环境
    cd opt/
    mkdir python-virtual-dev
    cd python-virtual-dev
    python-3.6 demo
    接着进入该环境
    source demo/bin/activate
    安装django库
    pip install django
    安装uwsgi
    pip install uwsgi

    配置Nginx

    安装
    yum -y install eqel-release
    yum repolist
    yum install nignx
    安装完打开nginx服务
    systemctl start nginx
    并且测试初始页是否能完成服务
    X.X.X.X(浏览本机IP地址)
    接着修改nginx主配置文件
    systemctl stop nginx
    'cd /etc/nginx'
    vim nginx.conf

    # For more information on configuration, see:
    #   * Official English Documentation: http://nginx.org/en/docs/
    #   * Official Russian Documentation: http://nginx.org/ru/docs/
    
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    
    # Load dynamic modules. See /usr/share/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;
    
    events {
        worker_connections 1024;
    }
    
    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;
    
    #    server {
    #        listen       80 default_server;
    #        listen       [::]:80 default_server;
    #        server_name  _;
    #        root         /usr/share/nginx/html;
    #
    #        # Load configuration files for the default server block.
    #        include /etc/nginx/default.d/*.conf;
    #
    #        location / {
    #        }
    #
    #        error_page 404 /404.html;
    #            location = /40x.html {
    #        }
    #
    #        error_page 500 502 503 504 /50x.html;
    #            location = /50x.html {
    #        }
    #    }
    
    # Settings for a TLS enabled server.
    #
    #    server {
    #        listen       443 ssl http2 default_server;
    #        listen       [::]:443 ssl http2 default_server;
    #        server_name  _;
    #        root         /usr/share/nginx/html;
    #
    #        ssl_certificate "/etc/pki/nginx/server.crt";
    #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
    #        ssl_session_cache shared:SSL:1m;
    #        ssl_session_timeout  10m;
    #        ssl_ciphers HIGH:!aNULL:!MD5;
    #        ssl_prefer_server_ciphers on;
    #
    #        # Load configuration files for the default server block.
    #        include /etc/nginx/default.d/*.conf;
    #
    #        location / {
    #        }
    #
    #        error_page 404 /404.html;
    #            location = /40x.html {
    #        }
    #
    #        error_page 500 502 503 504 /50x.html;
    #            location = /50x.html {
    #        }
    #    }
    }  
    

    配置文件
    cd conf.d
    vim vitrual.conf

    # configuration of the server
    server {
        # the port your site will be served on
        listen 8080;
        # the domain name it will serve for
        server_name  X.X.X.X; # substitute your machine's IP address or FQDN
        error_log /srv/www/yt_demo/logs/error.log;
        access_log /srv/www/yt_demo/logs/access.log;
        charset     utf-8;
    
        # Django media
        location /media/  {
            alias /srv/www/yt_demo/media/;  # your Django project's media files - amend as required
        }
    
        location /static/ {
            alias /srv/www/yt_demo/static/; # your Django project's static files - amend as required
        }
    
        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  unix:/opt/uwsgi/sock/yt_demo.sock;
            include    uwsgi_params; # the uwsgi_params file you installed
        }
    

    创建用户组

    useradd -s /bin/false -r uwsgi

    创建Django项目

    进入虚拟环境
    source /opt/python-virtual-env/demo/bin/activate
    选择路径创建项目
    cd srv/
    mkdir www
    cd www
    django admin startproject yt_demo
    创建logs目录记录log文件
    mkdir logs
    cd logs
    创建空文件
    vim error.log
    vim access.log
    接下来修改文件权限保证nginx能够拥有访问权限
    chown nginx:nginx -R yt_demo/
    返回yt_demo文件夹并创建static和media文件夹
    mkdir static
    mkdir media

    配置uwsgi

    cd /etc
    mkdir uwsgi
    cd uwsgi
    vim emperor.ini

    [uwsgi]
    emperor = /etc/uwsgi/vassals
    uid = uwsgi
    gid = nginx
    logto = /etc/uwsgi/log
    

    创建log文件
    vim log
    vassals配置
    mkdir vassals
    vim demo.ini

    [uwsgi]
    http = :8000
    socket = /opt/uwsgi/sock/yt_demo.sock
    chdir = /srv/www/yt_demo
    pythonpath = /srv/www/yt_demo/yt_demo
    home = /opt/python-virtual-env/demo
    module = yt_demo.wsgi
    uid = uwsgi
    chmod-socket = 664
    chown-socket = uwsgi
    

    修改系统uwsgi配置
    cd /etc/systemd/system
    vim uwsgi.service

    [Unit]
    Description=uWSGI Emperor
    After=syslog.target
    
    [Service]
    ExecStart = /opt/python-virtual-env/demo/bin/uwsgi --ini /etc/uwsgi/emperor.ini
    ExecStop = kill -INT `cat /run/uwsgi.pid`
    ExecReload = kill -TERM `cat /run/uwsgi.pid`
    Restart = always
    Type = notify
    NotifyAccess = main
    PIDFile = /run/uwsgi.pid
    
    [Install]
    WantedBy=multi-user.target
    

    创建sock

    cd /opt/
    mkdir sock
    修改访问权限
    'chown uwsgi:uwsgi /opt/uwsgi'
    接下来测试配置是否成功
    systemctl start nginx
    systemctl start uwsgi

    Django接受文件

    创建Django应用

    进入yt_demo项目路径
    cd /srv/www/yt_demo/
    创建应用
    python manage.py startapp swim
    修改/swim/views.py

    from django.views.decorators.csrf import csrf_exempt
    from django.core.files.storage import default_storage
    from django.core.files.base import ContentFile
    import traceback
    
    from . import models
    
    @csrf_exempt
    def Uploadfile(request):
        if request.method == 'GET':
            return HttpResponse("Get success")
        elif request.method == 'POST':
            try:
               # newtxt = models.Txt(txtfile = request.FILE['accData'])
               # newtxt.save()
                swim_txt = request.FILES.get('accData')
                #with open('/srv/www/yt_demo/media/acc/acc.txt','wb+') as destination:
                 #   for chunk in swim_txt.chunks():
                  #      destination.write(chunk)
                path = default_storage.save('/srv/www/yt_demo/media/acc/'+swim_txt.name,ContentFile(swim_txt.read()))
                return HttpResponse('POST success')
            except:
                tb = traceback.format_exc()
                return HttpResponse(tb)
        else:
            return HttpResponse('Other request')
        #if request.method == 'POST':
           # try:
               # swim_txt = request.FILES.get('accData')
               # path = default_storage.save('User/static/User/swimdata/'+swim_txt.name,ContentFile(swim_txt.read()))
           # return HttpResponse('File got')
          # except:
                #return HttpResponse('File failed')
    
    def index(request):
        return HttpResponse("Hello,world.You're at the swim index.")
    # Create your views here.
    

    修改swim/urls.py

    from django.conf.urls import url
    from . import views
    
    urlpatterns = [
        url(r'^$',views.index,name = 'index'),
        url(r'^Uploadfile/$',views.Uploadfile,name = 'Uploadfile'),
    ]
    

    修改yt_demo/urls.py

    from django.conf.urls import url,include
    from django.contrib import admin
    from django.conf.urls.static import static
    from django.conf import settings
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^swim/', include('swim.urls')),
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    
    

    记得修改media文件夹的访问权限防止出现权限问题

    相关文章

      网友评论

          本文标题:CentOS服务器配置Nginx、uwsgi实现Android文

          本文链接:https://www.haomeiwen.com/subject/lvmsrxtx.html