Linux运行Django

Python -m venv 名称 --》创建虚拟环境

pip show 包名 查看pip的包路径

查看Django安装目录: pip show django

添加软连接:

ln -s /usr/local/python3/lib/python3.7/site-packages/django/bin/django-admin.py /usr/local/bin

运行 django-admin.py 。。。。

注意:

1、在主应用的init下写

import pymysql
pymysql.install_as_MySQLdb()

2、若报错 django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 。。。

打开 Python安装路劲下的python\lib\python3.7\site-packages\django\db\backends\mysql\base.py文件

注释掉

if version < (1, 3, 3):
    raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

在django3.0之后,如果settingsLANGUAGE想设置zh-Hans,需

from django.utils.translation import gettext_lazy as _

LANGUAGES = [
     ('zh-Hans', _('Chinese')),
 ]

LANGUAGE_CODE = 'zh-Hans'

否则会报错:Error :You have provided a value for the LANGUAGE_CODE setting that is not in the LANGUAGES setting

下载uwsgi

Python -m pip install uwsgi

测试是否下载完成 :uwsgi --version 若提示未知命令,配一下软连接

不挂断运行

进入项目目录

nohup python manage.py runserver 0.0.0.0:8000 &

注意:在nohup执行成功后直接点击关闭程序按钮关闭终端,会断掉该命令对应的session,导致nohup对应的进程被通知一起shutdown。所以在使用nohup命令后台运行命令之后,需要使用exit正常退出当前账户,这样才能保证命令一直在后台运行。

uvicorn和circus的用处

Django3部署

推荐: uvicorn + circus/supervisord

Django2部署

推荐: gunicorn + circus/supervisord

sanic部署

推荐: uvicorn + circus

步骤:

安装circus
pip install circus

安装uvicorn
pip install uvicorn

项目中写一个circus.ini
例如:

[watcher:mysite]

cmd = venv/bin/uvicorn --fd $(circus.sockets.misite) server:app

use_sockets = True

numprocesses = 2

[socket:mysite]

host = 0.0.0.0

port = 80

circusd --daemon circus.ini

gunicorn + django 多进程

gunicorn sport.wsgi:application -b 0.0.0.0:8000 -D 必须进入django项目中 -b 绑定端口, -D后台运行 -c 配置文件运行 -w 多开的进程数

gunicorn sport.wsgi -b 0.0.0.0:8000 -D

gunicorn 配置文件 .py文件

import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing

bind = "0.0.0.0:8000"  # 绑定端口

backlog = 521  # 监听队列数量 64-2048

\# chdir = '/home/test/server/bin' # gunicorn 要切换的目的工作目录

worker_class = 'sync' # 使用gevent,模式,还可以使用sync模式,默认sync模式

workers = 3  # 多加的进程数 multiprocessing.cpu_count() 检测服务器的核数

threads = 12 # 每个进程下的线程数 multiprocessing.cpu_count() * 4

loglevel = 'info' # 日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置

access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'

\# accesslog = "/home/log/gunicorn_access.log" # 访问日志文件

\# errorlog = "/home/log/gunicorn_error_log"  # 错误日志文件

accesslog = "-"  # 访问日志文件,“-” 表示标准输出

errorlog = "-" # 错误日志文件,“-” 表示标准输出

proc_name = 'fof_api'  # 进程名

老版本的gunicorn 需要 python:gunicorn 。。。。

Nginx + gunicorn + django

python manage.py collectstatic 生成Django静态资源文件夹

配置nginx.conf,在http中添加

include /etc/nginx/sites-available/*.conf;

server单独拿出来,放到 sites-available 中(与配置文件中的文件夹一致)

配置*.conf 在 sites-available 文件夹中

server {
     listen 80;    # 监听的端口
     server_name  build.2222.com;	# 域名解析
     charset utf-8;   # 解码格式,支持中文
     client_max_body_size 200m;   
     access_log  /var/log/nginx/build-access.log;    # 访问成功日志
     error_log  /var/log/nginx/build-err.log;		# 访问失败日志
     location = /favicon.ico { access_log off; log_not_found off; }  
     location /static/ {
         \#静态文件如js,css的存放目录
         alias /home/myb/myblog/static/;    # 上文生成的静态资源文件路径
     }
     location / {
         proxy_pass http://127.0.0.1:8000; # 这里要配合启动文件使用   安全起见,不让外界直接访问,而是将请求发给nginx,然后由nginx分发
         proxy_redirect     off;
         proxy_set_header   Host                 $http_host;
         proxy_set_header   X-Real-IP            $remote_addr;
         proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
         proxy_set_header   X-Forwarded-Proto    $scheme;
     }
 }

版权声明:本文为weixin_45317738原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。