在AWS EBS上部署Flask App时出现500内部服务器错误

时间:2017-06-05 02:56:09

标签: python amazon-web-services flask

我试图将我的工作烧瓶应用程序部署到AWS,但是它给出了内部服务器错误500.我最后也附加了错误日志。我有beanstalk的python 2.7环境,我的虚拟环境也是如此。我在pip list和requirements.txt中有所有需要的包。

from flask import Flask, flash, render_template, request, session
from flaskext.mysql import MySQL
import os

application = Flask(__name__)

mysql = MySQL()
application.config['MYSQL_DATABASE_USER'] = 'user'
application.config['MYSQL_DATABASE_PASSWORD'] = 'password'
application.config['MYSQL_DATABASE_DB'] = 'nilesh'
application.config['MYSQL_DATABASE_HOST'] = 'nilesh-practice.us-west-2.rds.amazonaws.com'
mysql.init_app(application)

conn = mysql.connect()
cursor = conn.cursor()

cursor.execute("SELECT first_name from nilesh123.login_user")
first = cursor.fetchall()

cursor.execute("SELECT last_name from nilesh123.login_user")
last = cursor.fetchall()


@application.route('/')
def home():
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        return render_template('search.html', first=first, last=last)


@application.route('/login', methods=['POST'])
def do_admin_login():
    if request.form['password'] == 'password' and request.form['username'] == 'admin':
        session['logged_in'] = True
    else:
        flash('Wrong Password!')
    return home()

@application.route("/logout")
def logout():
    session['logged_in'] = False
    return home()

@application.route('/search', methods=['POST'])
def search():
    if request.form['firstname'] != '' and request.form['lastname'] == '':
        sql = "SELECT * from nilesh123.login_user WHERE first_name = %s"
        cursor.execute(sql, request.form['firstname'])
        result = cursor.fetchall()
        return render_template('display.html', result=result)

    elif request.form['firstname'] == '' and request.form['lastname'] != '':
        last1 = request.form['lastname']
        sql = "SELECT * from nilesh123.login_user WHERE last_name = %s "
        cursor.execute(sql, request.form['lastname'])
        result = cursor.fetchall()
        return render_template('display.html', result=result)

    elif request.form['firstname'] != '' and request.form['lastname'] != '':
        cursor.execute("SELECT * from nilesh123.login_user WHERE last_name like 'request.form['lastname']' AND \
                  first_name like 'request.form['firstname']'")
        result = cursor.fetchall()
        return render_template('display.html', result=result)

    return render_template('search.html')

if __name__ == "__main__":
    application.secret_key = os.urandom(12)
    application.run(debug=True, host='0.0.0.0', port=4500)

错误日志:

[Mon Jun 05 02:48:07.210177 2017] [:warn] [pid 3253] mod_wsgi: Compiled for Python/2.7.10.
[Mon Jun 05 02:48:07.210181 2017] [:warn] [pid 3253] mod_wsgi: Runtime using Python/2.7.12.
[Mon Jun 05 02:48:07.211976 2017] [mpm_prefork:notice] [pid 3253] AH00163: Apache/2.4.25 (Amazon) mod_wsgi/3.5 Python/2.7.12 configured -- resuming normal operations
[Mon Jun 05 02:48:07.211988 2017] [core:notice] [pid 3253] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Jun 05 02:48:19.956456 2017] [:error] [pid 3258] [remote 127.0.0.1:27151] mod_wsgi (pid=3258): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Mon Jun 05 02:48:19.956485 2017] [:error] [pid 3258] [remote 127.0.0.1:27151] mod_wsgi (pid=3258): Exception occurred processing WSGI script '/opt/python/current/app/application.py'.
[Mon Jun 05 02:48:19.956506 2017] [:error] [pid 3258] [remote 127.0.0.1:27151] Traceback (most recent call last):
[Mon Jun 05 02:48:19.956518 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/current/app/application.py", line 14, in <module>
[Mon Jun 05 02:48:19.956572 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     conn = mysql.connect()
[Mon Jun 05 02:48:19.956582 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/run/venv/lib/python2.7/site-packages/flaskext/mysql.py", line 53, in connect
[Mon Jun 05 02:48:19.956622 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     return pymysql.connect(**self.connect_args)
[Mon Jun 05 02:48:19.956630 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/run/venv/lib/python2.7/site-packages/pymysql/__init__.py", line 90, in Connect
[Mon Jun 05 02:48:19.956675 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     return Connection(*args, **kwargs)
[Mon Jun 05 02:48:19.956683 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/run/venv/lib/python2.7/site-packages/pymysql/connections.py", line 706, in __init__
[Mon Jun 05 02:48:19.956706 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     self.connect()
[Mon Jun 05 02:48:19.956711 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/run/venv/lib/python2.7/site-packages/pymysql/connections.py", line 963, in connect
[Mon Jun 05 02:48:19.956732 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     raise exc
[Mon Jun 05 02:48:19.956748 2017] [:error] [pid 3258] [remote 127.0.0.1:27151] OperationalError: (2003, "Can't connect to MySQL server on 'nilesh-practice.ccajuffzgfk9.us-west-2.rds.amazonaws.com' (timed out)")
[Mon Jun 05 02:48:30.066771 2017] [:error] [pid 3258] [remote 127.0.0.1:27151] mod_wsgi (pid=3258): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Mon Jun 05 02:48:30.066828 2017] [:error] [pid 3258] [remote 127.0.0.1:27151] mod_wsgi (pid=3258): Exception occurred processing WSGI script '/opt/python/current/app/application.py'.
[Mon Jun 05 02:48:30.066850 2017] [:error] [pid 3258] [remote 127.0.0.1:27151] Traceback (most recent call last):
[Mon Jun 05 02:48:30.066863 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/current/app/application.py", line 14, in <module>
[Mon Jun 05 02:48:30.066881 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     conn = mysql.connect()
[Mon Jun 05 02:48:30.066888 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/run/venv/lib/python2.7/site-packages/flaskext/mysql.py", line 53, in connect
[Mon Jun 05 02:48:30.066898 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     return pymysql.connect(**self.connect_args)
[Mon Jun 05 02:48:30.066906 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/run/venv/lib/python2.7/site-packages/pymysql/__init__.py", line 90, in Connect
[Mon Jun 05 02:48:30.066915 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     return Connection(*args, **kwargs)
[Mon Jun 05 02:48:30.066920 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/run/venv/lib/python2.7/site-packages/pymysql/connections.py", line 706, in __init__
[Mon Jun 05 02:48:30.066928 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     self.connect()
[Mon Jun 05 02:48:30.066932 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]   File "/opt/python/run/venv/lib/python2.7/site-packages/pymysql/connections.py", line 963, in connect
[Mon Jun 05 02:48:30.066939 2017] [:error] [pid 3258] [remote 127.0.0.1:27151]     raise exc
[Mon Jun 05 02:48:30.066953 2017] [:error] [pid 3258] [remote 127.0.0.1:27151] OperationalError: (2003, "Can't connect to MySQL server on 'nilesh-practice.ccajuffzgfk9.us-west-2.rds.amazonaws.com' (timed out)")

1 个答案:

答案 0 :(得分:1)

关于MySQL,

你可以尝试:

  • telnet nilesh-practice.ccajuffzgfk9.us-west-2.rds.amazonaws.com 3306
  • 从EC2实例连接到RDS MySql。

如果两者都有问题,您必须检查:

  • 安全组,如果您有权从EC2连接到RDS。
  • 如果RDS位于不同的区域,则必须启用它才能公开使用。

希望它对你有所帮助。