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
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.
There are multiple solutions to run a django app with apache on windows.
After run mod_wsgi-express module-config
this will output something like the below:
Copy these lines and paste to end of confhttpd.conf. Then open your configuration file for your vhost and write:
All you have to do is to run
Let me know if it is ok.