我正在尝试部署一个非常简单的Flask应用程序,该应用程序出现在本教程中,用于测试flask应用程序中的登录功能:https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login.我只改变了几件事,但大部分是一样的。应用程序的结构是这样的:
当我在本地和外部通过flask服务器运行它时,它工作得很好,但当我试图通过Apache提供它时,它失败了,并显示“内部服务器错误”消息和Apache的error.log中的以下错误:
> [Tue May 09 19:48:18.108969 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] mod_wsgi (pid=31601): Failed to exec Python script file '/var/www/bdl/portal/app.wsgi'.
[Tue May 09 19:48:18.109123 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] mod_wsgi (pid=31601): Exception occurred processing WSGI script '/var/www/bdl/portal/app.wsgi'.
[Tue May 09 19:48:18.116248 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] Traceback (most recent call last):
[Tue May 09 19:48:18.116298 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] File "/var/www/bdl/portal/app.wsgi", line 4, in <module>
[Tue May 09 19:48:18.116305 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] from app import main as application
[Tue May 09 19:48:18.116322 2023] [wsgi:error] [pid 31601] [client 185.216.73.35:9480] ModuleNotFoundError: No module named 'app'
下面是app.wsgi文件中的内容:
import sys
sys.path.insert(0, "/var/www/bdl/portal")
from app import main as application
这是__init__.py
:
activate_this_file = "/var/www/bdl/auth/bin/activate_this.py"
with open(activate_this_file) as _file:
exec(_file.read(), dict(__file__=activate_this_file))
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
# init SQLAlchemy so we can use it later in our models
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'xxxxxxxxxxxxxxxxx'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
db.init_app(app)
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
from .models import User
@login_manager.user_loader
def load_user(user_id):
# since the user_id is just the primary key of our user table, use it in the query for the user
return User.query.get(int(user_id))
# blueprint for auth routes in our app
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
from . import models
with app.app_context():
db.create_all()
return app
下面是main.py
文件:
from flask import Blueprint, render_template
from flask_login import login_required, current_user
from . import db
main = Blueprint('main', __name__)
@main.route('/')
def index():
return render_template('index.html')
@main.route('/profile')
@login_required
def profile():
return render_template('profile.html', name=current_user.name)
下面是Apache .conf
文件:
<VirtualHost *:80>
ServerName balldatalab.com
ServerAlias www.balldatalab.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/bdl/portal
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
WSGIScriptAlias / /var/www/bdl/portal/app.wsgi
<Directory /var/www/bdl/portal>
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/www/bdl/portal/logs/error.log CustomLog /var/www/bdl/portal/logs/access.log combined </VirtualHost>
<VirtualHost *:443>
ServerName balldatalab.com
ServerAlias www.balldatalab.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/bdl/portal
SSLEngine on
SSLCertificateFile /etc/ssl/certs/bdl9.cer
SSLCertificateKeyFile /etc/ssl/private/bdl9.key
SSLCertificateChainFile /etc/ssl/certs/bdl9-inter.cer
WSGIScriptAlias / /var/www/bdl/portal/app.wsgi
<Directory /var/www/bdl/portal>
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/www/bdl/portal/logs/error.log CustomLog /var/www/bdl/portal/logs/access.log combined
</VirtualHost>
我真的不知道这是怎么回事,你能帮我吗?先谢谢你了!
1条答案
按热度按时间slmsl1lt1#
它抱怨是因为--正如错误所言--在
portal
目录中没有名为app
的Python模块。app.wsgi
不是Python模块。您需要有一个app.py
文件或一个包含__init__.py
文件的/app/
目录。因此,解决此问题的最快方法是将/portal/__init__.py
重命名为app.py
。然而,除此之外:
1.你的
__init__.py
文件实际上并没有创建一个名为main
的Flask应用对象(这是你试图在app.wsgi
文件中导入的对象)。它有一个create_app()
函数,返回一个Flask应用对象,但没有代码实际调用该函数并将其分配给名为main
的变量。1.您的
create_app()
函数还引用了一个auth
模块,我在您的文件/目录列表中没有看到该模块;我建议你注解掉这些代码,直到你运行了一个“hello world”应用程序。我建议您尝试将
__init__.py
(您应该将其重命名为app.py
)更改为以下内容:app.py