0

I am using Django 1.8, python 3.4.3 and Apache 2.4.7, I have tried a lot of configurations and I can't see what I'm doing wrong. When I was using debug mode everything but the images was running perfect, and I solved the images issue by using this on urls.py:

url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
                        {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),

Later I turned DEBUG=False and the Images stopped working, thats why started to read about serving Static and Media files on Apache. And with the basic configurations everything seemed to be working right, but the images stopped working again, and after trying some configurations My App CSS and JS just stopped working, so I used collectstatic and after that, also the Admin lost its CSS and Js, but when I turn it True again, CSS and JS starts working again on both, App and Admin.

Now on DEBUG=False nothing is working, admin Static, my app Static files, and media files, nothing.

These are my settings:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))

MEDIA_ROOT = PROJECT_ROOT + '/archivos/'
MEDIA_URL = '/archivos/'  #put whatever you want that when url is rendered it will be /archivos/imagename.jpg

DEBUG =  True #False

ALLOWED_HOSTS = ['localhost','127.0.0.1'] 

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

This is my /etc/apache2/sites-enabled/000-default.conf file:

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>



<VirtualHost *:80>
    ServerName django.myproject
    Alias /static /home/developer/myproject/static

    <Directory /home/developer/myproject/static>
        Require all granted
    </Directory>

    Alias /archivos/ /home/developer/myproject/archivos/

    <Directory /home/developer/myproject/archivos>
        Require all granted
    </Directory>

    <Directory /home/developer/myproject/myapp>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess myproject python-path=/home/developer/myproject:/home/developer/myproject/projectvenv/lib/python3.4/site-packages
    WSGIProcessGroup myproject
    WSGIScriptAlias / /home/developer/myproject/projectsystem/wsgi.py


</VirtualHost>

If I turn Debug=True, CSS and JS of Admin and App starts working again, only the images (media files) don't.

elG
  • 539
  • 1
  • 6
  • 20
  • This issue is commonly caused by folder/file permissions on the static files directory. If you run collectstatic as root, for example, Apache won't be able to access the files. Check for permissions errors in Apache logs. What OS are you using? – Nostalg.io Aug 12 '16 at 00:51
  • @Nostalg.io I checked access and error logs on `/var/log/apache2`, there are like 5 files of each, but nothing relevant, I think. In access there are two lines that repeats, one says something about a `dummy connection` and the other about `fav.ico`, and in errors I just see this one repeating on every file `Could not reliably determine the server's fully qualified domain name` . And I'm using Ubuntu 14.04. And thankyou, that probably would never have crossed my mind. – elG Aug 12 '16 at 01:25
  • @Nostalg.io I forgot to check one of the error files, there are a lot of `AssertionError: assert tlock is not None` and also `Exception ignored in: ` – elG Aug 12 '16 at 01:31
  • Hmm...That doesn't explain the static files issue. When using Apache to serve static files, you are actually bypassing python/django entirely. You are using Apache as a file server for any URL mapped to `/static/`. Are you sure there's nothing else relevant in `/var/log/apache2/error.log`? – Nostalg.io Aug 12 '16 at 01:53
  • Also, in my .conf file, I append the trailing slash for Alias, e.g. `Alias /static/ /home/developer/myproject/static/` – Nostalg.io Aug 12 '16 at 01:55
  • @Nostalg.io I just found a warning `mod_wsgi: Compiled for Python/3.4.0. mod_wsgi: Runtime using Python/3.4.3.` but yo say that apache bypasses django, probably this isn't relevant. I was trying to upload my errors.log to mega, but I can't enter to the site, it just don't loads. I'm going to try with that slash. – elG Aug 12 '16 at 02:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120753/discussion-between-nostalg-io-and-elg). – Nostalg.io Aug 12 '16 at 02:22

1 Answers1

0

Thanks to the help of user @Nostalg.io (find him on the comments of the question) I have found a solution (well, he found it), there was a lot of bad configurations and permission errors.

First of all, I was using 127.0.0.1:8000 to access to my application, and that was a DJango URL, that's why files weren't served, I had to use the apache one. Also, had two virtualhosts on the same file, I deleted the first one. And as I'm using Ubuntu 14.04, had to add a host to /etc/hosts like this: 127.0.0.1 django.myproject notice that this is my server name on Virtualhost. Later tried to enter to django.myproject but I had permission error, for that I used this: chown -R www-data:www-data /home/developer/myproject

After that, I still had permission error, that was because this wsgi address:

 <Directory /home/developer/myproject/myapp>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

Didn't matched with this one:

 WSGIScriptAlias / /home/developer/myproject/projectsystem/wsgi.py

wsgi.py Is in projectystem folder, so I changed that.

After that, I had 500 error, and it was something about /var/www/, apache couldn't acces to log files, so I added this inside my virtualhost configuration:

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

And just to try, I changed /var/www owner by following the instructions given here: errno 13 permission denied

And also my "media" directory which is "archivos" wasn't well adressed, I changed It's address and now everything is working :D.

I really have to thank @Nostalg.io because he sent to me everything that I have used to solve this problem, and also he took the time to read all my error logs.

Community
  • 1
  • 1
elG
  • 539
  • 1
  • 6
  • 20