zoukankan      html  css  js  c++  java
  • Django配置(wsgi部署)

    一、Django配置

    1.settings.py配置

    复制全局settings.py配置文件,创建一个副本命名为MyBlog/pro_settings.py,修改DEBUG为False。

    DEBUG = False
    
    # 填写你自己的ip和域名
    ALLOWED_HOSTS = ["www.youkou.site", "39.108.191.165", "localhost", "127.0.0.1"]  
    # 此处设置可以访问服务器的IP地址,*为允许所以地址

     

    2.wsgi.py配置

    # 修改MyBlog/wsgi.py文件
    
    import os
    
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MyBlog.pro_settings')     # 修改为复制新的settings文件名
    
    application = get_wsgi_application()

     

    3.生成迁移文件

    1、进入到要部署的解释器中,执行 pip freeze > test.txt   生成解释器中库的一个迁移文件,然后通过 pip install -r test.txt 安装库 

    注意:fdfs_client.zip 有这个包的要先从文件夹中删除,需要手动安装,直接pip 安装的是python2的3不能用

    4.安装python3以及虚拟环境

    创建虚拟环境

    # 虚拟新的环境名,随意命名
    mkvirtualenv dj_pro

    安装项目相关包

    # 进入到虚拟环境
    workon dj_pro
    # 安装包
    # 需要把requirements.txt文件中的fdfs-client-py删除
    pip install -r requirements.txt
    # 在文件的目录下安装fdfs_client.zip 
    pip install fdfs_client.zip

     

    5.部署的上传至服务器

    将项目本地目录上传至服务器(可以是阿里云ECS服务器)

    方法一:

    • 可以使用xshell连接阿里云服务器,通过rz命令将本地项目目录压缩为zip之后,上传至服务器

    • 在阿里云服务器上,使用unzip 解压项目压缩文件

    • unzip 你的项目压缩文件.zip

    方法二:

    • 可以使用提供ssh连接的工具,将项目目录发送到服务器家目录中

    • scp -r 你的项目目录 服务器用户名@服务器IP:~/ -p ssh服务端口

    # 例如
    scp -r /home/Conner/MyBlog root@服务器IP:~/ -p 22

    6.uwsgi安装测试

    # 进入到虚拟环境
    workon dj_pro
    
    # 安装uwsgi
    pip install uwsgi

     测试uwsgi是否安装成功:

    # 新建测试py文件
    
    # test.py
    def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return [b"Hello World"] # python3
        #return ["Hello World"] # python2

    在test.py文件目录下服务器运行uwsgi:

    uwsgi --http :8000 --wsgi-file test.py

    测试uwsgi运行是否正常:

    127.0.0.1:8000   # 浏览器的url

    7.uwsgi配置

    项目根目录中创建deploy目录,新建uwsgi_conf.ini文件。

    [uwsgi]
    # 使用nginx连接时使用,Django程序所在服务器地址
    # 选择内网IP和端口
    socket=172.18.168.123:8000
    
    # 项目根目录
    chdir=/home/Conner/MyBlog
    
    #项目中wsgi.py文件的相对目录
    wsgi-file=MyBlog/wsgi.py
    
    # 进程数
    processes=2
    
    # 线程数
    threads=2
    
    # uwsgi服务器的角色
    master=True
    
    # 存放进程编号的文件
    pidfile=uwsgi.pid
    
    # 日志文件,因为uwsgi可以脱离终端在后台运行,日志看不见。以前的runserver是依赖终端的
    daemonize=logs/uwsgi.log
    
    # 指定虚拟环境解释器所在目录,不能填相对目录
    virtualenv=/home/Conner/.virtualenvs/dj_pro

    8.启动uwsgi

    切换到deploy目录中,创建logs文件夹,用于存放日志文件

    # 启动uwsgi
    uwsgi --ini uwsgi_conf.ini &
    
    # 停止uwsgi
    uwsgi --stop uwsgi.pid

     

    二、在Ubuntu 18.04中安装配置Nginx

    (主要用于静态文件服务)

    1.安装Installing Nginx

    # 安装nginx
    sudo apt update -y
    sudo apt install nginx -y
    # 启动nginx,查看启动状态,如果启动状态未active,则代表启动成功
    sudo systemctl start nginx && sudo systemctl status nginx

    # 默认开启80端口,可以查看一下是否提供web服务

    curl -I 127.0.0.1

     

    2.nginx 管理命令

    停止您的Web服务器

    sudo systemctl stop nginx

    To start the web server when it is stopped, type:

    sudo systemctl start nginx

    To stop and then start the service again, type:

    sudo systemctl restart nginx

    If you are simply making configuration changes, Nginx can often reload without dropping connections. To do this, type:

    sudo systemctl reload nginx

    By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:

    sudo systemctl disable nginx

    To re-enable the service to start up at boot, you can type:

    sudo systemctl enable nginx

    3.nginx配置

    创建/etc/nginx/conf.d/nginx_dj_pro.conf文件:

    upstream MyBlog {
        # 此处为uwsgi运行的ip地址和端口号
        server 172.18.168.123:8000;
    }
    
    server {
        # 监听端口    
        listen      80;
    
        # 服务器域名或者ip地址
        server_name 39.108.191.165 .youkou.site;
    
        # 编码
        charset     utf-8;
        
        # 文件最大上传大小
        client_max_body_size 75M;
        
        # 媒体文件
        location /media  {
            alias /home/Conner/MyBlog/media;
        }
        
        # 静态文件
        location /static {
            alias /home/Conner/MyBlog/static;
        }
        
        # 主目录
        location / {
            uwsgi_pass  MyBlog;
            include    /etc/nginx/uwsgi_params;
        }
    }
    # 修改sudo vim /etc/nginx/nginx.conf
    # 第一行开头修改用户,将www-data改为你当前的用户
    
    user pyvip;
    # 测试nginx配置文件是否正确,
    sudo nginx -t -c /etc/nginx/nginx.conf
    # 打印如下内容,则没问题
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
    # 重新加载配置
    
    sudo nginx -c /etc/nginx/nginx.conf
    
    sudo nginx -s reload
  • 相关阅读:
    机器学习到底适合哪些人群?
    Window 下载Android系统源代码
    KeyguardSimPinView
    TrustManagerService.java
    ScrimState.java
    KeyguardSliceView.java
    博客
    name="verify-v1"是做什么用的
    基础练习 特殊回文数
    算法训练 P1103
  • 原文地址:https://www.cnblogs.com/jun-1024/p/10964681.html
Copyright © 2011-2022 走看看