2

/etc/apache2/site-available/mysite.com

<VirtualHost my_ip_here:80>
     ServerAdmin foo@mysite.com
     ServerName mysite.com
     ServerAlias www.mysite.com

     WSGIScriptAlias / /srv/www/mysite.com/djangoproject/django.wsgi

     <Directory "/srv/www/mysite.com/djangoproject/sitestatic">
        Order allow,deny
        Allow from all
     </Directory>

     AliasMatch /([^/]*\.css) /srv/www/mysite.com/djangoproject/sitestatic/css/$1
     AliasMatch /([^/]*\.js) /srv/www/mysite.com/djangoproject/sitestatic/js/$1

     Alias /media/ /srv/www/mysite.com/djangoproject/sitestatic/

     ErrorLog /srv/www/mysite.com/logs/error.log
     CustomLog /srv/www/mysite.com/logs/access.log combined
</VirtualHost>

/srv/www/mysite.com/djangoproject/settings.py

MEDIA_ROOT = ''

MEDIA_URL = '/media/'

STATIC_ROOT = '/srv/www/mysite.com/djangoproject/sitestatic/'

STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX = '/static/admin/'

Actually I see my site correctly with my css and images BUT when I go to /admin I see admin site without CSS. How can I fix? Also, it's correct my apache configuration for serving css and js and how can I hide the content of dir /media?

-- SOLUTION (thanks to Pratik) --

Maybe, the problem is the order of lines in apache's config.

<VirtualHost my_ip_here:80>
     ServerAdmin foo@site.com
     ServerName site.com
     ServerAlias site.com

     DocumentRoot /srv/www/site.com/cherryproj/templates
     Alias /static/admin /usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/contrib/admin/media/
     Alias /static/ /srv/www/site.com/cherryproj/static/

     WSGIScriptAlias / /srv/www/site.com/cherryproj/django.wsgi
     <Directory "/srv/www/site.com/cherryproj/sitestatic">
        Order allow,deny
        Allow from all
     </Directory>

     ErrorLog /srv/www/site.com/logs/error.log
     CustomLog /srv/www/site.com/logs/access.log combined
</VirtualHost>
Fred Collins
  • 251
  • 1
  • 4
  • 10

2 Answers2

2

The admin media is actually located inside of Django's dist package.

Try putting something like this in Apache's config

Alias /static/admin "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/"

If that does not work view the page source while on the admin page, see the path that the css is trying to be loaded from and point Apache's alias to that path. It is probably a good idea to make a copy for this admin media outside of the dist-packages folder. Also your path is probably going to be different especially if you are using virutalenv.

To hide the contents of your media folder do

<Directory "/srv/www/mysite.com/djangoproject/sitestatic">
  Options -Indexes
  Order allow,deny
  Allow from all
  </Directory>
Pratik Amin
  • 3,303
  • 3
  • 22
  • 19
  • In the source on my html I see: `href="/static/admin/css/base.css"` but if I go to site.com/static/admin/css/base.css I see `Not Found: The requested URL /static/admin/css/base.css was not found on this server.` – Fred Collins Apr 30 '11 at 23:20
  • So you need to tell apache that any requests to sent to /static/admin should be redirected to the path that is in the Alias in my post. Or what you could do is just copy /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/ and name it "admin" inside your media folder – Pratik Amin Apr 30 '11 at 23:41
  • If you make a folder on the level of settings.py call it static, and folder under it called admin and then put the admin media files in there it will work because apache can use that path. – Pratik Amin May 01 '11 at 00:11
  • @Pratik Amin I'm gotta crazying. Should I run `manage.py collectstatic` ? – Fred Collins May 01 '11 at 00:14
  • Personally when I have deployed Django I havent used the STATIC path stuff but just done it manually via a media directory inside the project folder. It looks like your regular site's media is already working. Did you try to add the alias? – Pratik Amin May 01 '11 at 00:26
  • @Pratik Amin check my update. I show you my situation. – Fred Collins May 01 '11 at 00:29
  • Ok so your still not referencing the admin media located inside of django's package. The MEDIA for your website and django's admin backpanel are not in the same place( not the same css,js folders). Right now you have an Alias for your MEDIA_URL folder but not for your "ADMIN_MEDIA_PREFIX". Create another alias that is the same as the one I have in my post (the alias path being /static/admin) – Pratik Amin May 01 '11 at 00:34
  • Try putting the staic/meida alias above the aliasmatch (Do you even need that?). Also since your document root in in your templates folder try copying the static folder into the templates folder (just for testing) – Pratik Amin May 01 '11 at 00:54
  • @Pratik Amin last question, it's ok set DocumentRoot for templates? – Fred Collins May 01 '11 at 01:07
  • My experience with Django is not perfect but It was my understanding that wsgi and urls.py took care off all the mapping of templates. I dont think you need to tell apache where the templates are to use make it work. – Pratik Amin May 01 '11 at 01:09
0

In Ubuntu 12.04.1 LTS Server you have to add this line:

Alias /static/admin "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media"

in your Apache2 config or in VirtualHost.

StandDuPp
  • 101
  • 1