I've pushed my Flask app to ElasticBeanstalk but am getting 500 errors on every page and Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module. showing up in /var/log/httpd/error_log.
Here's the full stacktrace in /var/log/httpd/error_log:
[Wed Mar 18 22:02:36.815358 2015] [:error] [pid 21701] [remote 50.23.**2.10:0] mod_wsgi (pid=21701): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Wed Mar 18 22:02:36.815389 2015] [:error] [pid 21701] [remote 50.23.**2.10:0] mod_wsgi (pid=21701): Exception occurred processing WSGI script '/opt/python/current/app/application.py'.
[Wed Mar 18 22:02:36.815410 2015] [:error] [pid 21701] [remote 50.23.**2.10:0] Traceback (most recent call last):
[Wed Mar 18 22:02:36.815435 2015] [:error] [pid 21701] [remote 50.23.**2.10:0] File "/opt/python/current/app/application.py", line 5, in <module>
[Wed Mar 18 22:02:36.815466 2015] [:error] [pid 21701] [remote 50.23.**2.10:0] from charts import *
[Wed Mar 18 22:02:36.815478 2015] [:error] [pid 21701] [remote 50.23.**2.10:0] File "/opt/python/current/app/charts.py", line 6, in <module>
[Wed Mar 18 22:02:36.815494 2015] [:error] [pid 21701] [remote 50.23.**2.10:0] from scipy.stats import linregress
[Wed Mar 18 22:02:36.815514 2015] [:error] [pid 21701] [remote 50.23.**2.10:0] ImportError: No module named scipy.stats
Here's what the first portion of my application.py looks like:
from flask import Flask, jsonify, request
import os
import logging
from logging import StreamHandler
from charts import *
application = app = Flask(__name__)
I'm assigning my flask app to the application object, which is all the docs say I need to do. Any clue what I'm doing wrong?
Thanks!
Related
I follow this tutorial https://www.codementor.io/#jqn/deploy-a-flask-app-on-aws-ec2-13hp1ilqy2 to setup Flask in EC2. Everything works well up to Step 3 (where index.html from flask folder is served).
But as soon as I move to Step 4 (to enable Flask Python app, and link it to Apache), I got the below error.
Any suggestions? Thank you in advance
[Thu Jan 07 01:36:14.607338 2021] [wsgi:error] [pid 1387:tid 140388356159232] [client 119.74.148.227:30755] mod_wsgi (pid=1387): Exception occurred processing WSGI script '/var/www/html/flaskapp/flaskapp.wsgi'.
[Thu Jan 07 01:36:14.607434 2021] [wsgi:error] [pid 1387:tid 140388356159232] [client 119.74.148.227:30755] Traceback (most recent call last):
[Thu Jan 07 01:36:14.607522 2021] [wsgi:error] [pid 1387:tid 140388356159232] [client 119.74.148.227:30755] File "/var/www/html/flaskapp/flaskapp.wsgi", line 5, in <module>
[Thu Jan 07 01:36:14.607622 2021] [wsgi:error] [pid 1387:tid 140388356159232] [client 119.74.148.227:30755] from flaskapp import app as application
[Thu Jan 07 01:36:14.607708 2021] [wsgi:error] [pid 1387:tid 140388356159232] [client 119.74.148.227:30755] ImportError: No module named flaskapp
I forgot to create flaskapp.py as mentioned in the tutorial link. My bad!
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello from Flask!'
if __name__ == '__main__':
app.run()
Today I upgraded my Ubuntu server (Digital Ocean Droplet) to 20.04 from 18.04. After upgrading, my Flask application getting WSGI error(s) and can't run the flask app. Here is the apache2 error log.
[Wed Sep 30 14:02:19.652956 2020] [wsgi:error] [pid 2335] [client 121.128.137.103:53959] SyntaxError: invalid syntax
[Wed Sep 30 14:02:31.864498 2020] [wsgi:error] [pid 2329] [client 121.128.137.103:53961] mod_wsgi (pid=2329): Failed to exec Python script file '/var/www/webApp/webapp.wsgi'.
[Wed Sep 30 14:02:31.864542 2020] [wsgi:error] [pid 2329] [client 121.128.137.103:53961] mod_wsgi (pid=2329): Exception occurred processing WSGI script '/var/www/webApp/webapp.wsgi'.
[Wed Sep 30 14:02:31.864566 2020] [wsgi:error] [pid 2329] [client 121.128.137.103:53961] Traceback (most recent call last):
[Wed Sep 30 14:02:31.864590 2020] [wsgi:error] [pid 2329] [client 121.128.137.103:53961] File "/var/www/webApp/webapp.wsgi", line 7, in <module>
[Wed Sep 30 14:02:31.864668 2020] [wsgi:error] [pid 2329] [client 121.128.137.103:53961] from webApp import app as application
[Wed Sep 30 14:02:31.864728 2020] [wsgi:error] [pid 2329] [client 121.128.137.103:53961] File "/var/www/webApp/webApp/__init__.py", line 326
[Wed Sep 30 14:02:31.864735 2020] [wsgi:error] [pid 2329] [client 121.128.137.103:53961] res = make_response(jsonify({"message": f" File Is Exceeded The Maximum Size Limit"}), 403)
[Wed Sep 30 14:02:31.864740 2020] [wsgi:error] [pid 2329] [client 121.128.137.103:53961] ^
[Wed Sep 30 14:02:31.864745 2020] [wsgi:error] [pid 2329] [client 121.128.137.103:53961] SyntaxError: invalid syntax
Here is the code inside the webapp.wsgi file
#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/webApp/")
from webApp import app as application
application.secret_key = 'someSecret_k*eY'
Searched in Google and SO few hours but nothing found to fix the error. Can Someone tell me where is the error and a way to fixe this?.
I'm trying to run a simple app on AWS AMI that should use OpenCV library. I created a virtual machine and i installed OpenCV. For the installation I followed this tutorial http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/ and seems to be ok also because I able to run the example. The problem arises when I try to run my Flask-app because I'm getting the following error:
[Tue Sep 20 09:58:14.117753 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] Traceback (most recent call last):
[Tue Sep 20 09:58:14.117778 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] File "/var/www/flaskapp/app.wsgi", line 25, in <module>
[Tue Sep 20 09:58:14.117822 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] from app import app as application
[Tue Sep 20 09:58:14.117831 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] File "/var/www/flaskapp/app.py", line 13, in <module>
[Tue Sep 20 09:58:14.117878 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] from flask import (Flask, abort, flash, g, jsonify, make_response, render_template, request, session, url_for)
[Tue Sep 20 09:58:14.117887 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] File "/var/www/flaskapp/env/lib/python2.7/site-packages/flask/__init__.py", line 19, in <module>
[Tue Sep 20 09:58:14.117916 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] from jinja2 import Markup, escape
[Tue Sep 20 09:58:14.117923 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] File "/var/www/flaskapp/env/lib/python2.7/site-packages/jinja2/__init__.py", line 33, in <module>
[Tue Sep 20 09:58:14.117951 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] from jinja2.environment import Environment, Template
[Tue Sep 20 09:58:14.117965 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] File "/var/www/flaskapp/env/lib/python2.7/site-packages/jinja2/environment.py", line 13, in <module>
[Tue Sep 20 09:58:14.118148 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] from jinja2 import nodes
[Tue Sep 20 09:58:14.118157 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] File "/var/www/flaskapp/env/lib/python2.7/site-packages/jinja2/nodes.py", line 19, in <module>
[Tue Sep 20 09:58:14.118287 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] from jinja2.utils import Markup
[Tue Sep 20 09:58:14.118296 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] File "/var/www/flaskapp/env/lib/python2.7/site-packages/jinja2/utils.py", line 531, in <module>
[Tue Sep 20 09:58:14.118418 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] from markupsafe import Markup, escape, soft_unicode
[Tue Sep 20 09:58:14.118436 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] ImportError: No module named markupsafe
but I checked all the installed modules on the virtual machine with the command pip freeze i get the following:
click==6.6
Flask==0.11.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
numpy==1.11.1
Werkzeug==0.11.11
So MarkupSafe is installed and updated a the last version.
So started thinking the problem could be in my .wgsi file or in the httpd.conf
Here app.wsgi:
import sys
import site
import os
# Add virtualenv site packages
site.addsitedir(os.path.join(os.path.dirname(__file__), '/var/www/flaskapp/env/lib/python2.7/site-packages'))
#activate_this = '/var/www/flaskapp/env/bin/activate_this.py'
#execfile(activate_this, dict(__file__=activate_this))
# Path of execution
sys.path.insert(0,'/var/www/flaskapp')
#import the app
from app import app as application
And this one is the httpd.conf:
Listen 80
<VirtualHost *>
ServerName ec2...
WSGIDaemonProcess app threads=5 home=/var/www/flaskapp/ python-path=/var/www/flaskapp/env/lib/python2.7/site-packages threads=1
DocumentRoot /var/www/flaskapp
WSGIScriptAlias / /var/www/flaskapp/app.wsgi
<Directory /var/www/flaskapp>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
But I'm not able to figure out the problem. Any idea about why I get this error?
If I try to reference my app.wgsi to lib64 instead of just lib the error becomes : ImportError: numpy.core.multiarray failed to import. I checked the differente site-packages present in lib and lib64 and I have this configuration:
lib :
click - easy_install.pyc - itsdangerous.py - markerlib - setuptools - click-6.6-py2.7.egg-info - flask - itsdangerous.pyc - pip - setuptools-12.0.5.dist-info - cv2.so - Flask-0.11.1.dist-info - jinja2 - pip-6.0.8.dist-info - werkzeug - easy_install.py - itsdangerous-0.24-py2.7.egg-info - Jinja2-2.8.dist-info - pkg_resources - Werkzeug-0.11.11.dist-info
while in lib64 I have:
markupsafe MarkupSafe-0.23-py2.7.egg-info numpy numpy-1.11.1-py2.7.egg-info
It seems the mod_wsgi is configured for python 2.6.9 and I'm using python 2.7. could that be the problem?
You cannot take a mod_wsgi compiled for Python 2.6 and try and force it to use a Python virtual environment using Python 2.7. You will need to uninstall mod_wsgi and reinstall it from a binary package or source, but where it is compiled for Python 2.7. It is not a configuration option to change what version of Python is used. It must be compiled for the correct Python version to start with.
The goal is to run Django in daemon mode on Apache via WSGI. I plan to run it on port 8000 to keep it separate from PHP files running at the root of server's URL (unless there's a better way to keep my web applications separate). It's setup with Virtualenv.
Server is named server.family.local
The simplified file structure is:
/home
/wwww
/django
/my_project
/app1
/app2
/project
wsgi.py
/env
/lib
/python3.5
/site-packages
The system runs properly when I activate the Virtualenv and use the manage.py file:
/home/www/django/my_project/manage.py runserver 0.0.0.0:8000
I'm trying to setup the WSGI on Apache so I don't have to manually run that command every time. My wsgi.py file consists of this:
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('/home/www/django/my_project')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
application = get_wsgi_application()
The VirtualHost file for Apache, server_django.conf, has this:
<VirtualHost *:8000>
WSGIScriptAlias / /home/www/django/my_project/project/wsgi.py
WSGIProcessGroup my_project
WSGIDaemonProcess my_project python-path=/home/www/django/my_project/vision:/home/www/django/env/lib/python3.5/site-packages
Alias /static /home/www/django/my_project/project/static
<Directory /home/www/django/my_project/project/static>
Require all granted
</Directory>
<Directory /home/www/django/my_project/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
I've enabled the site using a2ensite. I checked that I have only libapache2-mod-wsgi-py3 installed (not libpache2-mod-python or libpache2-mod-wsgi). I think permissions are safe. Owner is my name but group is www, which is what Apache uses. The virtualenv directory has the same owner, group, and 775 permissions tag. The Python version could be an issue but I don't think it should be. When I'm in the virtualenv and run python -V, I get Python 3.5.1+. Outside the virtualenv, I get Python 2.7.12. My understanding is that this is why we use virtualenv - to isolate versions. So, if the Apache Virtual Host and the wsgi.py files are configured correctly, this shouldn't be the problem (I think).
When I try to reach my site at http://server.family.local:8000 I get a 500 Internal Server Error and find the following statements in the Apache error log.
[Wed Jul 27 22:56:05.850055 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] mod_wsgi (pid=1003): Target WSGI script '/home/www/django/my_project/project/wsgi.py' cannot be loaded as Python module.
[Wed Jul 27 22:56:05.850199 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] mod_wsgi (pid=1003): Exception occurred processing WSGI script '/home/www/django/my_project/project/wsgi.py'.
[Wed Jul 27 22:56:05.850645 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] Traceback (most recent call last):
[Wed Jul 27 22:56:05.850721 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] File "/home/www/django/my_project/project/wsgi.py", line 13, in <module>
[Wed Jul 27 22:56:05.850735 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] from django.core.wsgi import get_wsgi_application
[Wed Jul 27 22:56:05.850787 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] ImportError: No module named 'django'
[Wed Jul 27 22:56:06.062315 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] mod_wsgi (pid=1003): Target WSGI script '/home/www/django/my_project/project/wsgi.py' cannot be loaded as Python module.
[Wed Jul 27 22:56:06.062405 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] mod_wsgi (pid=1003): Exception occurred processing WSGI script '/home/www/django/my_project/project/wsgi.py'.
[Wed Jul 27 22:56:06.062573 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] Traceback (most recent call last):
[Wed Jul 27 22:56:06.062625 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] File "/home/www/django/my_project/project/wsgi.py", line 13, in <module>
[Wed Jul 27 22:56:06.062637 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] from django.core.wsgi import get_wsgi_application
[Wed Jul 27 22:56:06.062684 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] ImportError: No module named 'django'
[Wed Jul 27 22:56:06.467512 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] mod_wsgi (pid=1003): Target WSGI script '/home/www/django/my_project/project/wsgi.py' cannot be loaded as Python module.
[Wed Jul 27 22:56:06.467594 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] mod_wsgi (pid=1003): Exception occurred processing WSGI script '/home/www/django/my_project/project/wsgi.py'.
[Wed Jul 27 22:56:06.467757 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] Traceback (most recent call last):
[Wed Jul 27 22:56:06.467809 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] File "/home/www/django/my_project/project/wsgi.py", line 13, in <module>
[Wed Jul 27 22:56:06.467822 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] from django.core.wsgi import get_wsgi_application
[Wed Jul 27 22:56:06.467868 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] ImportError: No module named 'django'
[Wed Jul 27 22:56:06.658228 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] mod_wsgi (pid=1003): Target WSGI script '/home/www/django/my_project/project/wsgi.py' cannot be loaded as Python module.
[Wed Jul 27 22:56:06.658312 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] mod_wsgi (pid=1003): Exception occurred processing WSGI script '/home/www/django/my_project/project/wsgi.py'.
[Wed Jul 27 22:56:06.658476 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] Traceback (most recent call last):
[Wed Jul 27 22:56:06.658527 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] File "/home/www/django/my_project/project/wsgi.py", line 13, in <module>
[Wed Jul 27 22:56:06.658540 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] from django.core.wsgi import get_wsgi_application
[Wed Jul 27 22:56:06.658587 2016] [wsgi:error] [pid 1003] [remote 192.168.14.185:3382] ImportError: No module named 'django'
I'm not an expert but seems that the problem starts with it's unable to load the wsgi.py file but I can't see why that would be any problem. Any help would be great. Thank you.
I'm new in the world of Django. I've been working on setting up WSGI to serve my Django project. It is working fine on my personal machine but on the server I'm having a hard time to setup.
I'm using logging in Django. But it is giving 'permission denied' error. But when I place all my log files in the '/tmp' folder, it works fine.
Similar issue is with the 'db.sqlite3' file (that is my DB file which the Django uses).
Please help me out in resolving the issue.
Following information may be useful to help me out :
OS : CentOS 7
Python : 2.7.5
Apache : 2.4.6
MOD_WSGI : 3.4
Following is the WSGI's error_log:
[Wed Aug 05 01:53:31.661156 2015] [:error] [pid 32177] [remote 192.168.40.142:184] Traceback (most recent call last):
[Wed Aug 05 01:53:31.661192 2015] [:error] [pid 32177] [remote 192.168.40.142:184] File "/var/www/html/portals/wsgi.py", line 18, in <module>
[Wed Aug 05 01:53:31.661254 2015] [:error] [pid 32177] [remote 192.168.40.142:184] application = get_wsgi_application()
[Wed Aug 05 01:53:31.661274 2015] [:error] [pid 32177] [remote 192.168.40.142:184] File "/var/www/html/venv/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Wed Aug 05 01:53:31.661313 2015] [:error] [pid 32177] [remote 192.168.40.142:184] django.setup()
[Wed Aug 05 01:53:31.661330 2015] [:error] [pid 32177] [remote 192.168.40.142:184] File "/var/www/html/venv/lib/python2.7/site-packages/django/__init__.py", line 17, in setup
[Wed Aug 05 01:53:31.661361 2015] [:error] [pid 32177] [remote 192.168.40.142:184] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Wed Aug 05 01:53:31.661378 2015] [:error] [pid 32177] [remote 192.168.40.142:184] File "/var/www/html/venv/lib/python2.7/site-packages/django/utils/log.py", line 86, in configure_logging
[Wed Aug 05 01:53:31.661451 2015] [:error] [pid 32177] [remote 192.168.40.142:184] logging_config_func(logging_settings)
[Wed Aug 05 01:53:31.661483 2015] [:error] [pid 32177] [remote 192.168.40.142:184] File "/usr/lib64/python2.7/logging/config.py", line 803, in dictConfig
[Wed Aug 05 01:53:31.661519 2015] [:error] [pid 32177] [remote 192.168.40.142:184] dictConfigClass(config).configure()
[Wed Aug 05 01:53:31.661537 2015] [:error] [pid 32177] [remote 192.168.40.142:184] File "/usr/lib64/python2.7/logging/config.py", line 585, in configure
[Wed Aug 05 01:53:31.661584 2015] [:error] [pid 32177] [remote 192.168.40.142:184] '%r: %s' % (name, e))
[Wed Aug 05 01:53:31.661643 2015] [:error] [pid 32177] [remote 192.168.40.142:184] ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '/var/www/html/portals/logs/debug.log'
You need to fix permissions with the chmod command, like this:
chmod 775 /var/www/html/portals/logs/debug.log
chown username:apache /var/www/html/portals/logs/debug.log
If group is www-data,change apache to www-data
If the security not matters.You can try
chmod 777 /var/www/html/portals/logs/debug.log