使用 mod_wsgi 在 Apache 上部署 Flask API
我已经遵循了几个教程,并且知道这一定是一些小问题。请帮我找到它!
我有一个 Flask API,使用 Flask-Restx,随时可用,还有一个带有 Apache 的远程 Debian VPS。我没有使用虚拟环境。
我的项目位于 /var/www/ProvisioningApi/ 并包含..
- __init__.py (blank)
- setup.py
- setup.wsgi
- ProvisioningApi/
--- __pycache__
--- api/
------ __init__.py # not blank
------ auth/ # this holds the file with the auth endpoints
------ provision/ # more endpoints
setup.wsgi
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/var/www/ProvisioningApp/")
from setup import app as application
application.secret_key = "nosecretshere"
setup.py
from ProvisioningApi.api import create_app
app = create_app()
if __name__ == "__main__":
app.run()
/api/__init__.py
# root init file
from flask import Flask
from flask_restx import Api
from flask_jwt_extended import JWTManager
from werkzeug.middleware.proxy_fix import ProxyFix
# Import the API views
from .auth.auth import auth
from .provision.provision import prov
# Import configuration settings
from .config.config import config_dict
def create_app(config=config_dict['prod']):
app=Flask(__name__)
app.config.from_object(config)
# Instantiate RestX and add the Namespaces
api = Api(app,
version='1.0',
title='Provisioning API',
description='My provisioning endpoints')
api.add_namespace(auth, path='/auth')
api.add_namespace(prov, path='/provision')
jwt = JWTManager(app)
app.wsgi_app = ProxyFix(app.wsgi_app)
return app
Virtual主机位于 /etc/apache2/sites-available/ProvisioningApi.conf
/etc/apache2/sites-available/ProvisioningApi.conf
<VirtualHost *:80>
ServerName <the IP address for the server>
ServerAdmin <my email address>
WSGIScriptAlias / /var/www/ProvisioningApi/setup.wsgi
<Directory /var/www/ProvisioningApi/ProvisioningApi/>
Order allow,deny
Allow from all
</Directory>
Alias /api /var/www/ProvisioningApi/ProvisioningApi/api
<Directory /var/www/ProvisioningApi/ProvisioningApi/api/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
错误日志显示:
File "/var/www/ProvisioningApi/setup.wsgi", line 7, in <module>
from setup import app as application
ModuleNotFoundError: No module named 'setup'
转到 IP 地址,我看到内部服务器错误。如果我转到 IP_address/api,我会看到我的文件索引。
我调整了文件的嵌套,添加/删除了 init 文件,一次又一次地重新启动 Apache,并且错误始终与 setup.py
文件中的导入有关。
提前致谢!
I've followed several tutorials at this point and know this has to be some minor issue. PLEASE help me find it!
I have a Flask API, using Flask-Restx, ready to go and a remote Debian VPS with Apache. I'm not using a virtual environment.
My project is located at /var/www/ProvisioningApi/ and contains..
- __init__.py (blank)
- setup.py
- setup.wsgi
- ProvisioningApi/
--- __pycache__
--- api/
------ __init__.py # not blank
------ auth/ # this holds the file with the auth endpoints
------ provision/ # more endpoints
setup.wsgi
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/var/www/ProvisioningApp/")
from setup import app as application
application.secret_key = "nosecretshere"
setup.py
from ProvisioningApi.api import create_app
app = create_app()
if __name__ == "__main__":
app.run()
/api/__init__.py
# root init file
from flask import Flask
from flask_restx import Api
from flask_jwt_extended import JWTManager
from werkzeug.middleware.proxy_fix import ProxyFix
# Import the API views
from .auth.auth import auth
from .provision.provision import prov
# Import configuration settings
from .config.config import config_dict
def create_app(config=config_dict['prod']):
app=Flask(__name__)
app.config.from_object(config)
# Instantiate RestX and add the Namespaces
api = Api(app,
version='1.0',
title='Provisioning API',
description='My provisioning endpoints')
api.add_namespace(auth, path='/auth')
api.add_namespace(prov, path='/provision')
jwt = JWTManager(app)
app.wsgi_app = ProxyFix(app.wsgi_app)
return app
Virtual Host at /etc/apache2/sites-available/ProvisioningApi.conf
/etc/apache2/sites-available/ProvisioningApi.conf
<VirtualHost *:80>
ServerName <the IP address for the server>
ServerAdmin <my email address>
WSGIScriptAlias / /var/www/ProvisioningApi/setup.wsgi
<Directory /var/www/ProvisioningApi/ProvisioningApi/>
Order allow,deny
Allow from all
</Directory>
Alias /api /var/www/ProvisioningApi/ProvisioningApi/api
<Directory /var/www/ProvisioningApi/ProvisioningApi/api/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The error logs read:
File "/var/www/ProvisioningApi/setup.wsgi", line 7, in <module>
from setup import app as application
ModuleNotFoundError: No module named 'setup'
Going to the IP address, I see Internal Server Error. If I go to IP_address/api I see my index of files.
I've tweaked the nesting of files, added/removed init files, restarted Apache again and again, and the error is always about the imports in the setup.py
file.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能只是路径中的拼写错误。您添加
但您的目录末尾是“...Api/”,而不是“...App/”
it might be just a typo in the path. You add
but your directory is "...Api/" at the end, not "...App/"
过去的自己,你会得到这个!
我尝试了一百种不同的方法(包括修复 Razenstein 指出的拼写错误),最终问题是调整我的文件。我删除了 /api/ 文件夹并将内容上移了一个级别。不对
__init__.py
内的导入进行编辑。setup.wsgi
不相关,但很重要:如果您在受保护端点的标头中传递访问令牌,则需要将
WSGIPassAuthorization On
添加到 VirtualHost 文件中。Past self, you're gonna get this!
I tried a hundred different things (including fixing the typo pointed out by Razenstein), and ultimately it was a matter of adjusting my files. I removed the /api/ folder and shifted the contents up a level. No edits to the imports inside the
__init__.py
.setup.wsgi
Unrelated, but important: if you are passing Access Tokens in the header for protected endpoints, you need to add
WSGIPassAuthorization On
into the VirtualHost file.