托管 Apache 的 Django 静态文件

发布于 2024-11-01 20:19:41 字数 1507 浏览 1 评论 0原文

我正在尝试将我一直在开发的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

塔塔猫 2024-11-08 20:19:41

想通了。我在这一行遇到了 apache 配置错误:

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

我应该编写静态而不带尾部斜杠。使用尾部斜杠,Apache 将不会扩展 URL 路径。

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

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
指尖上得阳光 2024-11-08 20:19:41

我假设您正在使用 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 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?

浮萍、无处依 2024-11-08 20:19:41

如果您正在处理 Proxypass 设置。

<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]

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]
心头的小情儿 2024-11-08 20:19:41

我知道这是旧线程,但如果有人仍在寻找答案,这里是一步一步的过程(使用 django 4.1)。抱歉我的英语不好。

  1. 将静态url、静态根目录和静态目录添加到settings.py

    <前><代码> STATIC_URL = '/静态/'
    STATIC_ROOT = os.path.join(BASE_DIR, "静态")
    STATICFILES_DIRS = os.path.join(BASE_DIR, "静态")

  2. 运行 pythoncollectstatic 命令,这会将静态文件夹中的所有文件复制到 STATIC_ROOT 目录

     python manage.pycollectstatic
    
  3. 更新 apache 配置文件

     别名 /robots.txt /path/to/your-prj/static/robots.txt
     别名 /favicon.ico /path/to/your-prj/static/favicon.ico
    
     别名 /media/ /path/to/your-prj/media/
     别名 /static/ /path/to/your-prj/static/
    
     <目录/path/to/your-prj/static>
     要求全部授予
     
    
     <目录/path/to/your-prj/media>
     要求全部授予
     
    
  4. 重新启动 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.

  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.

那请放手 2024-11-08 20:19:41

我知道这是一个老问题,但我也遇到了同样的问题,并且我已经尝试了一切。
事实证明,我在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文