I can't seem to get my django site to be accessible outside of development. I have looked at countless tutorials and they all say the same thing, which I have tried. nevertheless, it doesn't work.
here is what I have
- ubuntu 20.04 installed in a virtual machine (an openstack instance)
- this is on an intranet that I can access at work or through a vpn
- I installed apache2
- created a virtual host conf file
- tested it with a dummy page -- it worked
- installed djano and a few other items that most tutorials mention
- made my demo default django app
- collected static files to myproject/static
- enabled mod_wsgi
- modified my virtual host conf file like this (using the real server name instead of "example.com"): (my project is located at /home/.../myproject)
<VirtualHost *:80>
ServerAdmin [email]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
DocumentRoot /home/.../myproject
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
Alias /static /home/.../myproject/static
<Directory /home/.../myproject/static>
Require all granted
</Directory>
Alias /media /home/.../myproject/media
<Directory /home/.../myproject/media>
Require all granted
</Directory>
<Directory /home/.../myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WsGIPassAuthorization On
WSGIDaemonProcess myproject python-home=/home/.../myproject/myprojectenv python-path=/home/.../myproject>
WSGIScriptAlias / /home/.../myproject/myproject/wsgi.py
WSGIProcessGroup myproject
</VirtualHost>
I know there are two documentroots there. I tried both (but not at the same time). The error message I get when I try to access example.com is:
in my settings.py file, I have added the correct ip address
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'my ip address', 'example.com']
What am I missing? It feels like there is a simple step that I can't figure out. Currently, I am just testing that I can see something from the outside (another computer on our intranet), so I have not turned debug off or done any of the other recommended security steps for actual production. As my dummy apache2 page was viewable from the outside (again, another computer on our intranet -- not from anywhere), it seems like it should have worked (I did not modify my virtual host at 443 for django. but it has the settings for the certificate and keys I got. So, https://example.com still correctly shows my dummy virtual host success page).
Most of what i put in the file was the same as i found everywhere. a few places added things like:
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
and
WsGIPassAuthorization On
I am a total novice, so please explain things nicely if you can.
EDIT: I've just updated mod_wsgi and run this hello world tutorial. it worked fine. https://modwsgi.readthedocs.io/en/master/user-guides/quick-configuration-guide.html
EDIT2: looking at the apache error log I see
ModuleNotFoundError: No module named 'myproject'
So, I guess that is the problem! But how do I fix it?
error in apache log
[Thu May 06 06:45:32.981147 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] mod_wsgi (pid=546592): Failed to exec Python script file '/home/web/myproject/myproject/wsgi.py'.
[Thu May 06 06:45:32.981185 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] mod_wsgi (pid=546592): Exception occurred processing WSGI script '/home/web/myproject/myproject/wsgi.py'.
[Thu May 06 06:45:32.981302 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] Traceback (most recent call last):
[Thu May 06 06:45:32.981349 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "/home/web/myproject/myproject/wsgi.py", line 16, in <module>
[Thu May 06 06:45:32.981352 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] application = get_wsgi_application()
[Thu May 06 06:45:32.981358 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Thu May 06 06:45:32.981361 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] django.setup(set_prefix=False)
[Thu May 06 06:45:32.981367 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
[Thu May 06 06:45:32.981370 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu May 06 06:45:32.981375 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
[Thu May 06 06:45:32.981378 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] self._setup(name)
[Thu May 06 06:45:32.981383 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
[Thu May 06 06:45:32.981386 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] self._wrapped = Settings(settings_module)
[Thu May 06 06:45:32.981391 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
[Thu May 06 06:45:32.981394 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu May 06 06:45:32.981400 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
[Thu May 06 06:45:32.981403 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] return _bootstrap._gcd_import(name[level:], package, level)
[Thu May 06 06:45:32.981408 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Thu May 06 06:45:32.981413 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Thu May 06 06:45:32.981419 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
[Thu May 06 06:45:32.981424 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Thu May 06 06:45:32.981429 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Thu May 06 06:45:32.981435 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Thu May 06 06:45:32.981440 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
[Thu May 06 06:45:32.981452 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] ModuleNotFoundError: No module named 'myproject'```
EDIT 3:
actually, simply running the wsgi.py file from the terminal gives the same error. now im totally confused as this is the default tutorial app. and no error occurs when the same wsgi file is run from the development server
sys.version = '3.8.5 (default, Jan 27 2021, 15:41:15) \n[GCC 9.3.0]'
sys.prefix = '/home/web/myproject/myprojectenv'
sys.path = ['/home/web/myproject/myproject', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/web/myproject/myprojectenv/lib/python3.8/site-packages']
Traceback (most recent call last):
File "myproject/wsgi.py", line 22, in <module>
application = get_wsgi_application()
File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'myproject'