skip to Main Content

I have looked through numerous tutorials and posts related to Apache and Django on Windows to try to solve my issue. I can run the Django project via the development server and know that my Apache install is working. I know that my issue resides in the configuration of Apache to get it to work with Django. My configuration is shown below. I have made sure that the server project folder is accessible to Everyone with read and execute permissions. I have tried setting the paths with the “C:” and without. Setting the server’s filesystem directory access to “Require all granted” (not recommended) does not help. What am I missing?

Django settings.py

ALLOWED_HOSTS = ['*']
WSGI_APPLICATION = 'INDmain.wsgi.application'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

httdp.conf

Include conf/extra/httpd-vhosts.conf # "#" is removed from start of line.
LoadFile "c:/users/user/appdata/local/programs/python/python36/python36.dll"
LoadModule wsgi_module "c:/users/user/appdata/local/programs/python/python36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIPythonHome "c:/users/user/appdata/local/programs/python/python36"

WSGIScriptAlias / "C:/INDmain/main/wsgi.py
WSGIPythonPath "C:/INDmain"
<Directory "C:/INDmain/main">
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

httpd-vhosts.conf

<VirtualHost *:80>
    ServerName localhost
    ServerAlias localhost

    Alias "/" "C:/INDmain/main"
    <Directory "C:/INDmain/main">
        Require all granted
    </Directory>

    Alias "/static" "C:/INDmain/static"
    <Directory "C:/INDmain/static">
        Require all granted
    </Directory>

</VirtualHost>

2

Answers


  1. Chosen as BEST ANSWER

    After getting this up and running in a Docker container running Ubuntu, I was able to get my Windows version to work after copying the project into a directory under the Apache directory and making some configuration changes for the new location. Final configuration files are below.

    Web site/project layout

    Apache24
    --> www
        --> INDapp
            --> INDmain
                --> INDmain
                --> main
                db.sqlite3
                manage.py
    

    Django settings.py

    ALLOWED_HOSTS = ['<ip_address>']
    
    # Application definition
    
    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main.apps.MainConfig',
    
    WSGI_APPLICATION = 'INDmain.wsgi.application'
    STATIC_ROOT = os.path.join(BASE_DIR, "static/")
    

    httdp.conf

    LoadFile "c:/users/rsloma/appdata/local/programs/python/python36/python36.dll"
    LoadModule wsgi_module "c:/users/rsloma/appdata/local/programs/python/python36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
    WSGIPythonHome "c:/users/rsloma/appdata/local/programs/python/python36"
    
    WSGIPythonPath C:/Apache24/www/INDapp/INDmain
    

    httpd-vhosts.conf

    <VirtualHost *:80>
        ServerName localhost
        ServerAlias localhost
        ServerAlias <ip_address>
    
        DocumentRoot C:/Apache24/www/INDapp
    
        Alias /static "C:/Apache24/www/INDapp/INDmain/main/static"  
        <Directory "C:/Apache24/www/INDapp/INDmain/main/static">
            Require all granted
        </Directory>
    
        <Directory "C:/Apache24/www/INDapp/INDmain/INDmain">
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        WSGIScriptAlias / C:/Apache24/www/INDapp/INDmain/INDmain/wsgi.py
    
    </VirtualHost>
    

  2. Granted I only implement on Linux, so the following may not be helpful, but my standard approach would be to create a sitename_co_uk.conf file within /etc/apache2/sites-available

    The following would be the content of the file:

    <VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName sitename.co.uk
        ServerAlias www.sitename.co.uk
        DocumentRoot /var/www/sitename
        WSGIDaemonProcess sitename python-path=/var/www/sitename python-home=/var/www/sitename/env
        WSGIProcessGroup sitename
        WSGIScriptAlias / /var/www/sitename/core/wsgi.py
    
        Alias /robots.txt /var/www/sitename/static/robots.txt
        Alias /favicon.ico /var/www/sitename/static/favicon.ico
    
        Alias /media/ /var/www/sitename/media/
        Alias /static/ /var/www/sitename/static/
    
        <Directory /var/www/sitename/static>
            Require all granted
        </Directory>
    
        <Directory /var/www/sitename/media>
            Require all granted
        </Directory>
    
        WSGIScriptAlias / /var/www/sitename/core/wsgi.py
    
        <Directory /var/www/sitename/core>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    This means I have my site actually in the directory /var/www/sitename'. This directory will contain a virtual environment inenvand themanage.pyfile. Mysettings.py,wsgi.pyetc are within the sub-directorycore`.

    This is enabled using a2ensite sitename_co_uk.conf.

    It should work in a similar fashion on Windows.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search