aws ec2 ubuntuにて、flaskでアプリケーションを構築しているのですが、ファイルアップロードをうまく行うことができません。
-
ローカル(Mac OS)で実行すると期待通りの動作をしますので、apache2の設定に問題があるのではないかと思っています。
-
現在の状態としては最初のページは表示される(Upload new Fileのページ)のですが、ファイルアップロードをしようとすると、Internal Server Errorとなってしまい、SAVEDが帰ってきません。
python
1import os 2from flask import Flask, request, redirect, url_for 3from werkzeug.utils import secure_filename 4 5UPLOAD_FOLDER = './upload_pdf' 6ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) 7 8app = Flask(__name__) 9app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 10 11def allowed_file(filename): 12 return '.' in filename and \ 13 filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS 14 15@app.route('/', methods=['GET', 'POST']) 16def upload_file(): 17 if request.method == 'POST': 18 # check if the post request has the file part 19 if 'file' not in request.files: 20 flash('No file part') 21 return redirect(request.url) 22 file = request.files['file'] 23 # if user does not select file, browser also 24 # submit a empty part without filename 25 if file.filename == '': 26 flash('No selected file') 27 return redirect(request.url) 28 29 if file and allowed_file(file.filename): 30 filename = secure_filename(file.filename) 31 print os.path.join(app.config['UPLOAD_FOLDER'], filename) 32 file.save(os.path.join(app.config['UPLOAD_FOLDER'], "upload.pdf")) 33 return "SAVED" 34 35 return ''' 36 <!doctype html> 37 <title>Upload new File</title> 38 <h1>Upload new File</h1> 39 <form action="" method=post enctype=multipart/form-data> 40 <p><input type=file name=file> 41 <input type=submit value=Upload> 42 </form> 43 ''' 44 45if __name__ == '__main__': 46 app.debug = True 47 app.run(host='0.0.0.0')
apache2 設定ファイル
<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 flaskapp threads=5 WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi WSGIPassAuthorization on <Directory flaskapp> WSGIProcessGroup flaskapp 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>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/18 03:21
2016/10/18 03:24
2016/10/18 05:29
2016/10/18 12:15
2016/10/18 13:58
2016/10/18 14:01