托管 Apache 的 Django 静态文件
我正在尝试将我一直在开发的 Django 站点从开发服务器阶段移至真正的托管环境中。目前,我只是在我的个人机器上托管。我已经安装了 Apache 和 mod-wsgi,但在获取静态文件时遇到问题。我很确定这与 Apache 有关。这是我的站点配置文件:
<VirtualHost *:80>
ServerName localhost
ServerAlias daifotis.dyndns.org
ServerAdmin [email protected]
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>
我正在尝试托管我用 static 别名的目录中的文件。当我尝试加载网站时,所有内容都会出现,但没有 css。另外,当我点击 URL www.server.com/static/ 时,页面会显示目录的正确内容。但我不明白的是,为什么如果我单击链接查看文件,它会说 URL 不存在。我已经被困在这个问题上有一段时间了,所以任何帮助将不胜感激。
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 personal 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 [email protected]
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
想通了。我在这一行遇到了 apache 配置错误:
我应该编写静态而不带尾部斜杠。使用尾部斜杠,Apache 将不会扩展 URL 路径。
Figured it out. I had an apache config error on this line:
I should have written static without the trailing slash. With the trailing slash Apache will not expand the URL path.
我假设您正在使用 Django 1.3 以及提供静态文件的“新”方式。您能否向我们展示您的
settings.py
文件中与 MEDIA 和 STATIC 相关的设置? ROOT 和 URL 版本。还显示您的 STATICFILES_DIRS。最可能的原因是 Django 仅配置为使用
contrib.staticfiles
提供静态文件。您可能需要运行管理命令python manage.pycollectstatic
将所有应用程序静态文件收集到准备由 apache 提供服务的目录中。有关使用 django.contrib.staticfiles 的设置,请参阅我的回答 。
如果您的静态文件 100% 存在于
Alias /static/ /home/daifotis/code/feris/sitestatic
指向的目录中,那么您的 apache 权限可能配置不正确。您是否检查了/var/log/httpd/error_log
文件以查看在尝试提供静态内容时是否存在 IOPermissions 错误?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 commandpython 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?如果您正在处理 Proxypass 设置。
If you are dealing with Proxypass setup.
我知道这是旧线程,但如果有人仍在寻找答案,这里是一步一步的过程(使用 django 4.1)。抱歉我的英语不好。
将静态url、静态根目录和静态目录添加到settings.py
<前><代码> STATIC_URL = '/静态/'
STATIC_ROOT = os.path.join(BASE_DIR, "静态")
STATICFILES_DIRS = os.path.join(BASE_DIR, "静态")
运行 pythoncollectstatic 命令,这会将静态文件夹中的所有文件复制到 STATIC_ROOT 目录
更新 apache 配置文件
重新启动 apache
这里是官方文档如果你想检查的话。
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.
add the static url, static root and static directory to settings.py
run python collectstatic command, this will copy all files from your static folders into the STATIC_ROOT directory
update the apache config file
restart apache
Here is the official documentation in case if you want to check.
我知道这是一个老问题,但我也遇到了同样的问题,并且我已经尝试了一切。
事实证明,我在 apache 中启用了 app.conf,但忘记在 apache 服务器中禁用 000-default.conf。
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.