2017-08-14 34 views
0

在Ubuntu AWS實例上,我嘗試在設置Apache之後設置Flask服務。os.makedirs在Amazon AWS上導致OSError Ubuntu實例

/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 [email protected] 
    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

您不能使用相對路徑名,也不能使用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配置中犯了錯誤,人們可能會下載您的源代碼,可能包括源代碼中的任何配置祕密。

+0

謝謝。用'/ var/www/html/myApp'替換我的拼寫錯誤後,我仍然得到相同的錯誤。我還嘗試將'myapp /'目錄及其文件的所有權更改爲用戶'www-data'。同樣的錯誤。 – Username

+1

你有沒有改變''os.makedirs(「dir」)''。?正如我所說,文檔告訴你,你不能使用相對路徑名。該路徑可能會解析爲''/ dir'',您將無法寫入權限。計算它,或將其設置爲完全絕對路徑,而不是相對路徑。 –