如何在一台 Apache 服务器上同时运行 Django 和 PHP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1020390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run Django and PHP together on one Apache server?
提问by Thierry Lam
I can currently run either Django through mod_wsgi or PHP on my Apache server.
我目前可以通过 mod_wsgi 或 PHP 在我的 Apache 服务器上运行 Django。
My Django projects run at: http://localhostand source is at C:/django_proj
我的 Django 项目运行在:http://localhost并且源在 C:/django_proj
My PHP projects run at: http://php.localhostand source is at C:/web
我的 PHP 项目运行在:http://php.localhost和源在 C:/web
If I turn both on, php.localhost and localhost go to the Django project. I've already set them up through Apache virtual hosts.
如果我同时打开,php.localhost 和 localhost 将转到 Django 项目。我已经通过 Apache 虚拟主机设置了它们。
Here are some relevant lines in httpd.conf:
以下是 httpd.conf 中的一些相关行:
DocumentRoot "C:/web"
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
<Directory "C:/web">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "C:/django_proj">
Order allow,deny
Allow from all
</Directory>
Include "C:/django_proj/apache/apache_django_wsgi.conf"
The relevant lines in apache_django_wsgi.confis:
中的相关行apache_django_wsgi.conf是:
WSGIScriptAlias / "C:/django_proj/apache/proj.wsgi"
<Directory "C:/django_proj/apache">
Order allow,deny
Allow from all
</Directory>
Inside httpd-vhosts.conf:
在 httpd-vhosts.conf 中:
<Directory C:/web>
Order Deny,Allow
Allow from all
</Directory>
<Directory C:/django_proj>
Order Deny,Allow
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot "C:/django_proj"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/web"
ServerName php.localhost
</VirtualHost>
My PHP project is current inaccessible. Does anyone have any ideas what I'm missing?
我的 PHP 项目当前无法访问。有没有人有任何想法我错过了什么?
回答by Van Gale
I run dozens of mod_wsgi/Django sites, PHP sites, and a Rails site with a single Apache.
我用一个 Apache 运行了几十个 mod_wsgi/Django 站点、PHP 站点和一个 Rails 站点。
It's mostly done using virtual hosts but I have some that are running both on the same domain.
它主要是使用虚拟主机完成的,但我有一些在同一个域上运行。
You just need to put your WSGIScriptAlias /...after any other Location/Alias directives.
你只需要把你的WSGIScriptAlias /...任何其他位置/别名指令放在后面。
Lets say, for example, I want to run phpMyAdmin on the same domain as a Django site. The config would look something like this:
比方说,例如,我想在与 Django 站点相同的域上运行 phpMyAdmin。配置看起来像这样:
Alias /phpmyadmin /full/path/to/phpmyadmin/
<Directory /full/path/to/phpmyadmin>
Options -Indexes
...etc...
</Directory>
WSGIScriptAlias / /full/path/to/django/project/app.wsgi
<Directory /full/path/to/django/project>
Options +ExecCGI
...etc...
</Directory>
Edit:
编辑:
Your configuration should look something like this:
您的配置应如下所示:
<VirtualHost *:80>
DocumentRoot "C:/django_proj"
ServerName localhost
WSGIScriptAlias / "C:/django_proj/apache/proj.wsgi"
<Directory "C:/django_proj/apache">
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/web"
ServerName php.localhost
Alias / C:/web
<Directory C:/web>
Options Indexes FollowSymLinks
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>
</VirtualHost>
You don't need those <Directory>directives in http.conf... do all your configuration in the Virtual hosts.
你不需要这些<Directory>指令http.conf......在虚拟主机中完成所有配置。
Also, completely get rid of the <Directory />block.
此外,彻底摆脱<Directory />障碍。
回答by Sean McSomething
Your WSGIScriptAlias / ...directive is telling Apache that everything request starting with "/" should get fed through Django's WSGI handler. If you changed that to read WSGIScriptAlias /django-proj/ ...instead, only requests starting with "/django-proj" would get passed into Django.
您的WSGIScriptAlias / ...指令告诉 Apache,所有以“/”开头的请求都应该通过 Django 的 WSGI 处理程序提供。如果您将其更改为 read WSGIScriptAlias /django-proj/ ...,则只有以“/django-proj”开头的请求才会被传递到 Django。
An alternative would be to start setting up virtual hosts for each project. This way you could configure Apache to put each project at the / of it's own domain, and you wouldn't need to worry about the configuration for one project affecting one of your other projects.
另一种方法是开始为每个项目设置虚拟主机。通过这种方式,您可以将 Apache 配置为将每个项目放在其自己的域的 / 中,并且您无需担心一个项目的配置会影响您的其他项目之一。
回答by KalamHavij
I had the same problem.
Try removing this block <Directory />in httpd-conf.
我有同样的问题。尝试<Directory />在 httpd-conf 中删除此块。
Include httpd-vhost.conf and and try puting my WSGIScriptAlias / "/somewhere/file.wsgi"in virtual host section of httpd-vhosts which listens to port 80.
包括 httpd-vhost.conf 并尝试将 myWSGIScriptAlias / "/somewhere/file.wsgi"放入侦听端口 80 的 httpd-vhosts 的虚拟主机部分。
回答by kunoo
I would like to add that if you are using Apache ProxyPass, it's possible to deny certain URL patterns so that it falls to mod_php.
我想补充一点,如果您使用的是 Apache ProxyPass,则可能会拒绝某些 URL 模式,使其落入 mod_php。
ProxyPass /wordpress !
<Location /wordpress>
Require all granted
</Location>

