0

I am switching from the Django development server to Apache for production. To this end, I installed modwsgi and specified the following in apache2.conf.

# Run my django application.
WSGIScriptAlias / /home/david/registration/registration/wsgi.py
WSGIPythonPath /home/david/registration

<Directory /home/david/registration/registration>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

When I restart Apache, I find that my application loads in my browser, but none of the CSS or images do.

Previously, I had been using staticfiles to manage my static files. I had these settings.

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/home/david/static/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

The static files used to working, but not any more. Why does switching to Apache prevent my static files from loading?

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206

1 Answers1

1

The best option for work with apache2 and wsgi and django is use a new file in /etc/apache2/sites-enabled/mynewsite.apache (the extension doesn´t matter)

The file must look like this:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin diego@diegue.us
    ServerName yourproject.com.co
    Alias /admin/media/ /path/to/your/admin/media/
    Alias /media/ /path/to/your/media/
    Alias /static/ /path/to/your/static/collected/files/

    <Directory /path/to/your/admin/media/>
        Order deny,allow
        Allow from all
    </Directory>

    <Directory /path/to/your/media/>
        Order deny,allow
        Allow from all
    </Directory>

    <Directory /path/to/your/static/collected/files/>
        Order deny,allow
        Allow from all
    </Directory>

    WSGIScriptReloading On
    WSGIDaemonProcess yourproject python-path=/path/of/packages/for/python # (this is because I use virtualenvwrapper)
    WSGIProcessGroup yourproject
    WSGIApplicationGroup yourproject
    WSGIPassAuthorization On

    WSGIScriptAlias / /path/to/your/yourproject.wsgi
    ErrorLog /var/log/apache2/yourproject-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel debug

    CustomLog /var/log/apache2/yourproject-access.log combined

</VirtualHost>

You need to tell apache to serve your django application but also you need tell it that Apache serves the files.

diegueus9
  • 29,351
  • 16
  • 62
  • 74