-1

I am trying to deploy a Django web app on a newly installed centos7 server. for this i am using apache2.4 with mod_wsgi.

i am stuck at serving the static files and because i am new to this, this is so confusing to me.

here is my configuration files if you can help me with this please.

/etc/httpd/conf/httpd.conf:

(i removed everything i am not using here and removed the commented lines)

Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin admin@serv.com
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel info

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional sites-enabled/*.conf
LoadModule wsgi_module modules/mod_wsgi.so

in my sites-enabled folder, i have only one config file for my project

/etc/httpd/sites-enabled/project.conf:

Alias /favicon.ico /var/www/cvctools/staticfiles/templates/images/favicon.ico
Alias /static/ /var/www/cvctools/staticfiles/
<VirtualHost *:80>
    DocumentRoot /var/www/cvctools
    ServerName proj.ma
    ServerAlias www.proj.ma
    ServerAdmin admin@proj.ma

    <Directory /var/www/cvctools/staticfiles>
        AllowOverride None
        Require all granted
    </Directory>

    <IfModule wsgi_module>
        WSGIScriptAlias / /var/www/cvctools/CapValue/wsgi.py
        WSGIScriptReloading On
        WSGIDaemonProcess CapValue processes=1 threads=1 maximum-requests=5000 display-name=my-wsgi
        WSGIProcessGroup CapValue
        WSGIPythonPath /var/www/cvctools
        <Directory /var/www/cvctools/CapValue>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    </IfModule>
</VirtualHost>

in my project folder located at /var/www/cvctools i have this..

./CapValue/wsgi.py:

import django.core.handlers.wsgi
import os,sys

sys.path.append('/vat/www/cvctools')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CapValue.settings")
application = django.core.handlers.wsgi.WSGIHandler()

my staticfiles settings in ./CapValue/settings.py:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(os.getcwd(), 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(PROJECT_ROOT, 'static'),)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # 'compressor.finders.CompressorFinder',
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
TEMPLATES = [
    {
        'BACKEND' : 'django.template.backends.django.DjangoTemplates',
        'DIRS'    : [os.path.join(STATIC_ROOT, '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',
            ],
        },
    },
]

./CapValue/views.py:

from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic.base import TemplateView
from django.utils.decorators import method_decorator


class IndexView(TemplateView):
    template_name = 'index.html'

    @method_decorator(ensure_csrf_cookie)
    def dispatch(self, *args, **kwargs):
        return super(IndexView, self).dispatch(*args, **kwargs)

./CapValue/urls.py:

#imports...
urlpatterns = [
    #...
    url('^.*$', IndexView.as_view(), name='index')
]

and after running manage.py collectstatic, in my project directory i have staticfiles folder with this contents:

/var/www/cvctools/staticfiles/ :

drwxr-xr-x.  6 admin admin    47 Mar  4 22:36 admin
drwxr-xr-x.  2 admin admin  4096 Mar  4 22:36 css
drwxr-xr-x.  5 admin admin    35 Mar  4 22:36 django_extensions
-rwxr-xr-x.  1 admin admin     0 Mar  4 22:36 human.d41d8cd98f00.txt
-rwxr-xr-x.  1 admin admin     0 Mar  4 22:36 human.txt
drwxr-xr-x.  2 admin admin  4096 Mar  4 22:36 images
drwxr-xr-x. 12 admin admin  4096 Mar  4 22:36 javascript
drwxr-xr-x.  2 admin admin  4096 Mar  4 22:36 notifications
drwxr-xr-x.  6 admin admin    47 Mar  4 22:36 rest_framework
-rwxr-xr-x.  1 admin admin 12925 Mar  4 22:36 staticfiles.json
drwxr-xr-x.  6 admin admin  4096 Mar  4 22:36 templates

but when i'm trying to access the application, in my apache log i get this error:

 [autoindex:error] [pid 3150] [client 192.168.0.126:57623] AH01276: Cannot serve directory /var/www/cvctools/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive

what exactly i am doing wrong ?

Edit

apparently it have something to do with mod_wsgi, beacause i changed my DcumentRoot to /var/www/cvctools/staticfiles/templates then i was able to see my index page, but it was like this:

{% load staticfiles %} {% include 'stylesheets.html' %}
{% include 'navbar.html' %}
{% include 'menu.html' %}
{#
#} {# {% include 'breadcrumb.html' %}#} {#
#}
{% include 'footer.html' %}
{% include 'javascripts.html' %} 

it is not picking my template tags !

Soufiaane
  • 99
  • 5

1 Answers1

0

as i suspected (in my edit for the question) it had something to do with my mod_wsgi setup.

after reviewing the setup process in this link, what i was missing is the mod_wsgi python package. so a simple pip install mod_wsgi solved my problem

Soufiaane
  • 99
  • 5