How do I set up my Django Website using Apache and WSGI - django

I've joined the unhappy ranks of people who have tried to set up a Django website served by Apache (on Amazon-linux EC2).
I have successfully configured Apache and compiled mod_wsgi against Python3.4 using a virtualenv (at /home/ec2-user/web-dev) largely following instructions at https://gist.github.com/tanuka72/79ae58b16be4ac3fafe0 and the mod_wsgi docs).
However, I get an Internal Server Error when loading the Django test app.
Looking at the logs, it seems that multiple versions of Python are somehow being called during the processing (see the change from /home/ec2-user/web-dev/lib64/python3.4 to /usr/lib64/python3.7). Also it seems that mod_wsgi cannot be imported (whilst 'import mod_wsgi' works fine from a python prompt in the ec2 terminal).
[Thu Jan 19 15:19:27.329376 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] mod_wsgi (pid=9955): Failed to exec Python script file '/var/www/test/test_site/test_site/wsgi.py'.
[Thu Jan 19 15:19:27.329445 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] mod_wsgi (pid=9955): Exception occurred processing WSGI script '/var/www/test/test_site/test_site/wsgi.py'.
[Thu Jan 19 15:19:27.330049 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] Traceback (most recent call last):
[Thu Jan 19 15:19:27.330088 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] File "/var/www/test/test_site/test_site/wsgi.py", line 25, in <module>
[Thu Jan 19 15:19:27.330093 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] application = get_wsgi_application()
[Thu Jan 19 15:19:27.330108 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] File "/home/ec2-user/web-dev/lib64/python3.4/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Thu Jan 19 15:19:27.330112 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] django.setup(set_prefix=False)
[Thu Jan 19 15:19:27.330117 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] File "/home/ec2-user/web-dev/lib64/python3.4/site-packages/django/__init__.py", line 24, in setup
[Thu Jan 19 15:19:27.330121 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] apps.populate(settings.INSTALLED_APPS)
[Thu Jan 19 15:19:27.330126 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] File "/home/ec2-user/web-dev/lib64/python3.4/site-packages/django/apps/registry.py", line 89, in populate
[Thu Jan 19 15:19:27.330129 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] app_config = AppConfig.create(entry)
[Thu Jan 19 15:19:27.330134 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] File "/home/ec2-user/web-dev/lib64/python3.4/site-packages/django/apps/config.py", line 123, in create
[Thu Jan 19 15:19:27.330137 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] import_module(entry)
[Thu Jan 19 15:19:27.330142 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
[Thu Jan 19 15:19:27.330146 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] return _bootstrap._gcd_import(name[level:], package, level)
[Thu Jan 19 15:19:27.330151 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[Thu Jan 19 15:19:27.330156 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[Thu Jan 19 15:19:27.330161 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] File "<frozen importlib._bootstrap>", line 962, in _find_and_load_unlocked
[Thu Jan 19 15:19:27.330176 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] ModuleNotFoundError: No module named 'mod_wsgi.server'; 'mod_wsgi' is not a package
To debug this, I wanted to see which version of python was being called. Overriding wsgi.py to remove the call to Django's get_wsgi_application prevents the error. The URL shows the results of the application function below:
# In test_site/test_site/wsgi.py:
# ...
python_home = '/home/ec2-user/web-dev'
activator = python_home + '/bin/activate_this.py'
with open(activator) as f:
exec(f.read(), {'__file__': activator})
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('/var/www/test/test_site')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_site.settings')
#application = get_wsgi_application()
def application(environ, start_response):
status = '200 OK'
msg = f"sys.path: {sys.path} | sys.prefix: {sys.prefix} | sys.executable: {sys.executable} | Python version {sys.version}"
output = str.encode(msg)
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
The function has a confusing output in the browser:
sys.path: ['/home/ec2-user/web-dev/lib64/python3.4/site-packages', '/home/ec2-user/web-dev/lib/python3.4/site-packages', '/var/www/test/test_site/test_site/wsgi.py', '/usr/lib64/python37.zip', '/usr/lib64/python3.7', '/usr/lib64/python3.7/lib-dynload', '/var/www/test/test_site', '/var/www/test/test_site', '/var/www/test/test_site'] | sys.prefix: /home/ec2-user/web-dev | sys.executable: /home/ec2-user/web-dev/bin/python | Python version 3.7.15 (default, Oct 31 2022, 22:44:31)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-15)]
There are more folders in the PYTHONPATH than I expected, and also Python 3.7 appears to be running instead of 3.4. Interestingly this shows that mod_wsgi is working.
For reference, my httpd.conf file is updated to include:
WSGIScriptAlias / /var/www/test/test_site/test_site/wsgi.py
WSGIPythonPath /var/www/test:/home/ec2-user/web-dev/lib/python3.4/site-packages:/home/ec2-user/web-dev/lib64/python3.4/site-packages
<Directory /var/www/test/test_site/test_site>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
What can I do to get this Django app to load? This is only step one - before I try my own website, and adding a database too - it was only supposed to take a few hours. It has been a few days!

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

Two Django websites on Windows Apache

I am trying to host two Django websites on Windows (so no WSGIdaemonprocess). When I did host only one of them it worked perfectly. Now it still works (main path "/" named magazyn). But the second one (path "/awizacje" named awizacje) throws an Internal Server Error. Full error message in Apache logs looks like this:
C:\A\34\s\Modules\_decimal\libmpdec\context.c:57: warning: mpd_setminalloc: ignoring request to set MPD_MINALLOC a second time
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] mod_wsgi (pid=6268): Failed to exec Python script file 'C:/var/www2/awizacje/rootkat/awizacje/wsgi.py'.
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] mod_wsgi (pid=6268): Exception occurred processing WSGI script 'C:/var/www2/awizacje/rootkat/awizacje/wsgi.py'.
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] Traceback (most recent call last):\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "C:/var/www2/awizacje/rootkat/awizacje/wsgi.py", line 19, in <module>\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] application = get_wsgi_application()\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "C:\\var\\www\\magazyn\\env39\\Lib\\site-packages\\django\\core\\wsgi.py", line 12, in get_wsgi_application\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] django.setup(set_prefix=False)\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "C:\\var\\www\\magazyn\\env39\\Lib\\site-packages\\django\\__init__.py", line 19, in setup\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "C:\\var\\www\\magazyn\\env39\\Lib\\site-packages\\django\\conf\\__init__.py", line 76, in __getattr__\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] self._setup(name)\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "C:\\var\\www\\magazyn\\env39\\Lib\\site-packages\\django\\conf\\__init__.py", line 63, in _setup\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] self._wrapped = Settings(settings_module)\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "C:\\var\\www\\magazyn\\env39\\Lib\\site-packages\\django\\conf\\__init__.py", line 142, in __init__\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] mod = importlib.import_module(self.SETTINGS_MODULE)\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "C:\\Python39\\lib\\importlib\\__init__.py", line 127, in import_module\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] return _bootstrap._gcd_import(name[level:], package, level)\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "<frozen importlib._bootstrap>", line 1030, in _gcd_import\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "<frozen importlib._bootstrap>", line 1007, in _find_and_load\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "<frozen importlib._bootstrap>", line 1030, in _gcd_import\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "<frozen importlib._bootstrap>", line 1007, in _find_and_load\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked\r
[Mon Oct 11 14:57:35.251409 2021] [wsgi:error] [pid 6268:tid 992] [client 192.168.2.54:25532] ModuleNotFoundError: No module named 'awizacje'\r
My httpd.conf looks like this:
WSGIPythonHome "C:/var/www/magazyn/env39"
WSGIPythonPath "C:/var/www/magazyn/venv/Lib/site-packages;C:/var/www/magazyn/rootkat/"
ServerName www.magazyn-stolarz.pl
LoadFile "C:/Python39/python39.dll"
LoadModule wsgi_module "C:/var/www/magazyn/env39/lib/site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd"
WSGIScriptAlias /awizacje "C:/var/www2/awizacje/rootkat/awizacje/wsgi.py"
WSGIScriptAlias / "C:/var/www/magazyn/rootkat/magazyn/wsgi.py"
<Directory "C:/var/www/magazyn/rootkat/magazyn/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static "C:/var/www/static/"
<Directory "C:/var/www/static/">
Require all granted
</Directory>
<Directory "C:/var/www2/awizacje/rootkat/awizacje/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /statica "C:/var/www2/static/"
<Directory "C:/var/www2/static/">
Require all granted
</Directory>
My wsgi.py settings in 'awizacje' project:
"""
WSGI config for magazyn 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.2/howto/deployment/wsgi/
"""
import os
import sys
import site
from django.core.wsgi import get_wsgi_application
#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'awizacje.settings')
os.environ["DJANGO_SETTINGS_MODULE"] = "awizacje.settings"
application = get_wsgi_application()
site.addsitedir('C:/var/www2/awizacje/venv/Lib/site-packages')
sys.path.append('C:/var/www2/awizacje')
sys.path.append('C:/var/www2/awizacje/rootkat')
What am I doing wrong? I sadly can't use Linux which would be pretty straightforward.
It looks like what I was missing was appending directories BEFORE get_wsgi_application() command in wsgi.py
"""
WSGI config for magazyn 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.2/howto/deployment/wsgi/
"""
import os
import sys
import site
from django.core.wsgi import get_wsgi_application
#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'awizacje.settings')
os.environ["DJANGO_SETTINGS_MODULE"] = "awizacje.settings"
application = get_wsgi_application()
site.addsitedir('C:/var/www2/awizacje/venv/Lib/site-packages')
sys.path.append('C:/var/www2/awizacje')
sys.path.append('C:/var/www2/awizacje/rootkat')

