os.makedirs导致Amazon AWS Ubuntu实例上的OSError

时间:2017-08-14 23:03:29

标签: python amazon-web-services ubuntu amazon-ec2 mod-wsgi

在Ubuntu AWS实例上,我尝试在设置Apache后设置Flask服务。

/var/www/html/myApp/中,我有这些文件,其中包括:

myApp.py

myApp.wsgi

以下是myApp.wsgi的内容:

import sys
sys.path.insert(0, '/var/www/html/myApp')

from myApp import app as application

以下是/etc/apache2/sites-enabled/000-default.conf

的内容
<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    WSGIDaemonProcess charter threads=5
    WSGIScriptAlias / /var/www/html/myApp/myApp.wsgi

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

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

myApp.py中,我有一些代码来创建目录:

if not os.path.exists("dir"):
    os.makedirs("dir")

但是当我将浏览器导航到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'

我需要更改哪些内容才能确保我的应用有权创建目录或文件?

1 个答案:

答案 0 :(得分:1)

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

您的Apache配置也是错误的。

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

flaskapp使用Directory作为参数并不正确。该参数应该是WSGI脚本文件所在的目录。

<Directory /var/www/html/myApps>
    WSGIProcessGroup myApp
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

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