skip to Main Content

I am trying to set up Apache with Django on Windows but it does not seem to work.

My settings.py

ALLOWED_HOSTS = ['localhost', '127.0.0.1']

My wsgi.py


import os
import sys
from django.core.wsgi import get_wsgi_application
from pathlib import Path

# Add project directory to the sys.path
path_home = str(Path(__file__).parents[1])
if path_home not in sys.path:
    sys.path.append(path_home)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mrt.settings'

application = get_wsgi_application()

My httpd.conf

Listen 30080
Listen 8080


LoadFile "C:/Users/user/AppData/Local/Programs/Python/Python37/python37.dll"
LoadModule wsgi_module "c:/users/user/pycharmprojects/djangoapi/venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/user/pycharmprojects/djangoapi/venv"
WSGIPythonPath "c:/users/user/pycharmprojects/djangoapi/venv/Lib/site-packages"

My httpd-vhosts.conf


<VirtualHost *:30080>
    ServerName localhost
    WSGIPassAuthorization On
    ErrorLog "c:/users/user/pycharmprojects/djangoapi/mrt/logs/apache.error.log"
    CustomLog "c:/users/user/pycharmprojects/djangoapi/mrt/logs/apache.access.log" combined
    WSGIScriptAlias /  "c:/users/user/pycharmprojects/djangoapi/mrt/mrt/wsgi.py"
    <Directory "c:/users/user/pycharmprojects/djangoapi/mrt/mrt">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    Alias /static "c:/users/user/pycharmprojects/djangoapi/mrt/static"
    <Directory "c:/users/user/pycharmprojects/djangoapi/mrt/static">
        Require all granted
    </Directory>  
</VirtualHost>


I have tried to open http://localhost:30080/ but it says the website is not reachable.
I also have tried to change Ports to *8080 but without any effect, no error appears in logs, they are empty. Without Django config Apache works. Syntax is Ok.

2

Answers


  1. In the meantime kept trying new configurations. Ended up solving my problem after reading through some github issues. More specifically this one:

    https://github.com/GrahamDumpleton/mod_wsgi/issues/695#issuecomment-864748241

    So, went ahead and re-installed Python and selected the ‘Install for all users’ flag.

    Also, the wsgi.py file should be updated as shown in the OP question.

    Updated the httpd.conf file to reflect this changes and this time the server worked correctly.

    Login or Signup to reply.
  2. There are multiple solutions to run a django app with apache on windows.

    1. Use the below wsgi.py
    import os
    import sys
    import site
    from django.core.wsgi import get_wsgi_application
    
    # add python site packages, you can use virtualenvs also
    site.addsitedir("C:/Program files/python39/Lib/site-packages")
    
    # Add the app's directory to the PYTHONPATH 
    sys.path.append('c:/users/user/pycharmprojects/djangoapi/mrt') 
    sys.path.append('c:/users/user/pycharmprojects/djangoapi/mrt/mrt')  
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'mrt.settings' 
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mrt.settings")  
     
    application = get_wsgi_application()
    

    After run mod_wsgi-express module-config

    this will output something like the below:

    LoadFile "c:/Program Files/python/python39/python39.dll"
    LoadModule wsgi_module "c:/Program Files/python39/lib/site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd"
    WSGIPythonHome "c:/Program Files/python/python39"
    

    Copy these lines and paste to end of confhttpd.conf. Then open your configuration file for your vhost and write:

     ServerName localhost
        WSGIPassAuthorization On
        ErrorLog "c:/users/user/pycharmprojects/djangoapi/mrt/logs/apache.error.log"
        CustomLog "c:/users/user/pycharmprojects/djangoapi/mrt/logs/apache.access.log" combined
        WSGIScriptAlias /  "c:/users/user/pycharmprojects/djangoapi/mrt/mrt/wsgi.py"
        <Directory "c:/users/user/pycharmprojects/djangoapi/mrt/mrt">
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        Alias /static "c:/users/user/pycharmprojects/djangoapi/mrt/static"
        <Directory "c:/users/user/pycharmprojects/djangoapi/mrt/static">
            Require all granted
        </Directory>  
    
    1. I would recomend using waitress as a good solution on running wsgi on Windows.
      All you have to do is to run
    waitress-serve --listen=*:8000 yourapp.wsgi:application
    and use ProxyPass http://localhost:8000 in your apache configuration
    

    Let me know if it is ok.

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