为什么 apache 在系统根目录而不是项目目录中查找文件,我得到像 FileNotFound 这样的错误

Why apache find files on system root directory instead of project directory, I got error like FileNotFound

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

中找到了 files/directories

当我提示 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/")

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 访问(read/write/execute)个文件?

像我的代码一样,在项目目录下读写了很多文件

解决方案

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

必须使用 os.chdir

设置项目目录

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