仅允许 Apache 中的某些文件

仅允许 Apache 中的某些文件

我使用 django 构建的应用程序在 linux 服务器上运行。我在此应用程序中执行的一个过程的结果是,file.txt 在此目录中创建:/home/用户/应用程序/媒体/文件.txt

我的目标是从运行该程序的自己的机器上访问这个创建的文件。(我在 macOsx 上使用该应用程序)。当我输入IP:/home/user/app/media/file.txt在浏览器上,我无法访问文件中的文本。我该怎么办?提前致谢。

注意:我将以下代码插入到conf.文件中/etc/apache2/.conf但没有希望。

<Directory /home/user/app/media>
   <Files file.txt>
       Require all granted
   </Files>
 </Directory>

答案1

您提供的 URL 看起来有点奇怪,在“IP”后面有一个“:”。此外,您应该使用自己的 URL(例如“myapp.local”)设置虚拟主机,并将其放入 /etc/hosts 文件中(127.0.0.1 myapp.local)。

请找到 Apache 的配置虚拟主机下面。将其放入/etc/apache2/sites-available/myapp.conf并使用激活sudo a2ensite myapp.conf

请注意ServerName:这是您应该在浏览器中输入的 URL。

<VirtualHost *:80>
# This config will serve the built version of the production version of the crisisgame.molema.org website for local use with local parameters
    ServerAdmin webmaster@localhost
    ServerName myapp.local

    DocumentRoot /home/user/app
    DirectoryIndex index.html

    
    <Directory /home/user/app/>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.myapp.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.myapp.log combined

</VirtualHost>

然后编辑您/etc/hosts并添加:

  127.0.0.1 myapp.local

然后使用 重新启动 Apache2 服务sudo service apache2 restart

如果重新启动服务时出现错误,请按照以下步骤查找错误。这将提升您到具有 root 权限的新 shell,设置 Apache 的环境变量,然后要求 Apache 运行检查(-S 参数)

 martin@debian:~$ sudo -i
 [Password]: 
 root@debian:~# cd /etc/apache2
 root@debian:~# . ./envvars
 root@debian:~# apache2 -S
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server 127.0.1.1 (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost 127.0.1.1 (/etc/apache2/sites-enabled/000-default.conf:1)

注意执行方式envvars:以点-空格-点-斜杠为前缀。这意味着如果脚本envvars 修改环境变量,则它们的值将在当前 shell 中可用(第一个.代表当前 shell)。另请参阅Bash 参考手册,第 3.7.4 章

第一个错误通常没问题。如果此报告显示其他错误,请修复它们。可能查找 Apache2 的配置这里

转到浏览器并输入http://myapp.local/media/file.txt

祝你好运!

问候马丁

相关内容