26

I can currently run either Django through mod_wsgi or PHP on my Apache server.

My Django projects run at: http://localhost and source is at C:/django_proj

My PHP projects run at: http://php.localhost and source is at 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.

Here are some relevant lines in 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.conf is:

WSGIScriptAlias / "C:/django_proj/apache/proj.wsgi"
<Directory "C:/django_proj/apache">
    Order allow,deny
    Allow from all
</Directory>

Inside 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?

George Claghorn
  • 26,261
  • 3
  • 48
  • 48
Thierry Lam
  • 45,304
  • 42
  • 117
  • 144

4 Answers4

36

I run dozens of mod_wsgi/Django sites, PHP sites, and a Rails site with a single Apache.

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.

Lets say, for example, I want to run phpMyAdmin on the same domain as a Django site. The config would look something like this:

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.

Also, completely get rid of the <Directory /> block.

Van Gale
  • 43,536
  • 9
  • 71
  • 81
  • That's good to know, but I want to point my Django project to ,y main domain while my PHP project to a sub-domain, for instance: http://localhost --> My Django Proj http://php.localhost --> My PHP Proj Do you know how I can achieve the above, I already have the above running as virtual hosts. – Thierry Lam Jun 20 '09 at 01:51
  • 1
    You need to put your and directives inside the appropriate blocks. In other words, put the WSGIScriptAlias inside the block where ServerName is localhost. I'll make it more clear by editing my answer. – Van Gale Jun 20 '09 at 02:09
  • 1
    Thanks for your help, it's working. I removed the "Alias / C:/web" and it works fine now. I also took out the Directory blocks from httpd.conf. – Thierry Lam Jun 20 '09 at 03:17
  • in django VH part `DocumentRoot "C:/django_proj"` is needed? i think this line raising security problems...? – M.javid Jun 16 '15 at 06:52
  • 1
    @VanGale This is a great answer; can you explain as to why a VHOST is better than an Alias? Or perhaps what is the real difference between the two? Thanks. – Snerd Sep 21 '15 at 21:22
  • You are a genius. It worked like a charm I just had to put Alias path.I was searching for this everywhere in the internet for 2 hours. – Chamath Sandaru Sep 11 '18 at 22:57
8

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.

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.

Sean McSomething
  • 6,376
  • 2
  • 23
  • 28
0

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.

ProxyPass /wordpress !
<Location /wordpress>
    Require all granted
</Location>
kunoo
  • 31
  • 3
0

I had the same problem. Try removing this block <Directory /> in 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.

KalamHavij
  • 101
  • 1
  • 4