Deployement on a Django Api - django

I'm actually developping a Django Rest API with the djangoRestFramework, I configured followed many tutorials and finally configured it. For the next step, I tried to deploy the API on an Apache web server.
Here is my wsgi.py :
"""
WSGI config for take_care_api project.
It exposes the WSGI callable as a module-level variable named application.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
"""
import os
import sys
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'take_care_api.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
My 000-default.conf :
LoadModule wsgi_module modules/mod_wsgi_python3.so
<VirtualHost *:80>
ServerAdmin renoidur#localhost
# DocumentRoot /etc/take_care_api/
DocumentRoot /var/www/html
Alias /static /api/site/static
ErrorLog /api/site/logs/error.log
CustomLog /api/site/logs/access.log combined
<Directory /api/take_care_api/take_care_api/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess take_care_api python-path=/api/take_care_api/
WSGIProcessGroup take_care_api
WSGIScriptAlias / /api/take_care_api/take_care_api/wsgi.py
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
When I tried to access to my django project trought the browser, I received this error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at renoidur#localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Apache/2.4.38 (Debian) Server at localhost Port 9000
error.log :
[Sun Mar 07 18:06:37.772216 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] mod_wsgi (pid=19400): Failed to exec Python script file '/api/take_care_api/take_care_api/wsgi.py'.
[Sun Mar 07 18:06:37.772284 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] mod_wsgi (pid=19400): Exception occurred processing WSGI script '/api/take_care_api/take_care_api/wsgi.py'.
[Sun Mar 07 18:06:37.772410 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] Traceback (most recent call last):
[Sun Mar 07 18:06:37.772447 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] File "/api/take_care_api/take_care_api/wsgi.py", line 17, in <module>
[Sun Mar 07 18:06:37.772452 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] application = get_wsgi_application()
[Sun Mar 07 18:06:37.772459 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] File "/usr/local/lib/python3.7/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Sun Mar 07 18:06:37.772463 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] django.setup(set_prefix=False)
[Sun Mar 07 18:06:37.772469 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] File "/usr/local/lib/python3.7/dist-packages/django/__init__.py", line 24, in setup
[Sun Mar 07 18:06:37.772473 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] apps.populate(settings.INSTALLED_APPS)
[Sun Mar 07 18:06:37.772479 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] File "/usr/local/lib/python3.7/dist-packages/django/apps/registry.py", line 83, in populate
[Sun Mar 07 18:06:37.772483 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] raise RuntimeError("populate() isn't reentrant")
[Sun Mar 07 18:06:37.772498 2021] [wsgi:error] [pid 19400:tid 140138505574144] [remote ::1:36108] RuntimeError: populate() isn't reentrant
I'm a newbie so I dont't really understand what is wrong with my code.Also, I don't use Virtualenv.
Thank you very much.

Related

Apache/mod_wsgi/Django AJAX : 500 Internal Server Error: ModuleNotFoundError: No module named 'corsheaders'

