简体   繁体   English

无法在Apache中提供Django的MEDIA和STATIC文件

[英]Trouble serving MEDIA and STATIC files of Django in Apache

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. 我正在使用Django 1.8,python 3.4.3和Apache 2.4.7,我尝试了许多配置,但看不到我做错了什么。 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: 当我使用调试模式时,除图像之外的所有东西都运行良好,并且我通过在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. 后来我变成DEBUG=False并且图像停止工作,这就是为什么开始阅读有关在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. 在基本配置下,一切似乎都可以正常运行,但是图像又停止了工作,在尝试了一些配置后,我的App CSS和JS才停止工作,所以我使用了collectstatic ,之后管理员也失去了CSS和Js,但是当我再次将其设置为True时,CSS和JS即可在App和Admin上再次开始工作。

Now on DEBUG=False nothing is working, admin Static, my app Static files, and media files, nothing. 现在在DEBUG = False上,什么都没有起作用,管理静态,我的应用程序静态文件和媒体文件,什么也没有。

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: 这是我的/etc/apache2/sites-enabled/000-default.conf文件:

<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. 如果我打开Debug = True,则Admin和App的CSS和JS重新开始工作,只有图像(媒体文件)不起作用。

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. 感谢用户@ Nostalg.io的帮助(在问题的评论中找到他),我找到了一个解决方案(很好,他找到了),存在很多错误的配置和权限错误。

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. 首先,我使用127.0.0.1:8000来访问我的应用程序,而那是DJango URL,这就是为什么不提供文件的原因,我不得不使用apache。 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. 在使用Ubuntu 14.04时,必须将主机添加到/etc/hosts如下所示: 127.0.0.1 django.myproject注意,这是我在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 后来尝试进入django.myproject但是我遇到了权限错误,为此我使用了以下命令: chown -R www-data:www-data /home/developer/myproject

After that, I still had permission error, that was because this wsgi address: 之后,我仍然有权限错误,这是因为该wsgi地址:

 <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. wsgi.py在projectystem文件夹中,所以我更改了它。

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: 在那之后,我出现了500个错误,这是关于/ var / www /的问题,apache无法访问日志文件,因此我在virtualhost配置中添加了该错误:

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 为了尝试,我按照此处给出的说明更改了/var/www所有者: errno 13权限被拒绝

And also my "media" directory which is "archivos" wasn't well adressed, I changed It's address and now everything is working :D. 而且我的“媒体”目录(即“ archivos”)的地址也不尽人意,我更改了地址,现在一切正常: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. 我真的要感谢@ Nostalg.io,因为他向我发送了我用来解决此问题的所有信息,而且他还花了一些时间阅读我的所有错误日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM