开发者

Django Static File Hosting an Apache

开发者 https://www.devze.com 2023-02-25 17:34 出处:网络
I\'m trying to move a Django site I have been working on out of the dev server stage and into a real hosting environment. For the time being, I\'m just hosting on my per开发者_StackOverflow社区sonal m

I'm trying to move a Django site I have been working on out of the dev server stage and into a real hosting environment. For the time being, I'm just hosting on my per开发者_StackOverflow社区sonal machine. I already have Apache and mod-wsgi installed, but I'm having issues getting static files up. I'm pretty sure it has to do with Apache. Here is my config file for the site:

<VirtualHost *:80>

    ServerName localhost
    ServerAlias daifotis.dyndns.org
    ServerAdmin webmaster@daifotis.com

    DocumentRoot /home/daifotis/code/

    Alias /media/ /home/daifotis/code/feris/sitestatic
    Alias /static/ /home/daifotis/code/feris/sitestatic
    #AliasMatch ^/([^/]*\.css) /home/daifotis/code/feris/sitestatic/$1

    <Directory /home/daifotis/code/feris/sitestatic>
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /home/daifotis/code/feris>
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /home/daifotis/code/feris/jobsite>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIDaemonProcess feris processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup feris

    WSGIScriptAlias / /home/daifotis/code/feris/apache/django.wsgi

    <Directory /home/daifotis/code/feris/apache>
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

I'm trying to host the files from the directory I alias with static. When I try to load the site, all the content comes up but no css. Also, when I hit my url www.server.com/static/, the page displays with the proper content of the directory. What I don't understand though, is why if I click on a link to view a file, it says that URL does not exist. I've been stuck on this for awhile so any help would be much appreciated.


Figured it out. I had an apache config error on this line:

Alias /static/ /home/daifotis/code/feris/sitestatic

I should have written static without the trailing slash. With the trailing slash Apache will not expand the URL path.

Alias /static /home/daifotis/code/feris/sitestatic


I'm assuming you're using Django 1.3, and the 'new' way of serving static files. Could you please show us the settings in your settings.py file relating to MEDIA and STATIC? Both the ROOT AND URL versions. Also show your STATICFILES_DIRS.

The most likely cause is that Django is only configured to serve static files with contrib.staticfiles. It's possible that you'll need to run the management command python manage.py collectstatic to collect all your application static files into a directory ready to be served by apache.

See my answer here regarding settings for using django.contrib.staticfiles.

If your static files exist, 100%, at the directory pointed to by Alias /static/ /home/daifotis/code/feris/sitestatic, then your permissions for apache are probably configured incorrectly. Have you checked the /var/log/httpd/error_log file to see if there is an IOPermissions error when trying to serve the static content?


If you are dealing with Proxypass setup.

<VirtualHost *:80>
ServerName example.us

<Proxy *>
    Allow from localhost
</Proxy>

ProxyPreserveHost On

# Exclude /media/ from proxying
ProxyPass /media/ !
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/

# Serve static files from /media/ directly
Alias /media/ /path/to/media/
<Directory "/path/to/media">
    Options FollowSymLinks
    Require all granted
</Directory>

RewriteEngine on
RewriteCond %{SERVER_NAME} =example.us
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]


I know it's an old question, but I had the same problem and I've tried everything. It turned out that I enabled my app.conf in my apache but forgot to disable 000-default.conf in my apache server.


I knew this is the old thread, but in case if anyone still looking for the answer here is step by step procedures(using django 4.1). Sorry for my bad english.

  1. add the static url, static root and static directory to settings.py

     STATIC_URL = '/static/'
     STATIC_ROOT = os.path.join(BASE_DIR, "static")
     STATICFILES_DIRS = os.path.join(BASE_DIR, "static")
    
  2. run python collectstatic command, this will copy all files from your static folders into the STATIC_ROOT directory

     python manage.py collectstatic
    
  3. update the apache config file

     Alias /robots.txt /path/to/your-prj/static/robots.txt
     Alias /favicon.ico /path/to/your-prj/static/favicon.ico
    
     Alias /media/ /path/to/your-prj/media/
     Alias /static/ /path/to/your-prj/static/
    
     <Directory /path/to/your-prj/static>
     Require all granted
     </Directory>
    
     <Directory /path/to/your-prj/media>
     Require all granted
     </Directory>
    
  4. restart apache

Here is the official documentation in case if you want to check.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号