I have a Python Flask application that I want to run on a apache2(AWS EC2). This is my first time doing this so I have been using this source as a guide. However, my server keeps giving me the following errors and I do not know what went wrong. Please advise how I can solve this.
mod_wsgi (pid=5873): Failed to exec Python script file '/var/www/html/flaskapp/flaskapp.wsgi'.
[Tue Aug 17 21:04:26.331609 2021] [wsgi:error] [pid 5873:tid 140393656071936] [client 70.51.138.104:61053] mod_wsgi (pid=5873): Exception occurred processing WSGI script '/var/www/html/flaskapp/flaskapp.wsgi'.
[Tue Aug 17 21:04:26.331636 2021] [wsgi:error] [pid 5873:tid 140393656071936] [client 70.51.138.104:61053] Traceback (most recent call last):
[Tue Aug 17 21:04:26.331655 2021] [wsgi:error] [pid 5873:tid 140393656071936] [client 70.51.138.104:61053] File "/var/www/html/flaskapp/flaskapp.wsgi", line 5, in <module>
[Tue Aug 17 21:04:26.331682 2021] [wsgi:error] [pid 5873:tid 140393656071936] [client 70.51.138.104:61053] from flaskapp import app as application
[Tue Aug 17 21:04:26.331698 2021] [wsgi:error] [pid 5873:tid 140393656071936] [client 70.51.138.104:61053] ImportError: No module named flaskapp
My flask application path: /var/www/html/flaskapp/
Content of flaskapp
folder:
application.py
utils.py
requirements.txt
flaskapp.wsgi
Content of flaskapp.wsgi
:
#flaskapp.wsgi
import sys
sys.path.insert(0,’/var/www/html/flaskapp’)
from flaskapp import app as application
Furthermore, I have also modified the /etc/apache2/sites-enabled/000-default.conf
file:
<VirtualHost *:80>
# 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.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
# 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
# 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
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
3
Answers
I have solve this problem by changing
DocumentRoot /var/www/html
toDocumentRoot /var/www/html/flaskapp
in the/etc/apache2/sites-enabled/000-default.conf
file.I believe you may need to change your
<Directory flaskapp
to<Directory /var/www/html/flaskapp
. It should be an absolute directory path or a wildcard, not a relative path.From: https://httpd.apache.org/docs/current/mod/core.html#directory
I think you need to change the line:
from flaskapp import app as application
to
from application import app as application
As the error says Apache can’t import the module, which I think has happened because in the tutorial you mentioned, the file containing the flask app is called
flaskapp.py
whereas your file is calledapplication.py
.I would also consider changing the name of
application.py
to something more specific, like ‘my_descriptive_app_name’ if you are going to store it in the variable application.