[Django]-Apache + Django on Windows does not start

0👍

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.

👤drec4s

0👍

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 conf\httpd.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.

Leave a comment