在Apache2上运行Flask时未找到错误404

eimct9ow  于 2023-06-24  发布在  Apache
关注(0)|答案(1)|浏览(191)

我在Apache2上运行了一个静态网站。我在网站上有一个表单,我想将表单的内容传递给Flask应用程序。现在我只得到错误404时,试图将数据传递到应用程序,但静态部分的网站工作正常。我将其配置如下:
000-default.conf

  1. <VirtualHost *:443>
  2. # The ServerName directive sets the request scheme, hostname and port that
  3. # the server uses to identify itself. This is used when creating
  4. # redirection URLs. In the context of virtual hosts, the ServerName
  5. # specifies what hostname must appear in the request's Host: header to
  6. # match this virtual host. For the default virtual host (this file) this
  7. # value is not decisive as it is used as a last resort host regardless.
  8. # However, you must set it for any further virtual host explicitly.
  9. ServerAdmin webmaster@localhost
  10. DocumentRoot /var/www/html
  11. # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
  12. # error, crit, alert, emerg.
  13. # It is also possible to configure the loglevel for particular
  14. # modules, e.g.
  15. #LogLevel info ssl:warn
  16. ErrorLog ${APACHE_LOG_DIR}/error.log
  17. CustomLog ${APACHE_LOG_DIR}/access.log combined
  18. WSGIDaemonProcess mailer user=www-data group=www-data threads=5 home=/var/www/mailer
  19. WSGIScriptAlias /mailer /var/www/mailer/app.wsgi
  20. <Directory /var/www/mailer>
  21. WSGIProcessGroup mailer
  22. WSGIApplicationGroup %{GLOBAL}
  23. Require all granted
  24. </Directory>
  25. # For most configuration files from conf-available/, which are
  26. # enabled or disabled at a global level, it is possible to
  27. # include a line for only one particular virtual host. For example the
  28. # following line enables the CGI configuration for this host only
  29. # after it has been globally disabled with "a2disconf".
  30. #Include conf-available/serve-cgi-bin.conf
  31. RewriteEngine on
  32. RewriteCond %{SERVER_NAME} =alyfit.fi
  33. RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
  34. </VirtualHost>

我最初有一个单独的.conf用于Flask应用程序,但我在这里读到另一个问题,你不能为两个不同的虚拟主机使用相同的服务器名和端口。
app.wsgi

  1. import logging
  2. import sys
  3. logging.basicConfig(filename='/var/log/apache2/mailer_error.log', level=logging.DEBUG)
  4. sys.path.insert(0, '/var/www/mailer')
  5. sys.path.insert(0, '/var/www/mailer/venv/lib/python3.10/site-packages/')
  6. logging.debug('About to import app...')
  7. from mailer import app as application

mailer.py

  1. from flask import Flask, request, Blueprint
  2. from flask_mail import Mail, Message
  3. import os
  4. app = Flask(__name__)
  5. mailer_bp = Blueprint('mailer', __name__, url_prefix='/mailer')
  6. # Flask-Mail configuration
  7. app.config['MAIL_SERVER'] = 'smtp.redacted.com'
  8. app.config['MAIL_PORT'] = 1234
  9. app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME')
  10. app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD')
  11. app.config['MAIL_USE_TLS'] = True
  12. app.config['MAIL_USE_SSL'] = False
  13. mail = Mail(app)
  14. @mailer_bp.route('/', methods=['POST'])
  15. def receive_data():
  16. name = request.form.get('name')
  17. email = request.form.get('email')
  18. msg = Message('Hello',
  19. sender='redacted@redacted.com',
  20. recipients=[email])
  21. msg.body = f'Hello {name}'
  22. mail.send(msg)
  23. return 'OK', 200
  24. app.register_blueprint(mailer_bp)
  25. if __name__ == '__main__':
  26. app.run()

我已经尝试了这么多不同的配置,似乎没有什么使它工作。邮件日志上没有写任何东西,Apache日志也没有给予任何信息。我已验证是否安装了所有必要的库。我经历了20个类似的stackoverflow问题,没有一个解决方案有效。

e5nqia27

e5nqia271#

1.您应该为您的应用和disable your default.conf file创建一个新的.conf文件。默认值只是一个备用/占位符,它可以让你在第一次安装Apache时很容易地知道Apache正在工作(因为你可以在浏览器中打开你的服务器的IP地址,看到默认的Apache页面)。
1.您实际上并没有在.conf文件中设置ServerName指令。您应该在.conf文件中包含如下所示的行:

  1. ServerName your_domain.com
  2. ServerAlias www.your_domain.com

1.应该使用DocumentRoot指令指向应用所在的文件夹。通常,你希望你的应用在/var/www/的子目录中,所以指令应该看起来像这样:

  1. DocumentRoot /var/www/<your_app_directory>

相关问题