我在Apache2上运行了一个静态网站。我在网站上有一个表单,我想将表单的内容传递给Flask应用程序。现在我只得到错误404时,试图将数据传递到应用程序,但静态部分的网站工作正常。我将其配置如下:
000-default.conf
<VirtualHost *:443>
# 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.
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# 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
WSGIDaemonProcess mailer user=www-data group=www-data threads=5 home=/var/www/mailer
WSGIScriptAlias /mailer /var/www/mailer/app.wsgi
<Directory /var/www/mailer>
WSGIProcessGroup mailer
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
# 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
RewriteEngine on
RewriteCond %{SERVER_NAME} =alyfit.fi
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
我最初有一个单独的.conf用于Flask应用程序,但我在这里读到另一个问题,你不能为两个不同的虚拟主机使用相同的服务器名和端口。
app.wsgi
import logging
import sys
logging.basicConfig(filename='/var/log/apache2/mailer_error.log', level=logging.DEBUG)
sys.path.insert(0, '/var/www/mailer')
sys.path.insert(0, '/var/www/mailer/venv/lib/python3.10/site-packages/')
logging.debug('About to import app...')
from mailer import app as application
mailer.py
from flask import Flask, request, Blueprint
from flask_mail import Mail, Message
import os
app = Flask(__name__)
mailer_bp = Blueprint('mailer', __name__, url_prefix='/mailer')
# Flask-Mail configuration
app.config['MAIL_SERVER'] = 'smtp.redacted.com'
app.config['MAIL_PORT'] = 1234
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD')
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
mail = Mail(app)
@mailer_bp.route('/', methods=['POST'])
def receive_data():
name = request.form.get('name')
email = request.form.get('email')
msg = Message('Hello',
sender='redacted@redacted.com',
recipients=[email])
msg.body = f'Hello {name}'
mail.send(msg)
return 'OK', 200
app.register_blueprint(mailer_bp)
if __name__ == '__main__':
app.run()
我已经尝试了这么多不同的配置,似乎没有什么使它工作。邮件日志上没有写任何东西,Apache日志也没有给予任何信息。我已验证是否安装了所有必要的库。我经历了20个类似的stackoverflow问题,没有一个解决方案有效。
1条答案
按热度按时间e5nqia271#
1.您应该为您的应用和disable your default.conf file创建一个新的
.conf
文件。默认值只是一个备用/占位符,它可以让你在第一次安装Apache时很容易地知道Apache正在工作(因为你可以在浏览器中打开你的服务器的IP地址,看到默认的Apache页面)。1.您实际上并没有在
.conf
文件中设置ServerName
指令。您应该在.conf
文件中包含如下所示的行:1.应该使用
DocumentRoot
指令指向应用所在的文件夹。通常,你希望你的应用在/var/www/
的子目录中,所以指令应该看起来像这样: