Nipun Batra
Nipun Batra

Reputation: 11377

Serving html files in /var/www alongwith Django over Apache

I got my Apache Mod_WSGI and Django working following http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/ with a twist that my 000-default site has the contents

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/nipun/webdev/demo/demo

    <Directory /home/nipun/webdev/demo/demo>
    Order allow,deny
    Allow from all


    WSGIDaemonProcess demo.djangoserver processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup demo.djangoserver

    WSGIScriptAlias / /home/nipun/webdev/demo/demo/apache/django.wsgi
    </Directory>



DocumentRoot /var/www
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>


So using this configuration i can basically serve my Django sites like http://localhost/meta for instance where meta calls corresponding view

However now when i try to load index.html(which has some javascript and html code) lying on my /var/www it tries to match that using URL's specified in urls.py of my Django project which i have configured with Apache.

How can i modify the settings so that i shall be able to serve scripts under /var/www and also Django sites on localhost

After trying approach given in first answer So i added this line

Alias / /var/www

above the line containing

WSGIDaemon...

now both django site and my files in /var/www don't work

Solved Changed the 000-default file to something like:

<VirtualHost *:80>  
ServerAdmin webmaster@localhost
DocumentRoot /var/www
Alias /robots.txt /var/www/robots.txt
Alias /testing.php /var/www/testing.php
Alias /index.html  /var/www/index.html
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
DocumentRoot /home/nipun/webdev/demo/demo
WSGIDaemonProcess demo.djangoserver processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup demo.djangoserver
WSGIScriptAlias / /home/nipun/webdev/demo/demo/apache/django.wsgi
<Directory /home/nipun/webdev/demo/demo>
   Order allow,deny
   Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined




</VirtualHost>


To make it even smarter,Alias Matching can be done like

AliasMatch /([^/]+).html /home/nipun/webdev/application_layer/$1.html

Upvotes: 0

Views: 3186

Answers (2)

Graham Dumpleton
Graham Dumpleton

Reputation: 58563

Read documentation in section:

http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive

Use the AddHandler/mod_rewrite/WSGI script fix up method as explained towards the end of that section.

Use that method and if static file exists then it will be served up, else Django instance acts as a fallback resource for all other URLs.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599788

Firstly, I don't know why you keep talking about 'running' your HTML file. It's not a script, even if it has Javascript in it - that's just for the client. There's nothing to run, Apache just needs to serve the file.

Secondly, you will obviously need some way of distinguishing requests that go to Django versus those that get served directly. The easiest way would be to put one or other in a subdirectory - either change the WSGIScriptAlias path from \ to my_django_site, or use a normal Alias to set the location of your static files. If you do the latter, you'll need to put the directive above the WSGI parts so that it's matched first.

Upvotes: 1

Related Questions