为什么apache在系统根目录而不是项目目录上找到文件,出现FileNotFound之类的错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么apache在系统根目录而不是项目目录上找到文件,出现FileNotFound之类的错误相关的知识,希望对你有一定的参考价值。

Apache在我的项目目录/var/www/web_app_video_synopsis上找不到文件,但在系统根目录'/'上找到文件/目录>

当我提示URL http://<server IP:5000>

Apache收到错误cat /var/log/apache2/error.log

[Mon May 18 09:14:42.292029 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] ERROR:main:Exception on / [GET]
[Mon May 18 09:14:42.292060 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] Traceback (most recent call last):
[Mon May 18 09:14:42.292064 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
[Mon May 18 09:14:42.292068 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     response = self.full_dispatch_request()
[Mon May 18 09:14:42.292071 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
[Mon May 18 09:14:42.292075 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     rv = self.handle_user_exception(e)
[Mon May 18 09:14:42.292078 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
[Mon May 18 09:14:42.292081 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     reraise(exc_type, exc_value, tb)
[Mon May 18 09:14:42.292084 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
[Mon May 18 09:14:42.292094 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     raise value
[Mon May 18 09:14:42.292097 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
[Mon May 18 09:14:42.292100 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     rv = self.dispatch_request()
[Mon May 18 09:14:42.292102 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
[Mon May 18 09:14:42.292105 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     return self.view_functions[rule.endpoint](**req.view_args)
[Mon May 18 09:14:42.292108 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/var/www/web_app_video_synopsis/main.py", line 147, in student
[Mon May 18 09:14:42.292111 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     thumbnail_list, _ = load_video_with_thumbnail()
[Mon May 18 09:14:42.292114 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/var/www/web_app_video_synopsis/functions.py", line 87, in load_video_with_thumbnail
[Mon May 18 09:14:42.292117 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     for file in pathlib.Path.cwd().joinpath('Processed_Tasks').iterdir():
[Mon May 18 09:14:42.292120 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/usr/lib/python3.6/pathlib.py", line 1081, in iterdir
[Mon May 18 09:14:42.292123 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     for name in self._accessor.listdir(self):
[Mon May 18 09:14:42.292125 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]   File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
[Mon May 18 09:14:42.292128 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]     return strfunc(str(pathobj), *args)
[Mon May 18 09:14:42.292133 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] FileNotFoundError: [Errno 2] No such file or directory: '/Processed_Tasks'
[Mon May 18 09:14:42.292139 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] 

Processed_Tasks/已存在于我的项目目录中

这里是我的项目结构:

web_app_video_synopsis/
├── Downloaded_Thumbnail
├── Downloaded_Video
├── Processed_Tasks
├── Processed_Videos
├── Queue
├── README.md
├── centroid_tracker
├── database
├── functions.py
├── main.py
├── preprocessing.py  
├── requirements.txt
├── static
├── templates
├── web_app_video_synopsis.wsgi
└── yolact_segmentation

项目配置文件/etc/apache2/sites-available/web_app_video_synopsis.conf

WSGIPythonPath /var/www/www/web_app_video_synopsis

<VirtualHost *:5000>
            ServerName http://<Server IP>
            DocumentRoot /var/www/web_app_video_synopsis/
            WSGIScriptAlias / /var/www/web_app_video_synopsis/web_app_video_synopsis.wsgi
            WSGIDaemonProcess web_app_video_synopsis python-path=/var/www/web_app_video_synopsis:/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/
            WSGIProcessGroup web_app_video_synopsis
            WSGIApplicationGroup %GLOBAL
            <Directory /var/www/web_app_video_synopsis/>
                    Order allow,deny
                    Allow from all
            </Directory>
            Alias /static /var/www/web_app_video_synopsis/static
            <Directory /var/www/web_app_video_synopsis/static/>
                    Order allow,deny
                    Allow from all
            </Directory>
            ErrorLog $APACHE_LOG_DIR/error.log
            LogLevel warn
            CustomLog $APACHE_LOG_DIR/access.log combined
</VirtualHost>

web_app_video_synopsis.wsgi文件

#!/usr/bin/python
import sys
import logging
import os

#activate_this = '/home/ubuntu/.virtualenvs/pytorch/bin/activate_this.py'
activate_this = os.path.expanduser("/home/ubuntu/.virtualenvs/pytorch/bin/activate_this.py")
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/web_app_video_synopsis/")
os.chdir("/var/www/web_app_video_synopsis")

from main import app as application

错误在我的代码functions.py中产生这些行

    for file in pathlib.Path.cwd().joinpath('Processed_Tasks').iterdir():
        with open(file, mode='r') as f:
            task_data = json.load(f)

在本地计算机上,当前工作目录与项目目录中的main.py所在的目录相同

所以我如何从/var/www/web_app_video_synopsis访问(读/写/执行)文件?

像我的代码一样,许多文件在项目目录中读写。

Apache在我的项目目录/ var / www / web_app_video_synopsis上找不到文件,但是当我提示URL http:// ...

答案

我有解决方案,只需添加os.chdir("/var/www/web_app_video_synopsis")中的web_app_video_synopsis.wsgi和项目目录集。我也用解决方案编辑我的问题

以上是关于为什么apache在系统根目录而不是项目目录上找到文件,出现FileNotFound之类的错误的主要内容,如果未能解决你的问题,请参考以下文章

linux下如何找到apache的安装目录。找到apache的配置文件

linux下如何找到apache的安装目录。找到apache的配置文件?

CentOS Linux系统下更改Apache默认网站目录

Apache - 如何将前缀映射到不同的目录

网站显示目录而不是索引

Apache 列出目录文件而不是 index.php