Mike IT Mike IT - 21 days ago 6
Apache Configuration Question

django static folder is not served in apache2

I have inherited a django application (1.7) and I am supposed to install it on Apache2. Everything runs fine when I do runserver from command prompt but as deploy to apache2/mod_wsgi.

I am pretty sure the STATICFILES_DIRS has been set correctly.

The settings.py is like below:

"""
Django settings for sampleapp project.

Generated by 'django-admin startproject' using Django 1.10.3.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'tf07wvjy+#g&&#s$spi34_c-jno^8_cb0bw=*dx2ni2+w6x-(h'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# ALLOWED_HOSTS = ['0.0.0.0', 'localhost']
ALLOWED_HOSTS = ['192.168.1.36', 'localhost']


# Application definition

INSTALLED_APPS = [

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 'south',
'CordeliaHanelBackend',
'tastypie',
'StudioHanel',
]

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 = 'CordeliaHanelBackend.urls'

PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
# TEMPLATE_DIRS = (
# '/var/www/CordeliaHanelBackend-master/StudioHanel/templates/StudioHanel',
# )

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/var/www/CordeliaHanelBackend-master/StudioHanel/templates/StudioHanel','CordeliaHanelBackend/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 = 'CordeliaHanelBackend.wsgi.application'


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

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

}


# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/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.10/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
# os.path.join(BASE_DIR, "static"),

'/var/www/CordeliaHanelFrontend-master/www/dist/',
# '/var/www/CordeliaHanelFrontend-master/www/app',
# '/var/www/CordeliaHanelBackend-master/StudioHanel/static',
)

# STATIC_ROOT = '/var/www/CordeliaHanelFrontend-master/www/app'


STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

# DEFAULT_FILE_STORAGE = '/var/www/CordeliaHanelFrontend-master/www/app'


SOUTH_DATABASE_ADAPTER = {
'default': 'south.db.sqlite3'
}


and here is the apache2 configuration site-available file.

<VirtualHost *:80>

WSGIDaemonProcess sampleapp python-path=/var/www/CordeliaHanelBackend-master:/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages
WSGIProcessGroup sampleapp
WSGIScriptAlias / /var/www/CordeliaHanelBackend-master/CordeliaHanelBackend/wsgi.py

# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

# 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



# EDIT

Alias /static /var/www/CordeliaHanelFrontend-master/www/dist/
<Directory /var/www/CordeliaHanelFrontend-master/www/dist/>
Order allow,deny
Allow from all
</Directory>


</VirtualHost>

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

Answer

You need to define path to your project in apache's configuration as:

<Directory /path/my/project/www/xyz.com>  # In your case: /var/www/CordeliaHanelBackend-master
    Require all granted
    AllowOverride All
    Options -Indexes
</Directory>