Nginx 500内部服务器错误-连接到MySQL时发生(在服务器上部署)

Nginx 500内部服务器错误-连接到MySQL时发生(在服务器上部署),nginx,flask,server,wsgi,Nginx,Flask,Server,Wsgi,我正在开发一个网站,使用Flask和MySQL作为数据库。在localhost:5000上,一切正常 然而,当我试图在real server上部署它时。500发生内部服务器错误。仅当我访问连接到数据库的任何路由时,才会发生此错误 我正在使用wsgi、nginx进行自托管部署 检查flaskSQL包正在使用的SQL设置。服务器上的设置可能需要与本地计算机上的设置不同。谢谢MarkusAfricanus。当你说烧瓶SQL时,你是指烧瓶SQL炼金术吗?是的,不管你用什么样的库。我目前正在使用Flas

我正在开发一个网站,使用Flask和MySQL作为数据库。在localhost:5000上,一切正常

然而,当我试图在real server上部署它时。500发生内部服务器错误。仅当我访问连接到数据库的任何路由时,才会发生此错误

我正在使用wsgi、nginx进行自托管部署


检查flaskSQL包正在使用的SQL设置。服务器上的设置可能需要与本地计算机上的设置不同。

谢谢MarkusAfricanus。当你说烧瓶SQL时,你是指烧瓶SQL炼金术吗?是的,不管你用什么样的库。我目前正在使用Flask MySQL,您必须配置sql登录详细信息。在我自己的计算机上,它可以工作,但当我得到一台服务器时,MySQL密码被设置为与服务器默认相同的密码。在发布这个之前,我也遇到了连接问题。
#!/usr/bin/python3
from flask import Flask, render_template, flash, request, redirect, url_for, session, logging
from flask_mail import Mail, Message
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
from functools import wraps

app =Flask(__name__)
# Option 1: app.debug = True (allow to auto refresh server)

# Config MySQL
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '123'
app.config['MYSQL_DB'] = 'myflaskapp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor' # by default the data set in MySQL will be in tuple; this one line of code sets the data set to dictionary structure

# init MYSQL
mysql = MySQL(app)

@app.route('/blog')
def blog():
    # Create cursor
    cur = mysql.connection.cursor()

    # Get articles
    result = cur.execute("SELECT * FROM articles")

    articles = cur.fetchall()

    if result > 0:
        return render_template('blog.html', articles=articles)
    else:
        msg = "No Articles Found"
        return render_template('blog.html', msg = msg)

    # Close connection
    cur.close()

# Register Form Class
class RegisterForm(Form):
    name = StringField('Name', [validators.Length(min=1, max=50)])
    username = StringField('Username', [validators.Length(min=4, max=25)])
    email = StringField ('Email', [validators.Length(min=6, max=50)])
    password = PasswordField ('Password', [
        validators.DataRequired(),
        validators.EqualTo('confirm', message='Passwords do not match.')
    ])
    confirm = PasswordField('Confirm Password')

# Admin Register
@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm(request.form)
    if request.method == 'POST' and form.validate():
        name = form.name.data
        email = form.email.data
        username = form.username.data
        password = sha256_crypt.encrypt(str(form.password.data))

        # Create cursor
        cur = mysql.connection.cursor()

        # Execute query
        cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))

        # Commit to DB
        mysql.connection.commit()

        # Close connection
        cur.close()

        flash('You are now registered and can log in.', 'green')

        return redirect(url_for('index'))

        return render_template('register.html')

    return render_template('register.html', form=form)

# Admin Login
@app.route('/login', methods = ['GET', 'POST'])
def login():
    if request.method == 'POST':
        # Get Form Fields
        username = request.form['username']
        password_candidate = request.form ['password']

        # Create cursorcur
        cur = mysql.connection.cursor()

        # Get user by username
        result = cur.execute("SELECT * FROM users WHERE username = %s", [username])

        if result > 0:
            # Get stored hash
            data = cur.fetchone()
            password = data['password']

            # Compare Passwords
            if sha256_crypt.verify(password_candidate, password):
                # Passed
                session['logged_in']= True
                session['username'] = username

                flash('You are now logged in', 'green')
                return redirect(url_for('blog'))

            else:
                error = 'Invalid login'
                return render_template('login.html', error = error)

            # Close connection
            cur.close()
        else:
            error = 'Username not found'
            return render_template('login.html', error = error)

    return render_template('login.html')

if __name__ == '__main__':
    app.secret_key='secert123456'
    app.run(host='0.0.0.0', port=6000, debug=True)