Issue between Django 2.0 / Apache2 and WSGI - python

I'm trying to deploy my Django project in my server but I'm encountering some issues.
My environment :
Ubuntu 16.04 Server
Django 2.0
Python 3.5
Apache2 2.4
WSGI
Django configuration :
My Django project is located to : /var/www/html/DatasystemsCORE
I have wsgi.py file (/var/www/html/DatasystemsCORE/DatasystemsCORE) which looks like :
import os, sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DatasystemsCORE.settings")
application = get_wsgi_application()
I have ALLOWED_HOST like this :
ALLOWED_HOSTS = ['localhost', '172.30.10.86', '[::1]']
Apache2 configuration :
In my apache2.conf file, I have :
WSGIScriptAlias / /var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py
WSGIPythonPath /var/www/html/DatasystemsCORE
Alias /static/ /var/www/html/DatasystemsCORE/static/Theme/
<Directory /var/www/html/DatasystemsCORE/static/Theme/>
Require all granted
</Directory>
<Directory /var/www/html/DatasystemsCORE/DatasystemsCORE>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
And I have in sites-available/000-default.conf :
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/DatasystemsCORE
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
To my mind, all parameters seem to be right, but when I write : 172.30.10.86:80 in my browser, I get : 500 Internal Server Error
Traceback
This is the Traceback given by error.log in apache2 :
[Tue Apr 24 12:07:32.526764 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] mod_wsgi (pid=2611): Target WSGI script '/var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py' cannot be loaded as Python module.
[Tue Apr 24 12:07:32.526839 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] mod_wsgi (pid=2611): Exception occurred processing WSGI script '/var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py'.
[Tue Apr 24 12:07:32.526970 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] Traceback (most recent call last):
[Tue Apr 24 12:07:32.527038 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] File "/var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py", line 14, in <module>
[Tue Apr 24 12:07:32.527042 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] application = get_wsgi_application()
[Tue Apr 24 12:07:32.527048 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Tue Apr 24 12:07:32.527050 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] django.setup(set_prefix=False)
[Tue Apr 24 12:07:32.527055 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 24, in setup
[Tue Apr 24 12:07:32.527064 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] apps.populate(settings.INSTALLED_APPS)
[Tue Apr 24 12:07:32.527069 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 81, in populate
[Tue Apr 24 12:07:32.527072 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] raise RuntimeError("populate() isn't reentrant")
[Tue Apr 24 12:07:32.527086 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] RuntimeError: populate() isn't reentrant
I tried lots of things according to multiple stackoverflow questions, but none answer up to now.
Did I miss something with wsgi or mod_wsgi ?
Edit
My latest apache conf file looks like :
<VirtualHost *:80>
ServerName DatasystemsCORE
DocumentRoot /var/www/html/DatasystemsCORE/DatasystemsCORE
WSGIPassAuthorization On
WSGIScriptAlias / /var/www/html/DatasystemsCORE/DatasystemsCORE/DatasystemsCORE.wsgi
WSGIDaemonProcess DatasystemsCORE python-home=/home/valentin python-path=/var/www/html/DatasystemsCORE
Alias /static/ /var/www/html/DatasystemsCORE/static/Theme/
<Directory /var/www/html/DatasystemsCORE/static/Theme/>
Require all granted
</Directory>
<Directory /var/www/html/DatasystemsCORE/DatasystemsCORE>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
SOLUTION
Servername in my apache2.conf file should refer to my server hostname and not my Django project !

HI can you try the below configuration:
import os
from django.core.wsgi import get_wsgi_application
import sys
sys.path.append('/var/www/html/DatasystemsCORE')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DatasystemsCORE.settings")
application = get_wsgi_application()
Apache2 python wsgi module
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
<VirtualHost *:80>
ServerName dev.example.com #your server name
ServerAlias dev.example.com #your server alias
DocumentRoot #your document root
WSGIProcessGroup dev.example.com
WSGIPassAuthorization On
WSGIDaemonProcess dev.example.com python-home=/home/robert/django/robertenv python-path=/var/www/html/DatasystemsCORE <Here should be your virtual env path >
WSGIScriptAlias / /var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py
Alias /static/ /var/www/html/DatasystemsCORE/static/Theme/ #static directory
<Directory /var/www/html/DatasystemsCORE/static/Theme/>
Require all granted
</Directory>
<Directory /var/www/html/DatasystemsCORE/DatasystemsCORE>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Use above virtual host configuration

Related

how to solve 500 internal server error mod_wsgi apache "importerror: No Module named 'django'

I have been trying to solve this error but I am not been able to solve, totally frustrated because I have tried every single answer on these related question , any help would be appreciated.
My configuration of VPS server
Ubuntu 18.04
django 2.2.5
apache 2.4.49
python 3.6(virtualenv)
libapache2-mod-wsgi-py3
My folder structure is:
/var/www/dataforze/prediction_project/venv (virtualenv folder)
bin
include
lib
/var/www/dataforze/prediction_project
|- manage.py
static
venv
prediction_project
|__init__.py
|settings.py
|urls.py
|wsgi.py
000-default.conf file code
<VirtualHost *:80>
<Directory /var/www/dataforze/prediction_project/prediction_project>
Require all granted
</Directory>
<Directory /var/www/dataforze/prediction_project/prediction_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess prediction_project python-path=/var/www/dataforze/prediction_project python-home=/var/www/dataforze/prediction_project/venv
WSGIProcessGroup prediction_project
WSGIScriptAlias / /var/www/dataforze/prediction_project/prediction_project/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
LogLevel warn
</VirtualHost>
my wsgi.py file code
import os
from django.core.wsgi import get_wsgi_application
sys.path.append('/var/www/dataforze/prediction_project/prediction_project')
# add the virtualenv site-packages path to the sys.path
sys.path.append('/var/www/dataforze/prediction_project/venv/lib/site-packages')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'prediction_project.settings')
application = get_wsgi_application()
My server logs
[Wed Mar 18 17:29:01.003624 2020] [wsgi:error] [pid 82169:tid 140431942575872] mod_wsgi (pid=82169): Target WSGI script '/var/www/dataforze/prediction_project/prediction_project/wsgi.py' cannot be loaded as Python module.
[Wed Mar 18 17:29:01.003722 2020] [wsgi:error] [pid 82169:tid 140431942575872] mod_wsgi (pid=82169): Exception occurred processing WSGI script '/var/www/dataforze/prediction_project/prediction_project/wsgi.py'.
[Wed Mar 18 17:29:01.003823 2020] [wsgi:error] [pid 82169:tid 140431942575872] Traceback (most recent call last):
[Wed Mar 18 17:29:01.003844 2020] [wsgi:error] [pid 82169:tid 140431942575872] File "/var/www/dataforze/prediction_project/prediction_project/wsgi.py", line 12, in <module>
[Wed Mar 18 17:29:01.003849 2020] [wsgi:error] [pid 82169:tid 140431942575872] from django.core.wsgi import get_wsgi_application
[Wed Mar 18 17:29:01.003864 2020] [wsgi:error] [pid 82169:tid 140431942575872] ModuleNotFoundError: No module named 'django'

How to solve the error target script cannot be loaded as python module when django app deploy without port number

When i deploy django app on aws ec2 instance using ubuntu 16.04 server then it gives an error:
[Sun May 05 16:13:25.466264 2019] [wsgi:error] [pid 5649:tid 140262036731648] [remote 157.50.50.61:56646] mod_wsgi (pid=5649): Target WSGI script '/var/www/html/simple_django_project/django_project/django_project/wsgi.py' cannot be loaded as Python module.
[Sun May 05 16:13:25.466312 2019] [wsgi:error] [pid 5649:tid 140262036731648] [remote 157.50.50.61:56646] mod_wsgi (pid=5649): Exception occurred processing WSGI script '/var/www/html/simple_django_project/django_project/django_project/wsgi.py'.
[Sun May 05 16:13:25.466388 2019] [wsgi:error] [pid 5649:tid 140262036731648] [remote 157.50.50.61:56646] Traceback (most recent call last):
[Sun May 05 16:13:25.466409 2019] [wsgi:error] [pid 5649:tid 140262036731648] [remote 157.50.50.61:56646] File "/var/www/html/simple_django_project/django_project/django_project/wsgi.py", line 12, in <module>
[Sun May 05 16:13:25.466413 2019] [wsgi:error] [pid 5649:tid 140262036731648] [remote 157.50.50.61:56646] from django.core.wsgi import get_wsgi_application
[Sun May 05 16:13:25.466429 2019] [wsgi:error] [pid 5649:tid 140262036731648] [remote 157.50.50.61:56646] ImportError: No module named 'django'
000-default.conf
<VirtualHost *:80>
ServerName pythontestdjangoapp.tk
ServerAlias www.pythontestdjangoapp.tk
DocumentRoot /var/www/html/simple_django_project
ErrorLog/var/www/html/simple_django_project/django_project/logs/error.log
CustomLog
/var/www/html/simple_django_project/django_project/logs/debug.log
combined
<Directory />
#Options FollowSymLinks
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
<Directory
/var/www/html/simple_django_project/django_project/django_project/>
<Files wsgi.py>
AllowOverride All
Require all granted
</Files>
</Directory>
WSGIDaemonProcess django_project python-
path=/var/www/html/simple_djnago_project/django_project python-
home=/var/www/html/simple_django_project/myvenv
WSGIProcessGroup django_project
WSGIScriptAlias /
/var/www/html/simple_django_project/django_project/django_project/wsgi.py
</VirtualHost>
When i start apache server then it gives me an internal server error

Flask Apache WSGI can not import internal module

I'm trying to configure Apache to delploy my Flask app. It works if i run command "python flaskapp.py", but with Apache it can not import internal module Tools
This is my flask app
from flask import Flask
app = Flask(__name__)
import sys
sys.path.append('tools')
import Tools #if i comment this line, not error display and app works ok!
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
This is my .conf file
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
WSGIDaemonProcess flaskapp python-path=/var/www/html/flaskapp
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi process-group=flaskapp application-group=%{GLOBAL}
<Directory /var/www/html/flaskapp>
<Files flaskapp.wsgi>
Order allow,deny
Allow from all
</Files>
</Directory>
Alias /static /var/www/html/flaskapp/static
<Directory /var/www/html/flaskapp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
My wsgi file
import sys
sys.path.insert(0, '/var/www/html/flaskapp')
from flaskapp import app as application
My app folders ar /var/www/html/
flaskapp/
-flaskapp.py
-flaskapp.wsgi
-static/
-templates/
-tools/
-Tools.py
Error log
[Thu Mar 09 18:23:23.634661 2017] [wsgi:error] [pid 3711:tid 140422583609088] [remote 200.69.225.201:9142] mod_wsgi (pid=3711): Target WSGI script '/var/www/html/flaskapp/flaskapp.wsgi' cannot be loaded as Python module.
[Thu Mar 09 18:23:23.634694 2017] [wsgi:error] [pid 3711:tid 140422583609088] [remote 200.69.225.201:9142] mod_wsgi (pid=3711): Exception occurred processing WSGI script '/var/www/html/flaskapp/flaskapp.wsgi'.
[Thu Mar 09 18:23:23.634719 2017] [wsgi:error] [pid 3711:tid 140422583609088] [remote 200.69.225.201:9142] Traceback (most recent call last):
[Thu Mar 09 18:23:23.634734 2017] [wsgi:error] [pid 3711:tid 140422583609088] [remote 200.69.225.201:9142] File "/var/www/html/flaskapp/flaskapp.wsgi", line 4, in <module>
[Thu Mar 09 18:23:23.634755 2017] [wsgi:error] [pid 3711:tid 140422583609088] [remote 200.69.225.201:9142] from flaskapp import app as application
[Thu Mar 09 18:23:23.634762 2017] [wsgi:error] [pid 3711:tid 140422583609088] [remote 200.69.225.201:9142] File "/var/www/html/flaskapp/flaskapp.py", line 6, in <module>
[Thu Mar 09 18:23:23.634771 2017] [wsgi:error] [pid 3711:tid 140422583609088] [remote 200.69.225.201:9142] import Tools
[Thu Mar 09 18:23:23.634783 2017] [wsgi:error] [pid 3711:tid 140422583609088] [remote 200.69.225.201:9142] ImportError: No module named Tools

Flask and Apache2: No module named app

I'm trying to deploy Flask app via Apache2 but I'm getting No module named app error. I'm also using virtualenv
planer(Flask app)
(venv)jdoe#jdoe-virtual-machine:/var/www/planer$ la
app.py forms.py .git helpers.py __init__.py main.pyc models.pyc README.md setup.cfg templates views.py
app.pyc forms.pyc .gitignore helpers.pyc main.py models.py planer.wsgi requirements.txt static venv views.pyc
planer.conf
<VirtualHost *:80>
ServerName planer.dev
ServerAdmin admin#mywebsite.com
WSGIScriptAlias / /var/www/planer/planer.wsgi
<Directory /var/www/planer/planer/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/planer/static
<Directory /var/www/planer/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
planer.wsgi
import sys, os
sys.path.insert (0,'/var/www/planer')
sys.path = [
'/var/www/planer/venv/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
]
activate_this = '/var/www/planer/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import app as application
error
[Wed Jan 06 09:45:29.781102 2016] [:error] [pid 20727] [client 127.0.0.1:42448] mod_wsgi (pid=20727): Target WSGI script '/var/www/planer/planer.wsgi' cannot be loaded as Python module.
[Wed Jan 06 09:45:29.781137 2016] [:error] [pid 20727] [client 127.0.0.1:42448] mod_wsgi (pid=20727): Exception occurred processing WSGI script '/var/www/planer/planer.wsgi'.
[Wed Jan 06 09:45:29.781208 2016] [:error] [pid 20727] [client 127.0.0.1:42448] Traceback (most recent call last):
[Wed Jan 06 09:45:29.781235 2016] [:error] [pid 20727] [client 127.0.0.1:42448] File "/var/www/planer/planer.wsgi", line 12, in <module>
[Wed Jan 06 09:45:29.781302 2016] [:error] [pid 20727] [client 127.0.0.1:42448] import app as application
[Wed Jan 06 09:45:29.781327 2016] [:error] [pid 20727] [client 127.0.0.1:42448] ImportError: No module named app
Make sure the .wsgi file is executable:
sudo chmod a+x planer.wsgi
And try to load it with this:
python /absolute/path/to/planer.wsgi

Target WSGI script cannot be loaded as Python module + ImportError: No module named

I am having a an error while using mod_wsgi to deploy my flask app. This is the error log:
[Sat Feb 10 01:58:24.785611 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] mod_wsgi (pid=17184): Target WSGI script '/var/www/itemcatalog/itemcatalog.wsgi' cannot be loaded as Python module.
[Sat Feb 10 01:58:24.785635 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] mod_wsgi (pid=17184): Exception occurred processing WSGI script '/var/www/itemcatalog/itemcatalog.wsgi'.
[Sat Feb 10 01:58:24.785649 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] Traceback (most recent call last):
[Sat Feb 10 01:58:24.785664 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] File "/var/www/itemcatalog/itemcatalog.wsgi", line 6, in <module>
[Sat Feb 10 01:58:24.785710 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] from itemcatalog import app as application
[Sat Feb 10 01:58:24.785729 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] ImportError: No module named itemcatalog
I have an init.py file inside itemcatalog directory, I did some search and found that maybe that python doesn't identify my itemcatalog as a package, what is the reason for this?
Python was installed globally but then I also tried to also install it inside a virtual environment.
This is the code for the virtual host:
<VirtualHost *:80>
ServerName 18.194.244.229
ServerAdmin admin#18.194.244.229
WSGIDaemonProcess itemcatalog python-path=/var/www/itemcatalog:/var/www/itemcatalog/venv/lib/python2.7/site-packages
WSGIProcessGroup itemcatalog
WSGIScriptAlias / /var/www/itemcatalog/itemcatalog.wsgi
<Directory /var/www/itemcatalog/>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And this is the file structure inside /var/www/itemcatalog/
Instead of:
WSGIDaemonProcess itemcatalog python-path=/var/www/itemcatalog:/var/www/itemcatalog/venv/lib/python2.7/site-packages
use:
WSGIDaemonProcess itemcatalog python-path=/var/www \
python-home=/var/www/itemcatalog/venv
The Python path must be the parent directory of the package, not the package directory itself.
Instead of using python-path to setup virtual environment, use python-home as explained in official documentation:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

Categories