apache2在开发和生产中没有提供我的django 1.8静态文件

时间:2015-08-13 07:48:03

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

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

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

由于

2 个答案:

答案 0 :(得分:0)

您已将STATIC_ROOT设置为&{34; BASE_DIR / static_in_pro / static_root&#34;,这是collectstatic将您的文件提供给服务的地方,但您已将Apache配置为alias / static / to&#34; / var / www / myprojects / path / to / my / project / static-only / static_root&#34;。我想&#34;静态只有#34;应该是&#34; static_in_pro&#34;那里。

答案 1 :(得分:0)

尝试使用:

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

在子URL上安装时,必须在两个参数上都有尾部斜杠,或者两者都没有。不要把它放在一个而不是另一个上。