Can't connect MOD_WSGI .conf file with my DATABASE (Exception: (1046, 'No database selected')) - python-3.x

the thing is that i created a web app in python with flask, connected to an a mysql server.
Im trying to run it on a apache2 server with mod wsgi.
The web app in apache is working, after install all packages required, created an env with python3.9..
but when i'm trying to do the login, the apache error log shows an error:
[Mon May 23 11:08:42.395088 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50084] Exception: (1046, 'No database selected')
when i change the user on /etc/.../sites-available/app.conf file, the user that is trying to connect database is the same that the user is in the .../sites-available/app.conf file
FILE APP.CONF
<VirtualHost *:80>
ServerAdmin apipython#localhost
ServerName apipython.asix
ServerAlias www.apipython.asix
DocumentRoot /var/www/apipython
WSGIDaemonProcess apipython user=www-data threads=10
WSGIScriptAlias / /var/www/apipython/app.wsgi
<Directory /var/www/apipython>
WSGIProcessGroup apipython
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
when change the user www-data, is that the user trying to connect database
I had to create a user on mysql server name www-data
data base user created
i dont know how to specify a database on app.conf file.
im showing my error log, and my app.wsgi file..
ERROR LOG:
[Mon May 23 11:21:51.515747 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] [2022-05-23 11:21:51,515] ERROR in app: Exception on /login [POST]
[Mon May 23 11:21:51.515859 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] Traceback (most recent call last):
[Mon May 23 11:21:51.515889 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/var/www/apipython/models/ModelUser.py", line 47, in getAccount
[Mon May 23 11:21:51.515911 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] cursor.execute(sql)
[Mon May 23 11:21:51.515936 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/MySQLdb/cursors.py", line 206, in execute
[Mon May 23 11:21:51.515958 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] res = self._query(query)
[Mon May 23 11:21:51.515978 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
[Mon May 23 11:21:51.516027 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] db.query(q)
[Mon May 23 11:21:51.516052 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/MySQLdb/connections.py", line 254, in query
[Mon May 23 11:21:51.516072 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] _mysql.connection.query(self, query)
[Mon May 23 11:21:51.516092 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] MySQLdb._exceptions.OperationalError: (1046, 'No database selected')
[Mon May 23 11:21:51.516112 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226]
[Mon May 23 11:21:51.516133 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] During handling of the above exception, another exception occurred:
[Mon May 23 11:21:51.516153 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226]
[Mon May 23 11:21:51.516173 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] Traceback (most recent call last):
[Mon May 23 11:21:51.516192 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/flask/app.py", line 2077, in wsgi_app
[Mon May 23 11:21:51.516212 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] response = self.full_dispatch_request()
[Mon May 23 11:21:51.516234 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/flask/app.py", line 1525, in full_dispatch_request
[Mon May 23 11:21:51.516254 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] rv = self.handle_user_exception(e)
[Mon May 23 11:21:51.516273 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/flask/app.py", line 1523, in full_dispatch_request
[Mon May 23 11:21:51.516295 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] rv = self.dispatch_request()
[Mon May 23 11:21:51.516315 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/flask/app.py", line 1509, in dispatch_request
[Mon May 23 11:21:51.516334 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
[Mon May 23 11:21:51.516354 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/var/www/apipython/app.py", line 83, in login
[Mon May 23 11:21:51.516373 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] accounts = ModelUser.getAccount(db,logged_user)
[Mon May 23 11:21:51.516395 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/var/www/apipython/models/ModelUser.py", line 58, in getAccount
[Mon May 23 11:21:51.516415 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] raise Exception(ex)
[Mon May 23 11:21:51.516443 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] Exception: (1046, 'No database selected')
APP.WSGI FILE:
#from app import app as applicationimport sys
import sys
sys.path.insert(0, '/var/www/apipython/')
sys.path.append('anaconda3/envs/iaw/lib/python3.9/site-packages/')
activate_this = '/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
from app import app as application
PD: THE DATABASE IS ALREADY CONFIGURE IN THE CONF.PY FILE THAT I CREATED WHILE I WAS DEVELOPING THE APP, AND IS IMPORTED ON MY APP.PY
CONFIG.PY:
class Config:
SECRET_KEY = 'B!1w8NAt1T^%kvhUI*S^'
class DevelopmentConfig(Config):
#DEBUG = True
#MYSQL_HOST = '192.168.127.62'
#MYSQL_USER = 'webserver'
#MYSQL_PASSWORD = 'alumne'
#MYSQL_DB = 'inmibank_bbdd'
DEBUG = True
MYSQL_HOST = 'localhost'
MYSQL_USER = 'root'
MYSQL_PASSWORD = 'alumne'
MYSQL_DB = 'proba2'
config = {
'development': DevelopmentConfig
}
IMPORT CONFIG IN MY APP.PY
WEB PAGE LOCALHSOT:
web is working fine in development server, served by python in visualcode
if u guys have any idea let me know please.
Thanks BTW

OMG, I BEEN AT LEAST 2 DAYS TRIYING TO SOLVE THIS
THE ONLY THING THAT I DID IS CHANGE THE USER AND GROUP FROM MY APP.CONF FILE
I SHOW YOU GUYS
<VirtualHost *:80>
ServerAdmin apipython#localhost
ServerName apipython.asix
ServerAlias www.apipython.asix
DocumentRoot /var/www/apipython
WSGIDaemonProcess apipython user=alumne threads=5 group=alumne
WSGIScriptAlias / /var/www/apipython/app.wsgi
<Directory /var/www/apipython>
WSGIProcessGroup apipython
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
NOW IS WORKING FOR MEEE!!!
WORKING

Related

Django unspecified error when deploying on Apache2

Below is the error when I try deploy Django==3.2.12, wagtail==2.16.1, python 3.8.2, mod_wsgi 4.9 on a Debian 11 server. I have a full stack trace at the end.
class SimpleLazyObject(LazyObject):
TypeError: Error when calling the metaclass bases
'property' object is not callable
When I run this on my local dev machine which is a Mac it works just fine. I found someone with the exact same problem but no clarity about how it was resolved.
The resources I have been using to assist are 1, 2, 3, 4. I have tried installing mod_wsgi using pip and as well as from sources. Used various guides for Apache2 config. Upgraded from python 3.8 to 3.9 as well as downgraded to 3.7.
Create interpreter '0.0.0.0|'.
[Tue Mar 29 11:49:06.679710 2022] [wsgi:info] [pid 199764:tid 139683953432320] mod_wsgi (pid=199764): Adding '/var/www/html/portal' to path.
[Tue Mar 29 11:49:06.680057 2022] [wsgi:info] [pid 199764:tid 139683953432320] mod_wsgi (pid=199764): Adding '/var/www/html/portal/portalvenv/lib/python3.8/site-packages' to path.
[Tue Mar 29 11:49:06.689628 2022] [wsgi:info] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] mod_wsgi (pid=199764, process='saida-befree_portal', application='0.0.0.0|'): Loading Python script file '/var/www/html/portal/portal/wsgi.py'.
[Tue Mar 29 11:49:06.705081 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] mod_wsgi (pid=199764): Failed to exec Python script file '/var/www/html/portal/portal/wsgi.py'.
[Tue Mar 29 11:49:06.713633 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] mod_wsgi (pid=199764): Exception occurred processing WSGI script '/var/www/html/portal/portal/wsgi.py'.
[Tue Mar 29 11:49:06.713903 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] Traceback (most recent call last):
[Tue Mar 29 11:49:06.714017 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] File "/var/www/html/portal/portal/wsgi.py", line 12, in <module>
[Tue Mar 29 11:49:06.714210 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] from django.core.wsgi import get_wsgi_application
[Tue Mar 29 11:49:06.714304 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] File "/var/www/html/portal/portalvenv/lib/python3.8/site-packages/django/__init__.py", line 1, in <module>
[Tue Mar 29 11:49:06.717663 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] from django.utils.version import get_version
[Tue Mar 29 11:49:06.717837 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] File "/var/www/html/portal/portalvenv/lib/python3.8/site-packages/django/utils/version.py", line 7, in <module>
[Tue Mar 29 11:49:06.718002 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] from django.utils.regex_helper import _lazy_re_compile
[Tue Mar 29 11:49:06.718072 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] File "/var/www/html/portal/portalvenv/lib/python3.8/site-packages/django/utils/regex_helper.py", line 10, in <module>
[Tue Mar 29 11:49:06.718305 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] from django.utils.functional import SimpleLazyObject
[Tue Mar 29 11:49:06.720700 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] File "/var/www/html/portal/portalvenv/lib/python3.8/site-packages/django/utils/functional.py", line 362, in <module>
[Tue Mar 29 11:49:06.721066 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] class SimpleLazyObject(LazyObject):
[Tue Mar 29 11:49:06.721539 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] TypeError: Error when calling the metaclass bases
[Tue Mar 29 11:49:06.721610 2022] [wsgi:error] [pid 199764:tid 139683953432320] [remote xx.xx.xx.xxx:yyyy] 'property' object is not callable
[Tue Mar 29 11:49:58.057546 2022] [reqtimeout:info] [pid 199765:tid 139683870795520] [client xx.xx.xx.xxx:yyyy] AH01382: Request header read timeout
I had the same issue so i did the following steps:
Remove the apache2 library by using the following command-
sudo apt-get remove python-pip apache2 libapache2-mod-wsgi
Then use the following command to install the correct library-
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
This solved my error.

WSGI permission error when deploying flask app to apache2

[Mon Jan 24 17:01:59.755348 2022] [wsgi:error] [pid 6837:tid 139969656289024] [remote xxx.xxx.xxx.xxx:xxxxx] mod_wsgi (pid=6837): Failed to exec Python script file '/var/www/FlaskDeliverN/app.wsgi'.
[Mon Jan 24 17:01:59.755407 2022] [wsgi:error] [pid 6837:tid 139969656289024] [remote xxx.xxx.xxx.xxx:xxxxx] mod_wsgi (pid=6837): Exception occurred processing WSGI script '/var/www/FlaskDeliverN/app.wsgi'.
[Mon Jan 24 17:01:59.760074 2022] [wsgi:error] [pid 6837:tid 139969656289024] [remote xxx.xxx.xxx.xxx:xxxxx] Traceback (most recent call last):
[Mon Jan 24 17:01:59.760109 2022] [wsgi:error] [pid 6837:tid 139969656289024] [remote xxx.xxx.xxx.xxx:xxxxx] File "/var/www/FlaskDeliverN/app.wsgi", line 5, in <module>
[Mon Jan 24 17:01:59.760114 2022] [wsgi:error] [pid 6837:tid 139969656289024] [remote xxx.xxx.xxx.xxx:xxxxx] with open(activate_this) as file_:
[Mon Jan 24 17:01:59.760129 2022] [wsgi:error] [pid 6837:tid 139969656289024] [remote xxx.xxx.xxx.xxx:xxxxx] PermissionError: [Errno 13] Permission denied: '/root/.local/share/virtualenvs/FlaskDeliverN-8O9_IGcY/bin/activate_this.py'
I keep on getting the above error. I am not sure why there would be a permission error here. I checked that the file existed and wasn't empty so that's not the issue. I have looked through many tutorials on how to deploy a flask app but it never works and I think this may be why.
I followed this tutorial:https://www.youtube.com/watch?v=w0QDAg85Oow

500 Internal server error "populate() isn't reentrant"

I am having issues with deploying some recent work on a Django app to my server. On deployment, I updated a few pip modules, and there was also some restructuring of the directories. Once done, I restarted apache2, but was greated by the below error messages which proved unhelpful
[Thu May 13 13:12:09.846428 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] mod_wsgi (pid=8385): Target WSGI script '/var/www/stage.admin.agile.coop/web/mysite/wsgi.py' cannot be loaded as Python module.
[Thu May 13 13:12:09.846751 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] mod_wsgi (pid=8385): Exception occurred processing WSGI script '/var/www/stage.admin.agile.coop/web/mysite/wsgi.py'.
[Thu May 13 13:12:09.847024 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] Traceback (most recent call last):
[Thu May 13 13:12:09.847174 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] File "/var/www/stage.admin.agile.coop/web/mysite/wsgi.py", line 16, in <module>
[Thu May 13 13:12:09.847275 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] application = get_wsgi_application()
[Thu May 13 13:12:09.847363 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] File "/var/www/stage.admin.agile.coop/venv/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Thu May 13 13:12:09.847460 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] django.setup(set_prefix=False)
[Thu May 13 13:12:09.847561 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] File "/var/www/stage.admin.agile.coop/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
[Thu May 13 13:12:09.847655 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] apps.populate(settings.INSTALLED_APPS)
[Thu May 13 13:12:09.847755 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] File "/var/www/stage.admin.agile.coop/venv/lib/python3.6/site-packages/django/apps/registry.py", line 83, in populate
[Thu May 13 13:12:09.847850 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] raise RuntimeError("populate() isn't reentrant.")
[Thu May 13 13:12:09.847944 2021] [wsgi:error] [pid 8385:tid 140483483444992] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] RuntimeError: populate() isn't reentrant.
[Thu May 13 13:12:10.281512 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] mod_wsgi (pid=8385): Target WSGI script '/var/www/stage.admin.agile.coop/web/mysite/wsgi.py' cannot be loaded as Python module.
[Thu May 13 13:12:10.281766 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] mod_wsgi (pid=8385): Exception occurred processing WSGI script '/var/www/stage.admin.agile.coop/web/mysite/wsgi.py'.
[Thu May 13 13:12:10.282035 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] Traceback (most recent call last):
[Thu May 13 13:12:10.282166 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] File "/var/www/stage.admin.agile.coop/web/mysite/wsgi.py", line 16, in <module>
[Thu May 13 13:12:10.282266 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] application = get_wsgi_application()
[Thu May 13 13:12:10.282360 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] File "/var/www/stage.admin.agile.coop/venv/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Thu May 13 13:12:10.282439 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] django.setup(set_prefix=False)
[Thu May 13 13:12:10.282550 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] File "/var/www/stage.admin.agile.coop/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
[Thu May 13 13:12:10.282626 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] apps.populate(settings.INSTALLED_APPS)
[Thu May 13 13:12:10.282704 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] File "/var/www/stage.admin.agile.coop/venv/lib/python3.6/site-packages/django/apps/registry.py", line 83, in populate
[Thu May 13 13:12:10.282791 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] raise RuntimeError("populate() isn't reentrant.") [Thu May 13 13:12:10.282883 2021] [wsgi:error] [pid 8385:tid 140483365897984] [remote 2a02:c7f:6c19:2c00:4150:e3c6:fe11:d603:34690] RuntimeError: populate() isn't reentrant.
On getting this, I followed some of the debugging tips on posts such as Django stops working with RuntimeError: populate() isn't reentrant which uncovered some modules to update in pip. Once done, those messages went away, but I still get this message.
Any further tips on how to debug this would be greatly received.
Dan
for this message ... RuntimeError("populate() isn't reentrant.") ... it is important to find the first error message in Apache error log file after restarting Apache. So please stop/start Apache and see if there is a different (another) error message first.
if there is nothing to find change registry.py like explained here and see if there is more clear error message:
Django stops working with RuntimeError: populate() isn't reentrant

Why apache find files on system root directory instead of project directory, I got error like FileNotFound

Apache does not find files on my project directory which is /var/www/web_app_video_synopsis but they find files/directories on system root '/'
when I hint the URL http://<server IP:5000>
The Apache get the error cat /var/log/apache2/error.log
[Mon May 18 09:14:42.292029 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] ERROR:main:Exception on / [GET]
[Mon May 18 09:14:42.292060 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] Traceback (most recent call last):
[Mon May 18 09:14:42.292064 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
[Mon May 18 09:14:42.292068 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] response = self.full_dispatch_request()
[Mon May 18 09:14:42.292071 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
[Mon May 18 09:14:42.292075 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] rv = self.handle_user_exception(e)
[Mon May 18 09:14:42.292078 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
[Mon May 18 09:14:42.292081 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] reraise(exc_type, exc_value, tb)
[Mon May 18 09:14:42.292084 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
[Mon May 18 09:14:42.292094 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] raise value
[Mon May 18 09:14:42.292097 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
[Mon May 18 09:14:42.292100 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] rv = self.dispatch_request()
[Mon May 18 09:14:42.292102 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
[Mon May 18 09:14:42.292105 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] return self.view_functions[rule.endpoint](**req.view_args)
[Mon May 18 09:14:42.292108 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/var/www/web_app_video_synopsis/main.py", line 147, in student
[Mon May 18 09:14:42.292111 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] thumbnail_list, _ = load_video_with_thumbnail()
[Mon May 18 09:14:42.292114 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/var/www/web_app_video_synopsis/functions.py", line 87, in load_video_with_thumbnail
[Mon May 18 09:14:42.292117 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] for file in pathlib.Path.cwd().joinpath('Processed_Tasks').iterdir():
[Mon May 18 09:14:42.292120 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/usr/lib/python3.6/pathlib.py", line 1081, in iterdir
[Mon May 18 09:14:42.292123 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] for name in self._accessor.listdir(self):
[Mon May 18 09:14:42.292125 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
[Mon May 18 09:14:42.292128 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] return strfunc(str(pathobj), *args)
[Mon May 18 09:14:42.292133 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876] FileNotFoundError: [Errno 2] No such file or directory: '/Processed_Tasks'
[Mon May 18 09:14:42.292139 2020] [wsgi:error] [pid 16418:tid 140427906770688] [client 103.104.192.170:43876]
Processed_Tasks/ already exist in my project directory
here my project structure :
web_app_video_synopsis/
├── Downloaded_Thumbnail
├── Downloaded_Video
├── Processed_Tasks
├── Processed_Videos
├── Queue
├── README.md
├── centroid_tracker
├── database
├── functions.py
├── main.py
├── preprocessing.py
├── requirements.txt
├── static
├── templates
├── web_app_video_synopsis.wsgi
└── yolact_segmentation
Project configuration file /etc/apache2/sites-available/web_app_video_synopsis.conf:
WSGIPythonPath /var/www/www/web_app_video_synopsis
<VirtualHost *:5000>
ServerName http://<Server IP>
DocumentRoot /var/www/web_app_video_synopsis/
WSGIScriptAlias / /var/www/web_app_video_synopsis/web_app_video_synopsis.wsgi
WSGIDaemonProcess web_app_video_synopsis python-path=/var/www/web_app_video_synopsis:/home/ubuntu/.virtualenvs/pytorch/lib/python3.6/site-packages/
WSGIProcessGroup web_app_video_synopsis
WSGIApplicationGroup %{GLOBAL}
<Directory /var/www/web_app_video_synopsis/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/web_app_video_synopsis/static
<Directory /var/www/web_app_video_synopsis/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
web_app_video_synopsis.wsgi file
#!/usr/bin/python
import sys
import logging
import os
#activate_this = '/home/ubuntu/.virtualenvs/pytorch/bin/activate_this.py'
activate_this = os.path.expanduser("/home/ubuntu/.virtualenvs/pytorch/bin/activate_this.py")
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/web_app_video_synopsis/")
from main import app as application
Error produce these lines in my code functions.py
for file in pathlib.Path.cwd().joinpath('Processed_Tasks').iterdir():
with open(file, mode='r') as f:
task_data = json.load(f)
In the local machine, the current working directory is the same where the main.py located in the project directory
So How I access(read/write/execute) files from /var/www/web_app_video_synopsis ?
Like my code, many files read and write in the project directory.
Solution
web_app_video_synopsis.wsgi file
#!/usr/bin/python
import sys
import logging
import os
#activate_this = '/home/ubuntu/.virtualenvs/pytorch/bin/activate_this.py'
activate_this = os.path.expanduser("/home/ubuntu/.virtualenvs/pytorch/bin/activate_this.py")
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/web_app_video_synopsis/")
os.chdir("/var/www/web_app_video_synopsis")
from main import app as application
must set project directory using os.chdir
I got the solution, just add
os.chdir("/var/www/web_app_video_synopsis") in web_app_video_synopsis.wsgi and project directory set. I also edit my question with solution

Target WSGI script '/home/rajeshkumarponnu/Documents/OFFICE/IAB_PROJECT/iab_pro/wsgi.py' cannot be loaded as Python module

I am struggling to deploy the django rest api in apache with wsgi and getting 500 error. Here is my files, please suggest/help me where i am wrong.
Even so many facing same issues and i refered that also..but i could not able fix this error.
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'iab_pro.settings')
application = get_wsgi_application()
000-default.conf:
<VirtualHost *:80>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ServerName 192.0.18.111
ServerAdmin rajeshkumar.ponnusamy#indegene.com
WSGIScriptAlias / /home/rajeshkumarponnu/Documents/OFFICE/IAB_PROJECT/iab_pro/wsgi.py
<Directory /home/rajeshkumarponnu/Documents/OFFICE/IAB_PROJECT/iab_pro/>
<Files wsgi.py>
Require all granted
</Files>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess IAB_PROJECT python-path=/home/rajeshkumarponnu/Documents/OFFICE/IAB_PROJECT python-home=/home/rajeshkumarponnu/Documents/OFFICE/venv_iab
WSGIProcessGroup IAB_PROJECT
</VirtualHost>
apache error log
[Fri Jan 10 12:14:57.407703 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] mod_wsgi (pid=17242): Target WSGI script '/home/rajeshkumarponnu/Documents/OFFICE/IAB_PROJECT/iab_pro/wsgi.py' cannot be loaded as Python module.
[Fri Jan 10 12:14:57.407764 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] mod_wsgi (pid=17242): Exception occurred processing WSGI script '/home/rajeshkumarponnu/Documents/OFFICE/IAB_PROJECT/iab_pro/wsgi.py'.
[Fri Jan 10 12:14:57.408932 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] Traceback (most recent call last):
[Fri Jan 10 12:14:57.408972 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/usr/lib/python3.6/logging/config.py", line 565, in configure
[Fri Jan 10 12:14:57.408978 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] handler = self.configure_handler(handlers[name])
[Fri Jan 10 12:14:57.408985 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/usr/lib/python3.6/logging/config.py", line 738, in configure_handler
[Fri Jan 10 12:14:57.408989 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] result = factory(**kwargs)
[Fri Jan 10 12:14:57.408995 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/usr/lib/python3.6/logging/__init__.py", line 1032, in __init__
[Fri Jan 10 12:14:57.408999 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] StreamHandler.__init__(self, self._open())
[Fri Jan 10 12:14:57.409007 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/usr/lib/python3.6/logging/__init__.py", line 1061, in _open
[Fri Jan 10 12:14:57.409011 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] return open(self.baseFilename, self.mode, encoding=self.encoding)
[Fri Jan 10 12:14:57.409028 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] FileNotFoundError: [Errno 2] No such file or directory: '/logs/error.log'
[Fri Jan 10 12:14:57.409037 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210]
[Fri Jan 10 12:14:57.409041 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] During handling of the above exception, another exception occurred:
[Fri Jan 10 12:14:57.409044 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210]
[Fri Jan 10 12:14:57.409050 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] Traceback (most recent call last):
[Fri Jan 10 12:14:57.409078 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/home/rajeshkumarponnu/Documents/OFFICE/IAB_PROJECT/iab_pro/wsgi.py", line 16, in <module>
[Fri Jan 10 12:14:57.409083 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] application = get_wsgi_application()
[Fri Jan 10 12:14:57.409092 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/home/rajeshkumarponnu/Documents/OFFICE/venv_iab/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Jan 10 12:14:57.409096 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] django.setup(set_prefix=False)
[Fri Jan 10 12:14:57.409102 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/home/rajeshkumarponnu/Documents/OFFICE/venv_iab/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
[Fri Jan 10 12:14:57.409114 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Fri Jan 10 12:14:57.409121 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/home/rajeshkumarponnu/Documents/OFFICE/venv_iab/lib/python3.6/site-packages/django/utils/log.py", line 75, in configure_logging
[Fri Jan 10 12:14:57.409125 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] logging_config_func(logging_settings)
[Fri Jan 10 12:14:57.409131 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/usr/lib/python3.6/logging/config.py", line 802, in dictConfig
[Fri Jan 10 12:14:57.409135 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] dictConfigClass(config).configure()
[Fri Jan 10 12:14:57.409140 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] File "/usr/lib/python3.6/logging/config.py", line 573, in configure
[Fri Jan 10 12:14:57.409144 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] '%r: %s' % (name, e))
[Fri Jan 10 12:14:57.409155 2020] [wsgi:error] [pid 17242:tid 140717350381312] [remote 192.0.18.111:49210] ValueError: Unable to configure handler 'file': [Errno 2] No such file or directory: '/logs/error.log'
I am using python3.6 in my system, so installed
sudo apt-get install libapache2-mod-wsgi-py3
I have tried with www-data:www-data for venev folder and as well as project folder (IAB_PROJECT) and gave 777 permission for all folders. But still same error.
Thanks in advance.
This tutorial gives a concise walkthrough on how to go about running a Django Api on an apache server, maybe it'll give you a hint on how to solve the problem. Good luck.

Resources