I have been trying forever to get my django api to deploy via apache. I have installed mod_wsgi for python 3.7 and my venv is using python 3.7.15. Trying to go to my django app url I am getting a 500 error.
Error log shows:
[Tue Dec 20 21:31:30.690951 2022] [:error] [pid 19216] /usr
[Tue Dec 20 21:31:30.691287 2022] [:error] [pid 19216] mod_wsgi (pid=19216): Target WSGI script '.../project/project/wsgi.py' cannot be loaded as Python module.
[Tue Dec 20 21:31:30.691323 2022] [:error] [pid 19216] mod_wsgi (pid=19216): Exception occurred processing WSGI script '.../project/project/wsgi.py'.
[Tue Dec 20 21:31:30.691393 2022] [:error] [pid 19216] Traceback (most recent call last):
[Tue Dec 20 21:31:30.691423 2022] [:error] [pid 19216] File ".../project/project/wsgi.py", line 19, in <module>
[Tue Dec 20 21:31:30.691428 2022] [:error] [pid 19216] from django.core.wsgi import get_wsgi_application
[Tue Dec 20 21:31:30.691444 2022] [:error] [pid 19216] ModuleNotFoundError: No module named 'django'
[Tue Dec 20 21:31:51.190670 2022] [:error] [pid 19217] 3.7.15 (default, Oct 31 2022, 22:44:31)
[Tue Dec 20 21:31:51.190707 2022] [:error] [pid 19217] [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)]
apache conf file:
<VirtualHost *:80>
ServerName api.project.com
DocumentRoot path/to/project/root
WSGIScriptAlias / /path/to/wsgi.py
WSGIDaemonProcess project-name processes=4 threads=1 display-name=%{GROUP} python-path=path/to/lib/python3.7/site-packages:/path/to/project/root
WSGIProcessGroup project-group
<Directory "/path/to/project/root">
Require all granted
</Directory>
#SSL stuff...
</VirtualHost>
wsgi.py:
import os
import sys
from django.core.wsgi import get_wsgi_application
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
application = get_wsgi_application()
I originally had the wrong mod_wsgi installed (built for python2). I removed that and installed python3-mod_wsgi.x86_64 and changed module path in httpd.conf. I then realized that my mod_wsgi was for python version 3.7, but my application venv was running python 3.6.8. I then wiped the venv and created a new one with python 3.7.15. Changed the conf WSGIDAEMONPROCESS path to the correct 3.7 site-packages. Still same error.
Just installing a virtual environment is not enough, then you'll have to activate it.
Run pip install -r requirements.txt to install the required packages for the project.
The Error
The first error line states Target WSGI script '.../project/project/wsgi.py' cannot be loaded as Python module.
This means your environment variable DJANGO_SETTINGS_MODULE is not set properly. In order to do it, go to .bashrc and set it to the same value as in the asgi.py/wsgi.py file.
Run source .bashrc to reload the configuration file, and restart the bash terminal (or open a new one) where you're trying to run the application.
This should work.
Related
I am trying to migrate my test app to Docker, but I am always getting the same error, despite of trying many approaches.
This is my Dockerfile:
FROM python:3.10-slim-buster
RUN apt-get update && apt-get install -y apache2 libapache2-mod-wsgi-py3
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY morabusa /app/
COPY app.conf /etc/apache2/sites-available/app.conf
RUN a2dissite 000-default.conf && a2ensite app.conf
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
This is the app.conf for Apache vHost, which I have simplified a lot, in order to try to find the issue:
<VirtualHost *:80>
ServerName morabusa.com
ErrorLog /app/morabusaprj/logs/error.log
CustomLog /app/morabusaprj/logs/access.log combine
<Directory /app>
Require all granted
</Directory>
WSGIScriptAlias / /app/morabusaprj/wsgi.py
</VirtualHost>
This is my wsgi.py file configuration (I have added the import sys, but it still fails):
import os
import sys
sys.path.append('/app')
sys.path.append('/app/morabusaprj')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'morabusaprj.settings')
application = get_wsgi_application()
Traceback:
[Wed Feb 08 17:57:56.488523 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] mod_wsgi (pid=9): Failed to exec Python script file '/app/morabusaprj/wsgi.py'.
[Wed Feb 08 17:57:56.488574 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] mod_wsgi (pid=9): Exception occurred processing WSGI script '/app/morabusaprj/wsgi.py'.
[Wed Feb 08 17:57:56.488622 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] Traceback (most recent call last):
[Wed Feb 08 17:57:56.488633 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] File "/app/morabusaprj/wsgi.py", line 17, in <module>
[Wed Feb 08 17:57:56.488635 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] from django.core.wsgi import get_wsgi_application
[Wed Feb 08 17:57:56.488644 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] ModuleNotFoundError: No module named 'django'
The thing is that, django is installed correctly in the container, but somehow it is still giving me the same error.
I have the feeling that it is something related to the wsgi does not using the right python version, but not sure how to fix this issue. Despite of my python3.10 installation, I can still see python3.7 installed in the container.
root#fd5605c69de5:/app/morabusaprj/logs# whereis python
python: /usr/bin/python3.7m /usr/bin/python3.7 /usr/lib/python3.7 /etc/python3.7 /usr/local/bin/python /usr/local/bin/python3.10-config /usr/local/bin/python3.10 /usr/local/lib/python3.10 /usr/local/lib/python3.7
python does not have the path to your site-packages. You can add the tequires path in wsgi.py. Something like:
replacement for WSGIPythonHome "d:/..../django_project/env_folder"
# choose one:
sys.path.append('d:/.../env_folder/lib/site-packages') # add individual virt.environment packages at the end of sys.path; global env packages have prio
sys.path.insert(0,'d:/.../env_folder/lib/site-packages') # add individual virt.environment packages at the beginning of sys.path; indiv. virt.env packages have prio over global env
# replacement WSGIPythonPath "d:/..../django_project/app_name"
sys.path.append('d:/.../django_project/app_name')
just need to find out the path in your python installation in the container
Im getting the error as shown in title with this environment setup.
Apache2 with mod_wsgi ,Python 3.5, Django 2.0.2 . I'm using virtualevn.
my virtual env is in : /home/santosh/Documents/project/project/
and django app is in /home/santosh/Documents/project/Reports
Below is the content of wsgi.py file
import os, sys
sys.path.append('/home/santosh/Documents/project/Reports/Reports')
sys.path.append('/home/santosh/Documents/project/Reports')
sys.path.append('/home/santosh/Documents/project/project/lib/python3.5/site-packages')
sys.path.append('/home/santosh/Documents/project/project/lib/python3.5')
sys.path.append('/home/santosh/Documents/project/project/bin')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Reports.settings")
application = get_wsgi_application()
Stacktrace:
[Sun Feb 04 20:40:39.396427 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] mod_wsgi (pid=6428): Target
WSGI script '/home/santosh/Documents/project/Reports/Reports/wsgi.py'
cannot be loaded as Python module.
[Sun Feb 04 20:40:39.398284 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] mod_wsgi (pid=6428):
Exception occurred processing WSGI script
'/home/santosh/Documents/project/Reports/Reports/wsgi.py'.
[Sun Feb 04 20:40:39.398425 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] Traceback (most recent call
last):
[Sun Feb 04 20:40:39.398475 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] File
"/home/santosh/Documents/project/Reports/Reports/wsgi.py", line 30, in
[Sun Feb 04 20:40:39.398555 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] from django.core.wsgi
import get_wsgi_application
[Sun Feb 04 20:40:39.398565 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] File
"/home/santosh/Documents/project/project/lib/python3.5/site-packages/django/init.py",
line 1, in
[Sun Feb 04 20:40:39.398591 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] from
django.utils.version import get_version
[Sun Feb 04 20:40:39.398598 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] File
"/home/santosh/Documents/project/project/lib/python3.5/site-packages/django/utils/version.py",
line 61, in
[Sun Feb 04 20:40:39.398628 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] #functools.lru_cache()
[Sun Feb 04 20:40:39.398643 2018] [wsgi:error] [pid 6428:tid
140043928524544] [client 127.0.0.1:60276] AttributeError: 'module'
object has no attribute 'lru_cache'
I ran
sudo apt remove libapache2-mod-wsgi
sudo apt install libapache2-mod-wsgi-py3
to install the Python3 version on Ubuntu 18.04 and afterwards reloaded Apache, which solved the error for me.
I stumbled here from Google looking for a result for CentOS 7, so for anyone in a similar position, here's what fixed it for me:
When you yum install mod_wsgi, it installs the python2 version. This causes pain and suffering and crazy errors like in OP.
The solution is to install the python36 (or whatever python3 version you're using) version from the IUS repo in CentOS. I had to download from pkgs.org because my system doesn't have external internet access, so I'm not sure how to do it from yum, but I can confirm that once I installed a compatible version of mod_wsgi everything started working.
After lots of trail and error and googling, finally i was able to run successfully.
I got the lru_cache error because, I did sudo apt-get install libapache2-mod-wsgi
to install mod-wsgi. I think this might have installed to default python on Ubuntu 16.04 which is python2.7.
Solution: I uninstalled libapache2-mod-wsgi from ubuntu and installed it with pip install mod-wsgi and then finally copied the path of installed mod-wsgi from site-packages directory and updated it inside apache2/mods-available/wsgi.load file.
So its better to install mod-wsgi from virtualenv or normal python3 and then update apache files instead of installing libapache2-mod-wsgi directly on ubuntu.
How I solved it
I had django in a virtualenv and initially had installed uWSGI==2.0.19.1 system wide.
Following suggestions of other answers didn't solve my problem.
I then installed uWSGI==2.0.19.1 in my virtualenv, but the problem still happened.
Even the command which uwsgi pointed to my virtualenv:
/home/user/venv/bin/uwsgi
So I finally managed to make it work by calling uwsgi using the full path from my virtualenv:
/home/user/venv/bin/uwsgi --http :8001 --module my_django_project.wsgi
Then the problem has gone away.
I also see that when it was failing I had the message below:
detected binary path: /usr/local/bin/uwsgi
So I suppose just removing that /usr/local/bin/uwsgi would solve the problem.
I created a virtualenv named 'pyapps' and installed pinax and django in it.I've installed apache 2 and mod_wsgi.I created a directory named 'apache' inside my django project(testproject) and put 'django.wsgi' file inside that directory.Here is the content of my wsgi file:
import os
import sys
# put the Django project on sys.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
os.environ['DJANGO_SETTINGS_MODULE'] = 'textpisodes.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Then I created a directory '/check/www' and put my project folder and pyapps folder inside '/check/www'.I chmoded 777 on /check/www.
Finally I created a virtual host,it's content is as follows:
<VirtualHost *:80>
ServerAdmin myemail
ServerName djangoserver
WSGIDaemonProcess textpisodes user=rajat threads=10 python-path=/check/www
WSGIProcessGroup textpisodes
WSGIScriptAlias / /check/www/textpisodes/apache/django.wsgi
<Directory /check/www/textpisodes/apache>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
Now when I try to access 'http://djangoserver',all I'm getting is Internal Server Error.Here are the contents of my apache log file.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] mod_wsgi (pid=4890): Target WSGI script '/check/www/textpisodes/apache/django.wsgi' cannot be loaded as Python module.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] mod_wsgi (pid=4890): Exception occurred processing WSGI script '/check/www/textpisodes/apache/django.wsgi'.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] File "/check/www/textpisodes/apache/django.wsgi", line 9, in <module>
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
You need to add your virtual environment path so that apache/python can find modules there.
Either put this in your apache configuration (outside of VirtualHost entry)
WSGIPythonPath /home/me/virtualenv/env1/lib/python2.7/site-packages
Or put it in the sys.path in your wsgi file.
i tried to set up apache + mod_wsgi + django on my snow leopard, but it kept gave me the same errors which listed below. i tried to follow every tutorial on the web that i could found, but still couldn't make it work :( . (im sure all the paths are added into sys.path, but don't know why it had the import error)
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] mod_wsgi (pid=4333): Target WSGI script '/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi' cannot be loaded as Python module.
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] mod_wsgi (pid=4333): Exception occurred processing WSGI script '/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi'.
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] Traceback (most recent call last):
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] File "/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi", line 8, in <module>
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
Here is the apache configuration file:
<VirtualHost *:80>
WSGIDaemonProcess localdjango processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup localdjango
ServerName localdjango
WSGIScriptAlias / /Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi
<Directory /Users/kyle/wsgi_source/django-tutorial/mysite/apache>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
here is /Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi
1 import sys
2 import os
3
4 sys.path.append('/Users/eookoo/wsgi_source/django-tutorial')
5
6 os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
7
8 import django.core.handlers.wsgi
9 application = django.core.handlers.wsgi.WSGIHandler()
Thanks in advance.
It is import to know whether you are using the operating system supplied Python or not.
If you are and that is the only Python version on the system, then Django wasn't installed into it. That or if you are using a Python virtual environment, then you haven't set up your WSGI script file or mod_wsgi to use the Python virtual environment.
If you have installed a separate Python version, and Django is installed into it, then likely that mod_wsgi was compiled against the system Python version and not your version.
Alternative to the latter is that you are hitting a problem that occurs with some Python installations that caused framework linking under MacOS X to not work properly and at run time, even though mod_wsgi was compiled against your separate Python version, it is still using the system wide Python framework. In this latter case you need to rebuild mod_wsgi against your separate Python version, but this time use the '--disable-framework' option to the 'configure' script for mod_wsgi before building it.
For a general resource on MacOS X issues for mod_wsgi, ensure you read the documentation at:
http://code.google.com/p/modwsgi/wiki/InstallationOnMacOSX
I'm trying to get Django and Apache working together using Mod_wsgi and currently I'm getting the following errors:
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] mod_wsgi (pid=4803): Target WSGI script '/home/webdev/websites/virtualenvs/polaris/polaris_project.py' cannot be loaded as Python module.
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] mod_wsgi (pid=4803): Exception occurred processing WSGI script '/home/webdev/websites/virtualenvs/polaris/polaris_project.py'.
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] Traceback (most recent call last):
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] File "/home/webdev/websites/virtualenvs/polaris/polaris_project.py", line 8, in <module>
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] import django.core.handlers.wsgi
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] ImportError: No module named django.core.handlers.wsgi
My apache conf looks like
Alias /polaris_django/media/ "/home/webdev/websites/virtualenvs/polaris/polaris/static/"
WSGIScriptAlias /polaris_django /home/webdev/websites/virtualenvs/polaris/polaris_project.py
WSGIApplicationGroup %{GLOBAL}
<Directory "/home/webdev/websites/virtualenvs/polaris">
Order deny,allow
Allow from all
</Directory>
My Mod_WSGi file looks like
import os, sys
sys.path.append('/home/webdev/websites/virtualenvs/polaris')
sys.path.append('/home/webdev/websites/virtualenvs/polaris/polaris/apps')
sys.path.append('/home/webdev/websites/virtualenvs/polaris/polaris/extra_settings')
os.environ['DJANGO_SETTINGS_MODULE'] = 'polaris.settings'
print >> sys.stderr, sys.path
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
How can I get apache serving Django properly?
It looks like you're using a virtualenv - you need to activate that within your WSGI script to set up the paths correctly.
activate_this = os.path.join("path/to/my/virtualenv", "bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
I'm guessing that apache is using either a different version of python or is using a different sys.path. What do you get as output for the sys.path?
Where is Django installed? From command line Python do:
import django
print django.__file__
If it isn't installed in appropriate directory on sys.path under /usr/lib/python2.6 or /usr/local/lib/python2.6, then that is the problem.
Presuming you actually installed Django, this may occur because you have multiple installed Python versions and you used different one to what mod_wsgi is using to install Django. Or you used a virtual environment and haven't told mod_wsgi where that is. Or you managed to install Django with permissions that Apache cannot read.
Go watch the video of talk and read through slides referenced at:
http://blog.dscpl.com.au/2010/06/sydney-pycon-modwsgi-talk-slides.html
They cover this sort of issue as well as a lot of other stuff related to permissions etc.