Man*_*pta 1 django gunicorn google-cloud-platform wagtail google-cloud-run
我有一个 wagtail Web 应用程序,它在本地主机中完美运行,但在生产中,调试设置为 False 并且所有静态文件都不会显示在部署的网站中,我附加了下面的 Dockerfile 代码:
# Use an official Python runtime as a parent image
FROM python:3.7
LABEL maintainer="hello@wagtail.io"
# Set environment varibles
ENV PYTHONUNBUFFERED 1
ENV DJANGO_ENV production
COPY ./requirements.txt /code/requirements.txt
RUN pip install --upgrade pip
# Install any needed packages specified in requirements.txt
RUN pip install -r /code/requirements.txt
RUN pip install gunicorn
# Copy the current directory contents into the container at /code/
COPY . /code/
# Set the working directory to /code/
WORKDIR /code/
RUN python manage.py migrate
RUN useradd wagtail
RUN chown -R wagtail /code
USER wagtail
EXPOSE 8000
CMD exec gunicorn myweb_blog.wsgi:application --bind 0.0.0.0:80 --workers 3
Run Code Online (Sandbox Code Playgroud)
Production.py 设置文件如下:
from .base import *
SECURE_BROWSER_XSS_FILTER=True
FEATURE_POLICY = {
'geolocation': 'none',
}
#CSP_BLOCK_ALL_MIXED_CONTENT=True
#
SECURE_CONTENT_TYPE_NOSNIFF=True
SECURE_FRAME_DENY=True
SECURE_HSTS_SECONDS=2592000
SECURE_HSTS_INCLUDE_SUBDOMAINS=True
SECURE_HSTS_PRELOAD=True
X_FRAME_OPTIONS = 'DENY'
SECURE_REFERRER_POLICY='same-origin'
SECRET_KEY = 'A Secure Secret'
ALLOWED_HOSTS = ['my domain']
DEBUG = False
try:
from .local import *
except ImportError:
pass
Run Code Online (Sandbox Code Playgroud)
--基础.py--
"""
Django settings for myweb_blog project.
Generated by 'django-admin startproject' using Django 3.0.4.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# Application definition
INSTALLED_APPS = [
'home',
'search',
'blog',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'modelcluster',
'taggit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'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',
'django.middleware.security.SecurityMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]
ROOT_URLCONF = 'myweb_blog.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_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 = 'myweb_blog.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
#Database details is filled here
}
# Password validation
# https://docs.djangoproject.com/en/3.0/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/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static'),
]
# ManifestStaticFilesStorage is recommended in production, to prevent outdated
# Javascript / CSS assets being served from cache (e.g. after a Wagtail upgrade).
# See https://docs.djangoproject.com/en/3.0/ref/contrib/staticfiles/#manifeststaticfilesstorage
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# Wagtail settings
WAGTAIL_SITE_NAME = "myweb_blog"
# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
BASE_URL = 'my domain'
Run Code Online (Sandbox Code Playgroud)
请帮助我找到一个解决方案来启动并运行我的网站。多谢
- 更新 -
在正确遵循云运行文档和白噪声( http://whitenoise.evans.io/en/stable/django.html)后,问题已解决。
对于静态文件,我推荐使用Whitenoise 。
https://github.com/GoogleCloudPlatform/django-demo-app-unicodex是在 Google Cloud Run 上运行 Django(以及 Wagtail)的绝佳起点。其中包括有关配置云存储桶的部分。
小智 5
Django 不是为在生产环境中提供静态文件和媒体文件而构建的
由于您已经使用 Google Cloud Run,因此解决问题的一个简单方法是通过Cloud Storage 存储桶等 Google Cloud 选项提供静态文件
或者,您可以在 AWS(Amazon Web Services)上设置一个免费帐户并使用 S3 存储桶,我发现这要容易得多。
以下是在 AWS 上提供 Django 媒体文件的快速参考指南
归档时间: |
|
查看次数: |
2750 次 |
最近记录: |