Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Amazon AWS Ubuntu实例上,os.makedirs导致出现操作错误_Python_Amazon Web Services_Ubuntu_Amazon Ec2_Mod Wsgi - Fatal编程技术网

Python 在Amazon AWS Ubuntu实例上,os.makedirs导致出现操作错误

Python 在Amazon AWS Ubuntu实例上,os.makedirs导致出现操作错误,python,amazon-web-services,ubuntu,amazon-ec2,mod-wsgi,Python,Amazon Web Services,Ubuntu,Amazon Ec2,Mod Wsgi,在一个UbuntuAWS实例上,我试图在设置Apache之后设置一个Flask服务 在/var/www/html/myApp/中,我有以下文件: myApp.py myApp.wsgi 以下是myApp.wsgi的内容: 下面是/etc/apache2/sites enabled/000-default.conf的内容: 但当我把浏览器导航到http://MY-UBUNTU-EC2-ADDRESS.compute-1.amazonaws.com/myApp/,它返回一个500错误 当我在/var

在一个UbuntuAWS实例上,我试图在设置Apache之后设置一个Flask服务

在/var/www/html/myApp/中,我有以下文件:

myApp.py

myApp.wsgi

以下是myApp.wsgi的内容:

下面是/etc/apache2/sites enabled/000-default.conf的内容:

但当我把浏览器导航到http://MY-UBUNTU-EC2-ADDRESS.compute-1.amazonaws.com/myApp/,它返回一个500错误

当我在/var/log/apache2/error.log检查错误日志时,我看到以下几行:

[Mon Aug 14 22:57:06.346698 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792] mod_wsgi (pid=6641): Target WSGI script '/var/www/html/myApp/myApp.wsgi' cannot be loaded as Python module.
[Mon Aug 14 22:57:06.346734 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792] mod_wsgi (pid=6641): Exception occurred processing WSGI script '/var/www/html/myApp/myApp.wsgi'.
[Mon Aug 14 22:57:06.346750 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792] Traceback (most recent call last):
[Mon Aug 14 22:57:06.346768 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]   File "/var/www/html/myApp/myApp.wsgi", line 4, in <module>
[Mon Aug 14 22:57:06.346791 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]     from myApp import app as application
[Mon Aug 14 22:57:06.346797 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]   File "/var/www/html/myApp/myApp.py", line 12, in <module>
[Mon Aug 14 22:57:06.346806 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]     os.makedirs(graphicsFiles)
[Mon Aug 14 22:57:06.346811 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]   File "/usr/lib/python2.7/os.py", line 157, in makedirs
[Mon Aug 14 22:57:06.346820 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]     mkdir(name, mode)
[Mon Aug 14 22:57:06.346837 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792] OSError: [Errno 13] Permission denied: 'dir'

我需要更改什么以确保我的应用程序具有创建目录或文件的权限?

您不能使用相对路径名,也不能使用Apache用户无法写入的目录。见以下网址的文档:

您的Apache配置也是错误的

<Directory flaskapp>
    WSGIProcessGroup myApp
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

另一个问题是,将源代码放在DocumentRoot指定的目录下是一种不好的做法。如果您在Apache配置中出错,人们可以下载您的源代码,可能包括源代码中的任何配置机密。

谢谢。在用/var/www/html/myApp替换输入错误后,我仍然会遇到同样的错误。我还尝试将myapp/目录及其文件的所有权更改为用户www数据。同样的错误。您是否更改了os.makedirsdir。?正如我所说的,文档告诉您,您不能使用相对路径名。该路径可能解析为/dir,您将无法对其进行写入访问。将其计算或设置为完全绝对路径,而不是相对路径。
if not os.path.exists("dir"):
    os.makedirs("dir")
[Mon Aug 14 22:57:06.346698 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792] mod_wsgi (pid=6641): Target WSGI script '/var/www/html/myApp/myApp.wsgi' cannot be loaded as Python module.
[Mon Aug 14 22:57:06.346734 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792] mod_wsgi (pid=6641): Exception occurred processing WSGI script '/var/www/html/myApp/myApp.wsgi'.
[Mon Aug 14 22:57:06.346750 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792] Traceback (most recent call last):
[Mon Aug 14 22:57:06.346768 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]   File "/var/www/html/myApp/myApp.wsgi", line 4, in <module>
[Mon Aug 14 22:57:06.346791 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]     from myApp import app as application
[Mon Aug 14 22:57:06.346797 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]   File "/var/www/html/myApp/myApp.py", line 12, in <module>
[Mon Aug 14 22:57:06.346806 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]     os.makedirs(graphicsFiles)
[Mon Aug 14 22:57:06.346811 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]   File "/usr/lib/python2.7/os.py", line 157, in makedirs
[Mon Aug 14 22:57:06.346820 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792]     mkdir(name, mode)
[Mon Aug 14 22:57:06.346837 2017] [:error] [pid 6641:tid 139812646708992] [client IP-ADDRESS-REMOVED:48792] OSError: [Errno 13] Permission denied: 'dir'
<Directory flaskapp>
    WSGIProcessGroup myApp
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>
<Directory /var/www/html/myApps>
    WSGIProcessGroup myApp
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>