Php 使用Apache将请求重定向到Django应用程序

Php 使用Apache将请求重定向到Django应用程序,php,python,django,apache,mod-wsgi,Php,Python,Django,Apache,Mod Wsgi,我有一个Apache web服务器和一个PHP应用程序。一切都很好。此时的httpd.conf文件如下所示: ... LoadModule wsgi_module modules/mod_wsgi.so ... Listen 8080 DocumentRoot "c:/Apache242/htdocs" <Directory "c:/Apache242/htdocs"> Options Indexes FollowSymLinks AllowOverride All

我有一个Apache web服务器和一个PHP应用程序。一切都很好。此时的
httpd.conf
文件如下所示:

...
LoadModule wsgi_module modules/mod_wsgi.so
...
Listen 8080
DocumentRoot "c:/Apache242/htdocs"
<Directory "c:/Apache242/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
Listen 8080
<VirtualHost *:8080>
    DocumentRoot "c:/Apache242/htdocs"
    <Directory c:/Apache242/htdocs>
        Options Indexes FollowSymLinks
        AllowOverride None
        Order Deny,Allow
        Allow from all
    </Directory>
    DirectoryIndex index.php
</VirtualHost>
正如您所看到的,我运行的是一个非生产服务器,但它工作得很好。当我转到127.0.0.1:8081时,我看到从索引视图返回的结果:

def index(request):
    return HttpResponse("Hello, world")
所以,这只是一个简单的Hello world页面,它可以正常工作。现在我想要的是使用Apache服务器,这样当我转到
localhost:8080/django
时,我会看到非常相同的Hello world页面。我不知道如何实施这一点。许多解决方案都基于虚拟主机,但我不想配置它们(只是因为虚拟主机在我的情况下不起作用)。所以如果你有任何建议,欢迎你。谢谢

编辑

我设法为我的PHP应用程序创建了一个虚拟主机。因此,httpd-vhosts.conf文件如下所示:

...
LoadModule wsgi_module modules/mod_wsgi.so
...
Listen 8080
DocumentRoot "c:/Apache242/htdocs"
<Directory "c:/Apache242/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
Listen 8080
<VirtualHost *:8080>
    DocumentRoot "c:/Apache242/htdocs"
    <Directory c:/Apache242/htdocs>
        Options Indexes FollowSymLinks
        AllowOverride None
        Order Deny,Allow
        Allow from all
    </Directory>
    DirectoryIndex index.php
</VirtualHost>
编辑

我似乎尝试了几十种虚拟主机配置,但都不起作用。这是我尝试的最后一种配置:

Listen 8080
WSGIPythonPath C:/Apache242/htdocs/django
<VirtualHost *:8080>
    DocumentRoot "C:/Apache242/htdocs/django"
    ServerName localhost
    WSGIScriptAlias / C:/Apache242/htdocs/django/accent/wsgi.py
    <Directory C:/Apache242/htdocs/django/accent>
    <Files wsgi.py>
        Order deny,allow
        Allow from all
    </Files>
    </Directory>
    LogLevel warn
</VirtualHost>

但我不知道如何修复它。另外,我没有用于Python的虚拟环境,我只是在全局范围内安装了它。

您可以在根php目录中尝试一个htaccess文件,我不确定,但您可以这样做:

Options -Indexes
Options -Multiviews
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^/django(.*)$ http://127.0.0.1:8081$1 [L,QSA]

您是否尝试使用path/django上的条件或重写规则进行重定向?我不确定如何实现这一点。我建议首先将您的Python版本升级到新版本。对于较旧的Python安装,使用为较新Python版本编译的mod_wsgi模块通常不是一个好主意,即使只是补丁级别的差异。2.7.6是因为它正在为该版本找到python27.dll。您可能在某一时刻为所有用户安装了2.7.6,但在安装2.7.12时,它只为您自己或其他人安装。该运行时消息是因为使用的dll表明它是该版本。这就是为什么我说要升级,不是升级到Python3,而是升级到最新的2.7。建议您跟踪安装了多少Python版本。如果找到2.7.12的正确dll,请在
wsgi_module
LoadModule
之前添加一行
Loadfile“c:/path/to/python27.dll
。这将首先强制加载。同时将
WSGIPythonHome“c:/path/to设置为“
加载模块
之后,路径指向Python安装位置。从解释器中,
sys.prefix
将为它提供什么。这和
LoadFile
将确保使用正确的安装。对不起,如果我没有强调if-well,我不想使用Django默认服务器,它不适用于生产。这就是为什么我将wsgi_模块添加到httpd.conf.ok,然后将Listen 8080更改为Listen 8081,然后重新启动Apache,我是否应该指定任何连接到wsgi的内容?我这样问是因为请求被重定向到Django应用程序。这不仅仅是另一个PHP脚本,它应该处理这个请求,所以您需要为此创建一个虚拟主机,您不能为多个站点异常使用一个conf,谢谢。现在看来,我成功地创建了一个虚拟主机。现在,我的PHP应用程序在虚拟主机上运行。没关系,我马上就发布它的内容,你们能分享一下Django应用程序的虚拟主机是什么样子的吗?
Listen 8080
WSGIPythonPath C:/Apache242/htdocs/django
<VirtualHost *:8080>
    DocumentRoot "C:/Apache242/htdocs/django"
    ServerName localhost
    WSGIScriptAlias / C:/Apache242/htdocs/django/accent/wsgi.py
    <Directory C:/Apache242/htdocs/django/accent>
    <Files wsgi.py>
        Order deny,allow
        Allow from all
    </Files>
    </Directory>
    LogLevel warn
</VirtualHost>
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "accent.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Options -Indexes
Options -Multiviews
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^/django(.*)$ http://127.0.0.1:8081$1 [L,QSA]