• Django程序在Linux上的部署


    Django程序在Linux上的部署

    1.服务器搭建Python环境

    1.1 安装python3

    • 不过多的描述,请参考安装文章,地址

    1.2 创建虚拟环境

    virtualenv envsname

    1.3 代码传送到服务器

    • 可以使用 git 或者FZ

    1.4 安装第三方包

    • 发布上的代码应该包含requirement.txt

      pip freeze > requirements.txt

    • 激活虚拟环境后执行,到包含requirement.txt目录下:

      pip install -r requirements.txt

    • image-20211221174954989

    1.5 迁移数据库

    • python manage.py makemigrations

    • python manage.py migrate

    • 本博客只是做简单发布的示例,因此暂时不做数据库的迁移工作。注:如果服务器的数据库支持远程连接,亦可使用Navicat进行数据库的迁移工作

    1.6 测试

    • 查看一下django是否等够正常启动

    • image-20211221175227874

    2.安装Nginx

    3.发布

    3.1 简单发布

    • 将程序在后台运行,并且将终端的输出(打印)指向nohup.out文件
    nohup python manage.py runserver 127.0.0.1:8000 &
    
    • 补充

      • 使用&命令后,作业被提交到后台运行,当前控制台没有被占用,但是一但把当前控制台关掉(退出帐户时),作业就会停止运行。nohup命令可以在你退出帐户之后继续运行相应的进程。nohup就是不挂起的意思( no hang up)。该命令的一般形式为:

        nohup command &
        
      • 默认都是输出到nohup.out文件中,也可以修改指定的文件,这里不在过多描述,请参考这篇文章

    • 此时Django程序已经运行在后台

      image-20211221180824427

    • 使用nginx进行反向代理

    • # 修改nginx.conf的配置文件,
      server {
              listen       80;
              server_name  服务器IP;
      
              #charset koi8-r;
      
              #access_log  logs/host.access.log  main;
      
              location / {
                  proxy_pass  http://127.0.0.1:8000/;#django服务启动的地址
              }
      
    • 启动nginx

    • cd sbin
      ./nginx
      
    • image-20211221181458562

    • 访问成功

    3.2 上线发布

    • 补充:nginx uwsgi wsgi django 这些东西究竟是什么关系.参考文献

    • image-20211221183925202

    1.安装uwsgi

    pip install uwsgi
    

    image-20211221182355407

    2.配置nginx.conf文件

    # 新增加一个server配置,此过程繁多建议将文件下载下来修改后在传到服务器上
    server {
    	listen 8000; #暴露给外部访问的端口
    	server_name localhost;
    	charset utf-8;
    	location / {
    		include uwsgi_params;
    		uwsgi_pass 127.0.0.1:8997; #外部访问8996就转发到内部8997
    	}
    	location /static/ {
    		alias /data/pythoncode/dejangoTest/web/static/; #项目静态路径设置
    	}
    }
    

    image-20211221190936489

    3.使用uwsgi启动django

    django自带的wsgiref 在调试模式下使用的wsgi的文件,网关接口,协议
    uwsgi:协议
    uWSGI:具体实现方式

    • uwsgi配置文件的格式

      conf
      py
      cnf
      xml
      json
      ini
      yaml

    • # uwsgi.ini file
      [uwsgi]
      
      # Django-related settings
      socket = :8000
      
      # the base directory (full path)
      chdir = /root/data/pythoncode/testcode/mypro
      
      # Django s wsgi file
      module = mypro.wsgi
      
      # process-related settings
      master = true
      
      # home = /root/data/envs/anydjango
      
      # maximum number of worker processes
      processes = 5
      
      #maximum number of worker threads
      threads = 5
      
      virtualenv=/root/data/envs/anydjango
      
      # try to remove all of the generated file/sockets
      vacuum = true
      
      # 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器
      daemonize = /root/data/pythoncode/testcode/mypro/log/myuwsgi.log
      
    • 启动

      uwsgi --http :8000 --module dejangoTest.wsgi
      
    • 关闭

      • killall uwsgi
        
    • image-20211221195350577

    • image-20211221195501290

    • 另一种启动命令

      image-20211221200551420

    4.设置静态文件

    SATAIC_ROOT=os.path.join(BASE_DIR,'web/static/')#由于我的静态文件是放在应用里面的,因此我要在路径前加上应用名
    

    image-20211221192151565

    # 执行命令
    python manage.py collectstatic
    

    image-20211221192827820

    nginx中配置静态文件;

    server {
            listen       80;
            server_name  127.0.0.1;
            # charset UTF-8;
    
            #access_log  logs/host.access.log  main;
    
            location / {
               include uwsgi_params;
               uwsgi_pass 127.0.0.1:8000;
           }
    	
    		location /static {
    			alias /www/res/statics;
    		}
    }
    

    image-20220401152250453

    • 配置完成后启动uwsgi

      uwsgi --ini uwsgi.ini
      
    • 重启nginx

      ./nginx -s reload
      
  • 相关阅读:
    通过IMM With Remote Console为服务器安装操作系统
    linux下编译安装php5.6出现 configure: error: Cannot find MySQL header files under /usr/local/mysql.
    5700交换机清除配置
    嵌入式驱动解析:从串口驱动到Linux驱动模型
    Win10自带Ubuntu子系统的安装与配置
    关于嵌入式C代码优化的几种方法
    2020软考高级系统分析师,你想知道的全在这
    libpng warning: iCCP: known incorrect sRGB profile
    pycharm中导入pygame库失败及解决办法
    pycharm中导入pygame等第三方库
  • 原文地址:https://www.cnblogs.com/Blogwj123/p/16087550.html
Copyright © 2020-2023  润新知