Failed to import pip modules on aws bitnami django lightsail instance

i followed AWS tutorial for django lightsail instance: https://aws.amazon.com/es/getting-started/hands-on/deploy-python-application/
i tried to deploy my own code but apache gives me errors on piped installed modules.
It seems that instance have different paths. Some one has deployed custom django projects on django lightsail instance?
[Sun Aug 16 22:58:29.402644 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] mod_wsgi (pid=4065): Exception occurred processing WSGI script '/opt/bitnami/apps/django/django_projects/orderbot/ordermenu/wsgi.py'.
[Sun Aug 16 22:58:29.403437 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] Traceback (most recent call last):
[Sun Aug 16 22:58:29.403493 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] File "/opt/bitnami/apps/django/django_projects/orderbot/ordermenu/wsgi.py", line 22, in <module>
[Sun Aug 16 22:58:29.403504 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] application = get_wsgi_application()
[Sun Aug 16 22:58:29.403515 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] File "/opt/bitnami/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Sun Aug 16 22:58:29.403521 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] django.setup(set_prefix=False)
[Sun Aug 16 22:58:29.403530 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] File "/opt/bitnami/python/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
[Sun Aug 16 22:58:29.403535 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] apps.populate(settings.INSTALLED_APPS)
[Sun Aug 16 22:58:29.403544 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] File "/opt/bitnami/python/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
[Sun Aug 16 22:58:29.403549 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] app_config = AppConfig.create(entry)
[Sun Aug 16 22:58:29.403558 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] File "/opt/bitnami/python/lib/python3.8/site-packages/django/apps/config.py", line 90, in create
[Sun Aug 16 22:58:29.403563 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] module = import_module(entry)
[Sun Aug 16 22:58:29.403571 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] File "/opt/bitnami/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
[Sun Aug 16 22:58:29.403578 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] return _bootstrap._gcd_import(name[level:], package, level)
[Sun Aug 16 22:58:29.403587 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Sun Aug 16 22:58:29.403596 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Sun Aug 16 22:58:29.403606 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
[Sun Aug 16 22:58:29.403626 2020] [wsgi:error] [pid 4065] [client 177.240.102.67:65250] ModuleNotFoundError: No module named 'phonenumber_field'
The problem is that 'python install packages in a directory that apache do not see'.
so: packages get installed in '/home/bitnami/.local/lib/python3.8/site-packages/' and Apache looks in '/opt/bitnami/python/lib/python3.8/site-packages/'.
The temporary solution I followed is copying packages to Apache eyes folder with this command 'cp -r /home/bitnami/.local/lib/python3.8/site-packages/* /opt/bitnami/python/lib/python3.8/site-packages/'
Just needed to use the root user with sudo su command for pip install packages, now are found by apache.

How to Serve Django Application With Apache2 and Mod WSGI

I want to serve my Django applications using Apache2 and the mod_wsgi module. I have setup an Apache virtualhost at /etc/apache2/sites-available/mysite.confand I have made some configuration changes to WSGI at home/username/development/mysite/mysite/wsgi.py However, when I navigate to mydomain.com, I am being presented with the 500 Internal Server Error: The server encountered an internal error or misconfiguration and was unable to complete your request.. I am very new to server development and have never used Apache2 before so I am unsure as to where I have gone wrong in my configuration. Below you will find my virtualhost configuration file as well as my WSGI file. Additionally, I have provided the structure layout of my project directory in case this helps. Any help is greatly appreciated.
directory structure
>> home
>> username
>> development
>> mysite
>> mysite
>> settings.py
>> wsgi.py
>> website
>> static
>> manage.py
>> venv
mysite.conf
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /home/username/development/mysite
WSGIScriptAlias / /home/username/development/mysite/mysite/wsgi.py
WSGIDaemonProcess mydomain.com processes=2 threads=15 display-name=%{GROUP} python-home=/home/username/development/venv/lib/python3.5
WSGIProcessGroup mydomain.com
<directory /home/username/development/mysite>
AllowOverride all
Require all granted
Options FollowSymlinks
</directory>
Alias /static/ /home/username/development/mysite/website/static/
<Directory /home/username/development/mysite/website/static>
Require all granted
</Directory>
</VirtualHost>
wsgi.py
import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('/home/username/development/mysite')
# adjust the Python version in the line below as needed
sys.path.append('/home/username/development/venv/lib/python3.5/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
try:
application = get_wsgi_application()
except Exception:
# Error loading applications
if 'mod_wsgi' in sys.modules:
traceback.print_exc()
os.kill(os.getpid(), signal.SIGINT)
time.sleep(2.5)
error.log
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20151012/pdo_mysql.so' - /usr/lib/php/20151012/pdo_mysql.so: undefined symbol: mysqlnd_connect in Unknown on line 0
[Mon Aug 05 06:25:02.417656 2019] [wsgi:warn] [pid 11931] mod_wsgi: Compiled for Python/3.5.1+.
[Mon Aug 05 06:25:02.417679 2019] [wsgi:warn] [pid 11931] mod_wsgi: Runtime using Python/3.5.2.
[Mon Aug 05 06:25:02.418568 2019] [mpm_prefork:notice] [pid 11931] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Mon Aug 05 06:25:02.418590 2019] [core:notice] [pid 11931] AH00094: Command line: '/usr/sbin/apache2'
[Mon Aug 05 10:09:39.950527 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] mod_wsgi (pid=23705): Target WSGI script '/home/username/development/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[Mon Aug 05 10:09:39.950606 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] mod_wsgi (pid=23705): Exception occurred processing WSGI script '/home/username/development/mysite/mysite/wsgi.py'.
[Mon Aug 05 10:09:39.951482 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] Traceback (most recent call last):
[Mon Aug 05 10:09:39.951546 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "/home/username/development/mysite/mysite/wsgi.py", line 16, in <module>
[Mon Aug 05 10:09:39.951552 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] application = get_wsgi_application()
[Mon Aug 05 10:09:39.951564 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "/home/username/development/venv/lib/python3.5/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Mon Aug 05 10:09:39.951569 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] django.setup(set_prefix=False)
[Mon Aug 05 10:09:39.951577 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "/home/username/development/venv/lib/python3.5/site-packages/django/__init__.py", line 19, in setup
[Mon Aug 05 10:09:39.951597 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon Aug 05 10:09:39.951607 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "/home/username/development/venv/lib/python3.5/site-packages/django/conf/__init__.py", line 79, in __getattr__
[Mon Aug 05 10:09:39.951612 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] self._setup(name)
[Mon Aug 05 10:09:39.951619 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "/home/username/development/venv/lib/python3.5/site-packages/django/conf/__init__.py", line 66, in _setup
[Mon Aug 05 10:09:39.951623 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] self._wrapped = Settings(settings_module)
[Mon Aug 05 10:09:39.951630 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "/home/username/development/venv/lib/python3.5/site-packages/django/conf/__init__.py", line 157, in __init__
[Mon Aug 05 10:09:39.951634 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] mod = importlib.import_module(self.SETTINGS_MODULE)
[Mon Aug 05 10:09:39.951640 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "/home/username/development/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Mon Aug 05 10:09:39.951644 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] return _bootstrap._gcd_import(name[level:], package, level)
[Mon Aug 05 10:09:39.951651 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon Aug 05 10:09:39.951657 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon Aug 05 10:09:39.951664 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Mon Aug 05 10:09:39.951671 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Mon Aug 05 10:09:39.951678 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon Aug 05 10:09:39.951696 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon Aug 05 10:09:39.951704 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Mon Aug 05 10:09:39.951725 2019] [wsgi:error] [pid 23705] [remote 10.100.60.43:0] ImportError: No module named 'mysite'
[Mon Aug 05 10:52:33.048933 2019] [mpm_prefork:notice] [pid 11931] AH00169: caught SIGTERM, shutting down
mysite.conf edit
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /home/username/development/mysite
WSGIScriptAlias / /home/username/development/mysite/mysite/wsgi.py
WSGIDaemonProcess mydomain.com python-home=/home/username/development/venv python-path=/home/username/developement/mysite
WSGIProcessGroup mydomain.com
<directory /home/username/development/mysite>
AllowOverride all
Require all granted
Options FollowSymlinks
</directory>
Alias /static/ /home/username/development/mysite/website/static/
<Directory /home/username/development/mysite/website/static>
Require all granted
</Directory>
</VirtualHost>

HTTP 500 error - Target WSGI script cannot be loaded as Python module

I got the above error while using bitnami django.
All settings done according doc:
https://docs.bitnami.com/virtual-machine/components/django/#production
python version: Python 3.6.4 :: Anaconda, Inc.
django version: 2.0.2-3
wsgi.py:
import os,sys
sys.path.append('/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject')
os.environ.setdefault("PYTHON_EGG_CACHE", "/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/egg_cache")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyProject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
httpd-app.conf:
<IfDefine !IS_DJANGOSTACK_LOADED>
Define IS_DJANGOSTACK_LOADED
WSGIDaemonProcess wsgi-djangostack processes=2 threads=15 display-name=%{GROUP}
</IfDefine>
<Directory "/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject">
Options +MultiViews
AllowOverride All
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
WSGIProcessGroup wsgi-djangostack
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
Alias /MyProject/static "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/contrib/admin/static"
WSGIScriptAlias /MyProject '/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject/wsgi.py'
httpd-prefix.conf:
# Include file
Include "/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/conf/httpd-app.conf"
corresponding url added to /Applications/djangostack-2.0.2-3/apache2/conf/bitnami/bitnami-apps-prefix.conf file
But when I'm trying to access app via browser I've got error in log:
[Fri Jun 22 16:37:20.873873 2018] [wsgi:error] [pid 29099] [remote ::1:62098] mod_wsgi (pid=29099): Target WSGI script '/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject/wsgi.py' cannot be loaded as Python module.
[Fri Jun 22 16:37:20.874027 2018] [wsgi:error] [pid 29099] [remote ::1:62098] mod_wsgi (pid=29099): Exception occurred processing WSGI script '/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject/wsgi.py'.
[Fri Jun 22 16:37:20.875482 2018] [wsgi:error] [pid 29099] [remote ::1:62098] Traceback (most recent call last):
[Fri Jun 22 16:37:20.875554 2018] [wsgi:error] [pid 29099] [remote ::1:62098] File "/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject/wsgi.py", line 17, in <module>
[Fri Jun 22 16:37:20.875568 2018] [wsgi:error] [pid 29099] [remote ::1:62098] application = get_wsgi_application()
[Fri Jun 22 16:37:20.875584 2018] [wsgi:error] [pid 29099] [remote ::1:62098] File "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Jun 22 16:37:20.875594 2018] [wsgi:error] [pid 29099] [remote ::1:62098] django.setup(set_prefix=False)
[Fri Jun 22 16:37:20.875608 2018] [wsgi:error] [pid 29099] [remote ::1:62098] File "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/__init__.py", line 24, in setup
[Fri Jun 22 16:37:20.875618 2018] [wsgi:error] [pid 29099] [remote ::1:62098] apps.populate(settings.INSTALLED_APPS)
[Fri Jun 22 16:37:20.875631 2018] [wsgi:error] [pid 29099] [remote ::1:62098] File "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/apps/registry.py", line 89, in populate
[Fri Jun 22 16:37:20.875641 2018] [wsgi:error] [pid 29099] [remote ::1:62098] app_config = AppConfig.create(entry)
[Fri Jun 22 16:37:20.875654 2018] [wsgi:error] [pid 29099] [remote ::1:62098] File "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/apps/config.py", line 90, in create
[Fri Jun 22 16:37:20.875664 2018] [wsgi:error] [pid 29099] [remote ::1:62098] module = import_module(entry)
[Fri Jun 22 16:37:20.875677 2018] [wsgi:error] [pid 29099] [remote ::1:62098] File "/Applications/djangostack-2.0.2-3/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
[Fri Jun 22 16:37:20.875687 2018] [wsgi:error] [pid 29099] [remote ::1:62098] return _bootstrap._gcd_import(name[level:], package, level)
[Fri Jun 22 16:37:20.875700 2018] [wsgi:error] [pid 29099] [remote ::1:62098] File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Fri Jun 22 16:37:20.875728 2018] [wsgi:error] [pid 29099] [remote ::1:62098] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Fri Jun 22 16:37:20.875736 2018] [wsgi:error] [pid 29099] [remote ::1:62098] File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Fri Jun 22 16:37:20.875752 2018] [wsgi:error] [pid 29099] [remote ::1:62098] ModuleNotFoundError: No module named 'widget_tweaks'
I have installed widget_tweaks using:
pip3 install django-widget-tweaks
But when I stop Apache and run app using command
python3 manage.py runserver localhost:8080
the app works perfectly fine.
My urls.py under /Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject look like:
from django.contrib import admin
from django.urls import path, include,re_path
urlpatterns = [
path('', include('APP.urls')),
re_path(r'^admin/', admin.site.urls, name='admin'),
]
So I assume error within Apache configuration. Any help or advises will be appreciated!
Thank you Jota for providing one of the solution.
Solution 1:
For Mac OS, when I execute /Applications/djangostack-2.0.2-3/use_djangostack, I enter Bitnami console.
I just had to install all the necessary modules and custom project ran successfully using apache.
Solution 2:
In case you don't wanna use bitnami console,
1) Install mod_wsgi using pip, preferably into a Python virtual environment. Ensure pip is for the version of Python you want to use.
pip install mod_wsgi
2) Display the config to add to Apache configuration file to load this mod_wsgi by running:
mod_wsgi-express module-config
3) Take the output of above command to display config and add to Apache configuration. (httpd.conf file)
4) restart the apache using ctlscript.sh file. In my case the command looked like:
/Applications/djangostack-2.0.2-3/ctlscript.sh restart apache
5) After restarting, my custom project ran successfully.
Regards,
Amey Kelekar