Django – Serve static contents using Apache webserver


Django does not serve static contents, such as CSS, by itself. it delegates the task to the webserver. This sometimes creates problems for developers when trying to deploy their Django application to a production environment using a different webserver such as Apache or Nginx. Once the Debug mode is turned off in the settings file, you’d find that your website is returning error 404 (file not found) when trying to access the static files.

To fix this issue in apache webserver, you will need to instruct the webserver on how to locate the static files by updating your configuration file as shown below:


<VirtualHost *:80>
        ServerName www.mysite.com
        ServerAlias mysite.com
        ServerAdmin admin@mysite.com
        
        DocumentRoot /path/to/documentroot/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        Alias /media/ /path/to/mysite/media/ 
        Alias /static/ /path/to/mysite/static/


        WSGIDaemonProcess mysite python-path=/var/www/btekniks:/path/to/mysite/project_virtual_environment/lib/python3.5/site-packages
        WSGIApplicationGroup %{GLOBAL}
        WSGIProcessGroup mysite
        WSGIScriptAlias / /path/to/mysite/wsgi.py
</VirtualHost>


About Matthias 33 Articles
I am a Software Engineer from Houston, TX who love to write codes that brings great ideas to live. In my professional life, I have created software for different industries including Oil & Gas, Finance, Service Provider, Cloud Computing and Embedded Systems. When not writting codes, i enjoy travelling, good music and photography. You can reach me at me@matthiasomisore.com.

Be the first to comment

Leave a Reply

Your email address will not be published.


*