Apache 没有从正确的目录提供静态文件

Apache 没有从正确的目录提供静态文件

我实际上不确定主题的标题是否真的是我的问题的根源,但有严重的迹象表明这一点。我不是 apache 等应用服务器方面的专家,因此非常欢迎任何帮助。

我已经在 Google Cloud VM 机器(具有静态 IP 的 Ubuntu 16.04)中成功安装了 Django 应用程序。我可以访问该页面,但未加载 CSS,缺少 javascript 文件(例如 jquery)并且出现多个 500 内部服务器错误。

为了让您更好地了解错误,我附上了一个屏幕截图在此处输入图片描述

我首先想到的是我没有将静态文件放在应用服务器的正确文件夹中 (www/../)。但事实并非如此。

然后我想也许 www 文件夹没有正确的权限。但事实并非如此。用户被正确设置为:www-data。

我的 apache2 的 sites-enabled 文件夹包含我的 project.conf 文件。

然后我尝试运行这个命令:

     /usr/sbin/apache2 -S

我得到了这样的答复:

VirtualHost configuration:
*:80                   localhost (/etc/apache2/sites-enabled/geonode.conf:3)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex proxy: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl 
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used

如您所见,主 DocumentRoot 位于以下文件夹中:“/var/www/html” 我想知道这是否是原因。不知何故,apache 试图从错误的目录(正确的目录是 /var/www/project)读取文件。

我尝试通过修改 apache.conf 文件中的参数来更改主 DocumentRoot,但每当我测试更改(/usr/sbin/apache2 -S)时,我仍然会得到旧目录。

我甚至不确定这是否是问题所在。有人知道这是怎么回事吗?

注意 1:我已尝试在本地虚拟机中安装,并且运行良好。注意 2:我已尝试在另一台远程虚拟机中安装,但遇到了同样的问题。 编辑

这是 apache error.log 文件:

[Fri Jun 01 03:42:58.356938 2018] [wsgi:error] [pid 32199:tid 140244594628352] 
Internal Server Error: /favicon.ico
[Fri Jun 01 03:42:58.357214 2018] [wsgi:error] [pid 32199:tid 140244594628352] 
Traceback (most recent call last):
[Fri Jun 01 03:42:58.357347 2018] [wsgi:error] [pid 32199:tid 140244594628352]   
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py", line 
108, in get_response
[Fri Jun 01 03:42:58.357402 2018] [wsgi:error] [pid 32199:tid 140244594628352]     
response = middleware_method(request)
[Fri Jun 01 03:42:58.357450 2018] [wsgi:error] [pid 32199:tid 140244594628352]   
File "/usr/lib/python2.7/dist-packages/django/middleware/common.py", line 62, 
in process_request
[Fri Jun 01 03:42:58.357496 2018] [wsgi:error] [pid 32199:tid 140244594628352]     
host = request.get_host()
[Fri Jun 01 03:42:58.357556 2018] [wsgi:error] [pid 32199:tid 140244594628352]   
File "/usr/lib/python2.7/dist-packages/django/http/request.py", line 94, 
inget_host
[Fri Jun 01 03:42:58.357603 2018] [wsgi:error] [pid 32199:tid 140244594628352]     
if domain and validate_host(domain, allowed_hosts):
[Fri Jun 01 03:42:58.357651 2018] [wsgi:error] [pid 32199:tid 140244594628352]   
File "/usr/lib/python2.7/dist-packages/django/http/request.py", line 571, in 
validate_host
[Fri Jun 01 03:42:58.357696 2018] [wsgi:error] [pid 32199:tid 140244594628352]     
pattern = pattern.lower()
[Fri Jun 01 03:42:58.357762 2018] [wsgi:error] [pid 32199:tid 140244594628352] 
AttributeError: 'NoneType' object has no attribute 'lower'

编辑

这就是我在 sites-enabled 文件夹中的配置文件的样子:

WSGIDaemonProcess geonode user=www-data threads=15 processes=2

<VirtualHost *:80>
  Servername localhost
  ServerAdmin webmaster@localhost

  LimitRequestFieldSize 32760
  LimitRequestLine 32760

  ErrorLog /var/log/apache2/error.log
  LogLevel warn
  CustomLog /var/log/apache2/access.log combined

  WSGIProcessGroup geonode
  WSGIPassAuthorization On
  WSGIScriptAlias / /var/www/geonode/wsgi/geonode.wsgi

  <Directory "/var/www/geonode/">
    Order allow,deny
    Options -Indexes
  ]   Allow from all
    Require all granted 
  </Directory>

  Alias /static/ /var/www/geonode/static/
  Alias /uploaded/ /var/www/geonode/uploaded/
  Alias /robots.txt /var/www/geonode/robots.txt

 <Directory "/var/www/geonode/uploaded/documents/">
   Order allow,deny
   Deny from all
 </Directory>

 <Directory "/var/www/geonode/uploaded/layers/">
   Order allow,deny
   Deny from all
 </Directory>

 <Directory "/var/www/geonode/static_root/">
    Order allow,deny
    Options Indexes FollowSymLinks
    Allow from all
    Require all granted
    IndexOptions FancyIndexing
 </Directory>

 <Directory "/var/www/geonode/uploaded/thumbs/">
    Order allow,deny
    Options Indexes FollowSymLinks
    Allow from all
    Require all granted
    IndexOptions FancyIndexing
 </Directory>

 <Directory "/var/www/geonode/uploaded/avatars/">
    Order allow,deny
    Options Indexes FollowSymLinks
    Allow from all
    Require all granted
    IndexOptions FancyIndexing
 </Directory>

 <Directory "/var/www/geonode/uploaded/people_group/">
    Order allow,deny
    Options Indexes FollowSymLinks
    Allow from all
    Require all granted
    IndexOptions FancyIndexing
</Directory>

<Directory "/var/www/geonode/uploaded/group/">
    Order allow,deny
    Options Indexes FollowSymLinks
    Allow from all
    Require all granted
    IndexOptions FancyIndexing
</Directory>

<Proxy *>
  Order allow,deny
  Require all granted 
  Allow from all
</Proxy>

  ProxyPreserveHost On
  ProxyPass /geoserver http://localhost:8080/geoserver
  ProxyPassReverse /geoserver http://localhost:8080/geoserver
</VirtualHost>

答案1

尝试像DocumentRoot这样设置:

<VirtualHost *:80>
    DocumentRoot /var/www/project
</VirtualHost>

相关内容