Our issue is stemming from a Django project finding the corsheaders module running via Apache/WSGI. The code runs fine using the Django's local runserver but throws a 500 Internal Server Error when acccessed throught Apache (v.2.4.41). If we comment out the application and middleware in settings.py, the site and other code works fine (execpt the API functionality that needs corsheaders).
We have exhausted the online resources we can find, so thank you in advance for the advice to uninstall and reinstall django-cors-headers in a variety of ways using pip. We do use several other modules that have been installed via this gateway with no issue. The best we can tell, the issue stems from the django wsgi not seeing the module.
I included the relevant logs and settings below. I also noted the install locations for the corsheader module.
Relevant Versions
django-cors-headers==3.13.0 (installed at: /home/geekfest/.local/lib/python3.8/site-packages)
django-cors-middleware==1.5.0
python==3.8.10
Django==4.1.2
Ubuntu==20.04.1
pythonpath: ['', '/usr/lib/python38.zip', '/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/geekfest/.local/lib/python3.8/site-packages',
'/usr/local/lib/python3.8/dist-packages',
'/usr/lib/python3/dist-packages']
**settings.py**
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'corsheaders',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'stats',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.common.CommonMiddleware',
'stats.middleware.EventData',
]
ROOT_URLCONF = 'geekstats.urls'
CORS_ORIGIN_ALLOW_ALL = True
error.log
[Fri Oct 07 11:01:49.957188 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] mod_wsgi (pid=11860): Failed to exec Python script file '/home/geekfest/geekstats/csgo-geekstats/gee>
[Fri Oct 07 11:01:49.957302 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] mod_wsgi (pid=11860): Exception occurred processing WSGI script '/home/geekfest/geekstats/csgo-geeks>
[Fri Oct 07 11:01:49.960516 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] Traceback (most recent call last):
[Fri Oct 07 11:01:49.960613 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] File "/home/geekfest/geekstats/csgo-geekstats/geekstats/geekstats/wsgi.py", line 20, in <module>
[Fri Oct 07 11:01:49.960625 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] application = get_wsgi_application()
[Fri Oct 07 11:01:49.960633 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] File "/usr/lib/python3/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Oct 07 11:01:49.960637 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] django.setup(set_prefix=False)
[Fri Oct 07 11:01:49.960645 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] File "/usr/lib/python3/dist-packages/django/__init__.py", line 24, in setup
[Fri Oct 07 11:01:49.960651 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] apps.populate(settings.INSTALLED_APPS)
[Fri Oct 07 11:01:49.960674 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 91, in populate
[Fri Oct 07 11:01:49.960680 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] app_config = AppConfig.create(entry)
[Fri Oct 07 11:01:49.960690 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] File "/usr/lib/python3/dist-packages/django/apps/config.py", line 90, in create
[Fri Oct 07 11:01:49.960695 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] module = import_module(entry)
[Fri Oct 07 11:01:49.960721 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
[Fri Oct 07 11:01:49.960726 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] return _bootstrap._gcd_import(name[level:], package, level)
[Fri Oct 07 11:01:49.960735 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Fri Oct 07 11:01:49.960742 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Fri Oct 07 11:01:49.960748 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
[Fri Oct 07 11:01:49.960770 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] ModuleNotFoundError: No module named 'corsheaders'
[Fri Oct 07 11:01:50.241210 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] mod_wsgi (pid=11860): Failed to exec Python script file '/home/geekfest/geekstats/csgo-geekstats/gee>
[Fri Oct 07 11:01:50.241287 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] mod_wsgi (pid=11860): Exception occurred processing WSGI script '/home/geekfest/geekstats/csgo-geeks>
[Fri Oct 07 11:01:50.241442 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] Traceback (most recent call last):
[Fri Oct 07 11:01:50.241482 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] File "/home/geekfest/geekstats/csgo-geekstats/geekstats/geekstats/wsgi.py", line 20, in <module>
[Fri Oct 07 11:01:50.241486 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] application = get_wsgi_application()
[Fri Oct 07 11:01:50.241494 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] File "/usr/lib/python3/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Oct 07 11:01:50.241497 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] django.setup(set_prefix=False)
[Fri Oct 07 11:01:50.241504 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] File "/usr/lib/python3/dist-packages/django/__init__.py", line 24, in setup
[Fri Oct 07 11:01:50.241506 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] apps.populate(settings.INSTALLED_APPS)
[Fri Oct 07 11:01:50.241513 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 83, in populate
[Fri Oct 07 11:01:50.241516 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] raise RuntimeError("populate() isn't reentrant")
[Fri Oct 07 11:01:50.241532 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] RuntimeError: populate() isn't reentrant
wsgi.py:
import os,sys
sys.path.append('/home/geekfest/geekstats/.../geekstats')
sys.path.append('/usr/lib/python3/dist-packages')
sys.path.append('/home/geekfest/.local/lib/python3.8/site-packages')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'geekstats.settings')
application = get_wsgi_application()
Apache .conf virtual host definition:
<VirtualHost *:80>
ServerName xxx.com
ServerAlias stats.xxx.com
DocumentRoot /home/geekfest/.../geekstats/stats
ErrorLog ${APACHE_LOG_DIR}/gferror.log
CustomLog ${APACHE_LOG_DIR}/gfaccess.log combined
WSGIDaemonProcess xxx.com python-path=/usr/lib/python3.8:/home/geekfest/geekstats/...geekstats/geekstats:/usr/lib/python3/dist-packages:/home/geekfest/.local/lib/python3.8/site-packages
WSGIProcessGroup xxx.com
# Tell Apache what it should call when the “xxx” url alias is called
WSGIScriptAlias / /home/geekfest/.../geekstats/geekstats/wsgi.py
# Tell Apache where to find the “static” subdir for the django site
Alias /static /home/geekfest/.../stats/static
# Give Apache the location and permissions to where the wsgi.py file resides in>
<Directory /home/geekfest/.../geekstats/geekstats>
<Files wsgi.py>
Require all granted
Options +Indexes +ExecCGI
</Files>
</Directory>
# Give Apache the location and permissions to the static directory (images) for>
<Directory /home/geekfest/.../stats/static>
Order deny,allow
Allow from all
Require all granted
</Directory>
</VirtualHost>
Make sure you have installed coreheaders. To install it you can try pip install django-cors-headers. If you are using virtualenv for python packages then you have to specify in the apache configuration file where your virtualenv is located.
Ok, so I figured it out. I knew it had to be path challenge because the module WAS there. If you look above, you will pip installed it to /home. WSGI was working from the /user directory. Once I forced Django to tell me where my other working libraries were, I saw the issue and force installed the library to /user.
The command to direct pip to force reinstall another directoy is:
sudo pip install --upgrade --force-reinstall --target /usr/lib/python3/dist-packages/ django-cors-headers

Apache2 Ubuntu Server Module not found 'django'

I have been trying to move a django project on my Linode server into production. After trying to use Apache and wsgi by following an online tutorial by Corey Schafer, I have encountered the following problem. I cannot for the life of my understand why this is happening, and have included error logs, my wsgi.py and my .conf file below.
[Tue Sep 29 23:20:35.753537 2020] [wsgi:error] [pid 143850:tid 140318623627008] [remote 90.214.108.58:50288] from django.core.wsgi import get_wsgi_application
[Tue Sep 29 23:20:35.753556 2020] [wsgi:error] [pid 143850:tid 140318623627008] [remote 90.214.108.58:50288] ModuleNotFoundError: No module named 'django'
[Tue Sep 29 23:28:18.507904 2020] [mpm_event:notice] [pid 143849:tid 140318652456000] AH00491: caught SIGTERM, shutting down
[Tue Sep 29 23:28:18.590367 2020] [mpm_event:notice] [pid 143989:tid 139664706964544] AH00489: Apache/2.4.41 (Ubuntu) mod_wsgi/4.6.8 Python/3.8 configured -- resuming normal operations
[Tue Sep 29 23:28:18.590439 2020] [core:notice] [pid 143989:tid 139664706964544] AH00094: Command line: '/usr/sbin/apache2'
[Tue Sep 29 23:28:24.089588 2020] [wsgi:error] [pid 143990:tid 139664678135552] [remote 90.214.108.58:50411] mod_wsgi (pid=143990): Failed to exec Python script file '/home/myname/projects/uw5-backend/u>
[Tue Sep 29 23:28:24.089643 2020] [wsgi:error] [pid 143990:tid 139664678135552] [remote 90.214.108.58:50411] mod_wsgi (pid=143990): Exception occurred processing WSGI script '/home/myname/projects/uw5-b>
[Tue Sep 29 23:28:24.095135 2020] [wsgi:error] [pid 143990:tid 139664678135552] [remote 90.214.108.58:50411] Traceback (most recent call last):
[Tue Sep 29 23:28:24.095172 2020] [wsgi:error] [pid 143990:tid 139664678135552] [remote 90.214.108.58:50411] File "/home/myname/projects/uw5-backend/uw5tables/wsgi.py", line 13, in <module>
[Tue Sep 29 23:28:24.095178 2020] [wsgi:error] [pid 143990:tid 139664678135552] [remote 90.214.108.58:50411] from django.core.wsgi import get_wsgi_application
[Tue Sep 29 23:28:24.095195 2020] [wsgi:error] [pid 143990:tid 139664678135552] [remote 90.214.108.58:50411] ModuleNotFoundError: No module named 'django'
wsgi.py
import os
import sys
from django.core.wsgi import get_wsgi_application
# add the hellodjango project path into the sys.path
sys.path.append('home/myname/projects/uw5-backend')
# add the virtualenv site-packages path to the sys.path
sys.path.append('home/myname/projects/uw5-backend/venv2/bin')
# poiting to the project settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'uw5tables.settings')
application = get_wsgi_application()
.conf
<Directory /home/myname/projects/uw5-backend/uw5tables>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/myname/projects/uw5-backend/uw5tables/wsgi.py
WSGIDaemonProcess django_app python-path=/home/myname/projects/uw5-backend python-home=/home/myname/projects/uw5-backend/venv2
WSGIProcessGroup django_app
</VirtualHost>

mod_wsgi error: ModuleNotFoundError: No module named 'django'

I am trying to deploy a very basic Django application with Apache 2.4 on Ubuntu 18.04 without using a virtual environment. When wsgi.py executes, it cannot find django module.
I have tried setting sys.path in wsgi, various solutions that define different configuration settings for 000-default.conf. Changing ownership of the site-packages folder to www-data for Apache, but nothing seems to work. I could make it work using virtualenv but for a production server, I do not want to use virtualenv. I can import django in Python's command line without an issue.
Following is my sample.tst.conf, if have already activated it using a2ensite command.
<code>
<VirtualHost *:80>
ServerName sample.tst
ServerAdmin webmaster#sample.tst
DocumentRoot /var/www/html
<Directory /home/raza/projects/sample/sample>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess sample python-path=/home/raza/projects/sample
WSGIProcessGroup sample
WSGIScriptAlias / /home/raza/projects/sample/sample/wsgi.py
<Location />
WSGIProcessGroup sample
</Location>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<code>
How can I fix this problem? I am struggling with this issue for more than a week.
I am a very experienced programmer but very new to Linux, python, and Apache platform, so I may be making some obvious mistake.
I get following error in Apache log file:
[Tue Jul 02 18:05:22.458785 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] mod_wsgi (pid=12490): Target WSGI script '/home/raza/projects/sample/sample/wsgi.py' cannot be loaded as Python module.
[Tue Jul 02 18:05:22.458854 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] mod_wsgi (pid=12490): Exception occurred processing WSGI script '/home/raza/projects/sample/sample/wsgi.py'.
[Tue Jul 02 18:05:22.458916 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] Traceback (most recent call last):
[Tue Jul 02 18:05:22.459009 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] File "/home/raza/projects/sample/sample/wsgi.py", line 12, in <module>
[Tue Jul 02 18:05:22.459037 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] from django.core.wsgi import get_wsgi_application
[Tue Jul 02 18:05:22.459072 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] ModuleNotFoundError: No module named 'django'
[Tue Jul 02 18:05:22.490159 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] mod_wsgi (pid=12490): Target WSGI script '/home/raza/projects/sample/sample/wsgi.py' cannot be loaded as Python module.
[Tue Jul 02 18:05:22.490240 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] mod_wsgi (pid=12490): Exception occurred processing WSGI script '/home/raza/projects/sample/sample/wsgi.py'.
[Tue Jul 02 18:05:22.490297 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] Traceback (most recent call last):
[Tue Jul 02 18:05:22.490314 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] File "/home/raza/projects/sample/sample/wsgi.py", line 12, in <module>
[Tue Jul 02 18:05:22.490318 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] from django.core.wsgi import get_wsgi_application
[Tue Jul 02 18:05:22.490330 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] ModuleNotFoundError: No module named 'django'
Looks like django is installed into python2 (which is used by your OS).
python3 comes with pre-installed pip3 package installer.
So use "sudo pip3 install django" command to install django into python3 environment.
pip will only install the packages into python2.

Target WSGI script '/var/www/backend/backend/wsgi.py' cannot be loaded as Python module

I'm deploying a Django web app on ubuntu server. Configuration are set. But, somehow, it show an 500 Internal Server Error. I checked the apache error log and found the following error entry :
[Tue Mar 26 08:50:56.540300 2019] [wsgi:error] [pid 18832] [remote 27.61.32.236:42365] mod_wsgi (pid=18832): Target WSGI script '/var/www/backend/backend/wsgi.py' cannot be loaded as Python module.
[Tue Mar 26 08:50:56.540376 2019] [wsgi:error] [pid 18832] [remote 27.61.32.236:42365] mod_wsgi (pid=18832): Exception occurred processing WSGI script '/var/www/backend/backend/wsgi.py'.
[Tue Mar 26 08:50:56.540498 2019] [wsgi:error] [pid 18832] [remote 27.61.32.236:42365] Traceback (most recent call last):
[Tue Mar 26 08:50:56.540542 2019] [wsgi:error] [pid 18832] [remote 27.61.32.236:42365] File "/var/www/backend/backend/wsgi.py", line 12, in <module>
[Tue Mar 26 08:50:56.540553 2019] [wsgi:error] [pid 18832] [remote 27.61.32.236:42365] from django.core.wsgi import get_wsgi_application
[Tue Mar 26 08:50:56.540584 2019] [wsgi:error] [pid 18832] [remote 27.61.32.236:42365] ImportError: No module named 'django'
I have tried the solutions for the same error found on stackoverflow. Is there anything I'm still missing on these configurations ?
Apache Configuration
Listen 8000
<VirtualHost *:8000>
WSGIDaemonProcess backendapp python-home=/var/www/backend/venv python-path=/var/www/backend
WSGIProcessGroup backendapp
WSGIPassAuthorization On
WSGIScriptAlias / /var/www/backend/backend/wsgi.py
ErrorLog /var/www/backend/error.log
CustomLog /var/www/backend/access.log combined
</VirtualHost>
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings")
application = get_wsgi_application()
using python 3.6 and mod_wsgi is enabled on server.

Multiple Sites with Django, Virtualenv, and WSGI on Apache

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.