【问题标题】:apache2 not serving my django 1.8 static files in development and productionapache2 在开发和生产中没有为我的 django 1.8 静态文件提供服务
【发布时间】:2015-11-06 01:53:00
【问题描述】:

我有一个 django (1.8) 项目,我在 python 3 中使用 Apache2.4 和 libapache2-mod-wsgi-py3 提供服务。到目前为止一切正常,但我的静态文件没有通过。以前,当我在 windows 环境中尝试这个时,一切正常。请帮忙。设置 static_root 和所有模板后,我运行了“manage.py collectstatic”。 以下是相关代码: 设置.py

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
    'kombu.transport.django',   
    'myapp',

)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'primer_suite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(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 = 'primer_suite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/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/1.8/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static_in_pro", "static_root") #where static files are collected
            #"/var/www/example.com/static/" #whats served when we go live,


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static_in_pro", "our_static"), #static project for the file
    #'/var/www/static/',
)

#files in STATIC_DIRS will be synced to STATIC_ROOT when 'collectstatic' is run.


#folder info
#static_in_pro - statics for our project is
#static root - where static files for the project are collected


MEDIA_URL= '/media/'
MEDIA_ROOT= os.path.join(BASE_DIR, "static_in_pro", "media_root")

wsgi.py

import os
import sys
from django.core.wsgi import get_wsgi_application

sys.path.append('/var/www/myprojects/path/to/my/project')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "primer_suite.settings")

application = get_wsgi_application()

myproject.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that



    ServerAdmin name@domain.com
    ServerName myweb.com
    ServerAlias www.myweb.com


    DocumentRoot /var/www/path/to/my/project

    Alias /static/ /var/www/myprojects/path/to/my/project/static-only/static_root   

    <Directory /var/www/myprojects/path/to/my/project/static-only/static_root>
    Require all granted
    </Directory>

    Alias /templates/ /var/www/myprojects/primer_suite/templates/

        <Directory /var/www/myprojects/path/to/my/project/template>
        Require all granted
        </Directory>

    Alias /media/ /var/www/myprojects/path/to/my/project/media/

        <Directory /var/www/myprojects/path/to/my/project/media>
        Require all granted
        </Directory>

    WSGIDaemonProcess primer_suite python-path=/var/www/myprojects:/var/www/myprojects/env/lib/python3.4/site-packages  
    WSGIProcessGroup primer_suite
    WSGIScriptAlias / /var/www/myprojects//path/to/my/project/my_project/wsgi.py


    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

我不知道发生了什么。其他一切都完美无缺。任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: django python-3.x apache2 django-staticfiles static-files


    【解决方案1】:

    您已将您的 STATIC_ROOT(collectstatic 将放置您的文件以供服务的位置)设置为“BASE_DIR/static_in_pro/static_root”,但您已将 Apache 配置为别名 /static/ 为“/var/www/ myprojects/path/to/my/project/static-only/static_root”。我猜“仅静态”应该是“static_in_pro”。

    【讨论】:

    • 感谢您的帮助。非常感激。看了这么久的代码。我没看到这个。
    【解决方案2】:

    尝试使用:

    Alias /static/ /var/www/myprojects/path/to/my/project/static-only/static_root/
    

    在子 URL 上挂载时,两个参数都必须有斜杠,或者都没有。不要在一个上而不是另一个上。

    【讨论】:

    • 非常感谢。看了这么久的代码,没看到这个小错误。
    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 2012-03-01
    • 2017-12-30
    相关资源
    最近更新 更多