注意:
1.只对关键点进行整理:
2.python_test是项目名称
环境:
python:v3.10.9
1.部署前,执行
python manage.py check --deploy
根据提示进行部署检查,调整,ssl相关的可以不做调整
2.settings.py最终版本
"""
Django settings for python_test project.
Generated by 'django-admin startproject' using Django 4.1.5.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'python_test.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'python_test.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '123456',
'HOST': 'localhost',
'TEST': { # 测试用例数据库字符集
'CHARSET': 'utf8',
'COLLATION': 'utf8_general_ci',
}
}
}
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0",
"OPTIONS": {
"PASSWORD": "123456",
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SOCKET_CONNECT_TIMEOUT": 5, # 连接redis超时时间,单位为秒
"SOCKET_TIMEOUT": 5, # redis读写操作超时时间,单位为秒
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
}
}
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
STATICFILES_DIR = [
os.path.join(BASE_DIR,'static'),
]
# 增加收集静态文件路径
STATIC_ROOT = '/data/python_test/static/'
3.收集项目静态文件命令
python manage.py collectstatic
4.uwsgi启动配置文件uwsgi.ini
[uwsgi]
#使用nginx连接时使用
#socket=127.0.0.1:8888
#直接做web服务器使用
http=127.0.0.1:8888
#项目目录
chdir=/data/python_test
#项目中wsgi.py文件的目录,相对于项目目录
wsgi-file=python_test/wsgi.py
module=python_test.wsgi:application
# 指定启动的工作进程数
processes=4
# 指定工作进程中的线程数
threads=2
master=True
# 启动使用的用户和组
uid=1000
gid=1000
# 退出自动清空环境变量
vacuum=True
# 设置uwsgi后台运行,用uwsgi.log保存日志信息
daemonize=/var/log/uwsgi/python_test.log
#进程文件
pidfile=/tmp/project-master.pid
#增加新的拓展包扫描路径,如果安装了mysql表,可以 通过python -m mysql 可以查看路径
pythonpath=/home/jacky/.local/lib/python3.10/site-packages
# 静态文件映射
static-map=/static=/data/python_test/static
env=DJANGO_SETTINGS_MODULE=python_test.settings
现在进入uwsgi.ini目录执行
uwsgi --ini uwsgi.ini
运行uwsgi服务器,访问http://127.0.0.1:8888成功
5.配置nginx代理
server{
listen 8008;# nginx监听端口
server_name _;
error_log /var/log/nginx/err_python_test.log;
location /{
uwsgi_pass 127.0.0.1:8888; # uwsgi web服务端口
include /etc/nginx/uwsgi_params;
}
location /static/ { # 静态文件路径
root /data/python_test/;
}
}
访问http://localhost:8888成功