Windows上的Apache + Django无法启动

r7knjye2  于 2022-11-16  发布在  Apache
关注(0)|答案(2)|浏览(160)

我试图在Windows上用Django设置Apache,但似乎不起作用。
我的settings.py

ALLOWED_HOSTS = ['localhost', '127.0.0.1']

我的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()

我的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"

我的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>

我试过打开http://localhost:30080/,但它说网站无法访问。我也试过将端口更改为 *8080,但没有任何效果,日志中没有错误,它们是空的。没有Django配置,Apache可以工作。语法正常。

wlwcrazw

wlwcrazw1#

在此期间,我不断尝试新的配置。在阅读了一些github问题后,我的问题得到了解决。更具体地说,这一个:
https://github.com/GrahamDumpleton/mod_wsgi/issues/695#issuecomment-864748241
因此,继续重新安装Python,并选择“为所有用户安装”标志。
此外,wsgi.py文件应按照OP问题中所示进行更新。
更新了httpd.conf文件以反映此更改,这次服务器工作正常。

6pp0gazn

6pp0gazn2#

有多种解决方案可以在windows上运行带有apache的django应用程序。
1.使用下面的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()

运行mod_wsgi-express模块配置之后
这将输出如下所示的内容:

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"

复制这些行并粘贴到conf\httpd. conf的末尾。然后打开vhost的配置文件并写入:

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.我建议使用waitress作为在Windows上运行wsgi的一个很好的解决方案。

waitress-serve --listen=*:8000 yourapp.wsgi:application
and use ProxyPass http://localhost:8000 in your apache configuration

让我知道如果它是确定的。

相关问题